import { Form, Formik, Field } from 'formik' import React from 'react' import { useLazyQuery, gql } from '@apollo/client' import ErrorMessage from '../../components/ErrorMessage' import Modal from '../../components/Modal' import { HelpTooltip } from '../../components/Tooltip' import { H1, H3, P } from '../../components/typography' import * as Yup from 'yup' import { Button } from '../../components/buttons' import { NumberInput, AsyncAutocomplete } from '../../components/inputs/formik' const SEARCH_CUSTOMERS = gql` query searchCustomers($searchTerm: String!, $limit: Int) { searchCustomers(searchTerm: $searchTerm, limit: $limit) { id name phone email } } ` const initialValues = { customer: '', discount: '', } const validationSchema = Yup.object().shape({ customer: Yup.string().required('A customer is required!'), discount: Yup.number() .required('A discount rate is required!') .min(0, 'Discount rate should be a positive number!') .max(100, 'Discount rate should have a maximum value of 100%!'), }) const getErrorMsg = (formikErrors, formikTouched, mutationError) => { if (!formikErrors || !formikTouched) return null if (mutationError) return 'Internal server error' if (formikErrors.customer && formikTouched.customer) return formikErrors.customer if (formikErrors.discount && formikTouched.discount) return formikErrors.discount return null } const IndividualDiscountModal = ({ showModal, setShowModal, onClose, creationError, addDiscount, }) => { const [searchCustomersQuery] = useLazyQuery(SEARCH_CUSTOMERS) const searchCustomers = async searchTerm => { const { data } = await searchCustomersQuery({ variables: { searchTerm, limit: 20 }, }) return data?.searchCustomers || [] } const handleAddDiscount = (customer, discount) => { addDiscount({ variables: { customerId: customer, discount: parseInt(discount), }, }) setShowModal(false) } return ( <> {showModal && ( { handleAddDiscount(customer, discount) }}> {({ errors, touched }) => (
{ const name = option.name const contact = option.phone || option.email return contact ? `${name} (${contact})` : name }} getOptionId={option => option.id} placeholder="Type to search customers..." noOptionsText="Type at least 3 characters to search" minSearchLength={2} />

Define discount rate

This is a percentage discount off of your existing commission rates for a customer entering this code at the machine.

For instance, if you charge 8% commissions, and this code is set for 50%, then you'll instead be charging 4% on transactions using the code.

%

{getErrorMsg(errors, touched, creationError) && ( {getErrorMsg(errors, touched, creationError)} )}
)}
)} ) } export default IndividualDiscountModal