import { useQuery, useMutation } from '@apollo/react-hooks' import { makeStyles, Box } from '@material-ui/core' import gql from 'graphql-tag' import * as R from 'ramda' import React, { useState } from 'react' import PhoneIdIcon from 'src/styling/icons/ID/phone/zodiac.svg?react' import DeleteIcon from 'src/styling/icons/action/delete/enabled.svg?react' import { DeleteDialog } from 'src/components/DeleteDialog' import { Link, Button, IconButton } from 'src/components/buttons' import DataTable from 'src/components/tables/DataTable' import { Label3, TL1 } from 'src/components/typography' import styles from './IndividualDiscount.styles' import IndividualDiscountModal from './IndividualDiscountModal' const useStyles = makeStyles(styles) const GET_INDIVIDUAL_DISCOUNTS = gql` query individualDiscounts { individualDiscounts { id customerId discount } } ` const DELETE_DISCOUNT = gql` mutation deleteIndividualDiscount($discountId: ID!) { deleteIndividualDiscount(discountId: $discountId) { id } } ` const CREATE_DISCOUNT = gql` mutation createIndividualDiscount($customerId: ID!, $discount: Int!) { createIndividualDiscount(customerId: $customerId, discount: $discount) { id } } ` const GET_CUSTOMERS = gql` { customers { id phone idCardData } } ` const IndividualDiscounts = () => { const classes = useStyles() const [deleteDialog, setDeleteDialog] = useState(false) const [toBeDeleted, setToBeDeleted] = useState() const [errorMsg, setErrorMsg] = useState('') const [showModal, setShowModal] = useState(false) const toggleModal = () => setShowModal(!showModal) const { data: discountResponse, loading: discountLoading } = useQuery( GET_INDIVIDUAL_DISCOUNTS ) const { data: customerData, loading: customerLoading } = useQuery( GET_CUSTOMERS ) const [createDiscount, { error: creationError }] = useMutation( CREATE_DISCOUNT, { refetchQueries: () => ['individualDiscounts'] } ) const getCustomer = id => { const customers = R.path(['customers'])(customerData) return R.find(R.propEq('id', id))(customers) } const [deleteDiscount] = useMutation(DELETE_DISCOUNT, { onError: ({ message }) => { const errorMessage = message ?? 'Error while deleting row' setErrorMsg(errorMessage) }, onCompleted: () => setDeleteDialog(false), refetchQueries: () => ['individualDiscounts'] }) const elements = [ { header: 'Identification', width: 312, textAlign: 'left', size: 'sm', view: t => { const customer = getCustomer(t.customerId) return (