import { useQuery, useMutation } from '@apollo/react-hooks'
import { makeStyles } from '@material-ui/core'
import classnames from 'classnames'
import { Form, Formik, Field as FormikField } from 'formik'
import gql from 'graphql-tag'
import * as R from 'ramda'
import React, { useState } from 'react'
import * as Yup from 'yup'
import ErrorMessage from 'src/components/ErrorMessage'
import PromptWhenDirty from 'src/components/PromptWhenDirty'
import { Link, IconButton } from 'src/components/buttons'
import { Switch } from 'src/components/inputs'
import { TextInput } from 'src/components/inputs/formik'
import { H4, Info2, Info3, Label2, Label3, P } from 'src/components/typography'
import { ReactComponent as EditIcon } from 'src/styling/icons/action/edit/enabled.svg'
import { fromNamespace, toNamespace, namespaces } from 'src/utils/config'
import { global, fieldStyles } from './OperatorInfo.styles'
const useFieldStyles = makeStyles(fieldStyles)
const Field = ({
editing,
name,
width,
placeholder,
label,
value,
multiline = false,
rows,
onFocus,
...props
}) => {
const classes = useFieldStyles()
const classNames = {
[classes.field]: true,
[classes.notEditing]: !editing,
[classes.notEditingSingleLine]: !editing && !multiline,
[classes.notEditingMultiline]: !editing && multiline
}
return (
{!editing && (
<>
{label}
{value}
>
)}
{editing && (
)}
)
}
const GET_CONFIG = gql`
query getData {
config
}
`
const SAVE_CONFIG = gql`
mutation Save($config: JSONObject) {
saveConfig(config: $config)
}
`
const useTermsConditionsStyles = makeStyles(global)
const TermsConditions = () => {
const [error, setError] = useState(null)
const [editing, setEditing] = useState(false)
const [saveConfig] = useMutation(SAVE_CONFIG, {
onCompleted: () => {
setError(null)
setEditing(false)
},
refetchQueries: () => ['getData'],
onError: e => setError(e)
})
const classes = useTermsConditionsStyles()
const { data } = useQuery(GET_CONFIG)
const termsAndConditions =
data?.config && fromNamespace(namespaces.TERMS_CONDITIONS, data.config)
const formData = termsAndConditions ?? {}
const showOnScreen = termsAndConditions?.active ?? false
const addDelayOnScreen = termsAndConditions?.delay ?? false
const tcPhoto = termsAndConditions?.tcPhoto ?? false
const save = it =>
saveConfig({
variables: { config: toNamespace(namespaces.TERMS_CONDITIONS, it) }
})
const fields = [
{
name: 'title',
label: 'Screen title',
value: formData.title ?? '',
width: 282
},
{
name: 'text',
label: 'Text content',
value: formData.text ?? '',
width: 502,
multiline: true,
rows: 6
},
{
name: 'acceptButtonText',
label: 'Accept button text',
value: formData.acceptButtonText ?? '',
placeholder: 'I accept',
width: 282
},
{
name: 'cancelButtonText',
label: 'Cancel button text',
value: formData.cancelButtonText ?? '',
placeholder: 'Cancel',
width: 282
}
]
const findField = name => R.find(R.propEq('name', name))(fields)
const findValue = name => findField(name).value
const initialValues = {
title: findValue('title'),
text: findValue('text'),
acceptButtonText: findValue('acceptButtonText'),
cancelButtonText: findValue('cancelButtonText')
}
const validationSchema = Yup.object().shape({
title: Yup.string('The screen title must be a string')
.required('The screen title is required')
.max(50, 'Too long'),
text: Yup.string('The text content must be a string').required(
'The text content is required'
),
acceptButtonText: Yup.string('The accept button text must be a string')
.required('The accept button text is required')
.max(50, 'The accept button text is too long'),
cancelButtonText: Yup.string('The cancel button text must be a string')
.required('The cancel button text is required')
.max(50, 'The cancel button text is too long')
})
return (
<>
Terms & Conditions
Show on screen
save({
active: event.target.checked
})
}
/>
{showOnScreen ? 'Yes' : 'No'}
Capture customer photo on acceptance
of Terms & Conditions
screen
save({
tcPhoto: event.target.checked
})
}
/>
{tcPhoto ? 'Yes' : 'No'}
Add 7 seconds delay on screen
save({
delay: event.target.checked
})
}
/>
{addDelayOnScreen ? 'Yes' : 'No'}
Info card
{!editing && (
setEditing(true)}>
)}
save(values)}
onReset={() => {
setEditing(false)
setError(null)
}}>
{({ errors }) => (
)}
>
)
}
export default TermsConditions