import CardContent from '@mui/material/CardContent' import Card from '@mui/material/Card' import { Form, Formik, Field as FormikField } from 'formik' import * as R from 'ramda' import { useState, React, useRef } from 'react' import ErrorMessage from 'src/components/ErrorMessage' import PromptWhenDirty from 'src/components/PromptWhenDirty' import { MainStatus } from 'src/components/Status' import { Label1, P, H3 } from 'src/components/typography' import EditIcon from 'src/styling/icons/action/edit/enabled.svg?react' import EditReversedIcon from 'src/styling/icons/action/edit/white.svg?react' import AuthorizeIcon from 'src/styling/icons/button/authorize/white.svg?react' import BlockIcon from 'src/styling/icons/button/block/white.svg?react' import CancelReversedIcon from 'src/styling/icons/button/cancel/white.svg?react' import DataReversedIcon from 'src/styling/icons/button/data/white.svg?react' import DataIcon from 'src/styling/icons/button/data/zodiac.svg?react' import ReplaceReversedIcon from 'src/styling/icons/button/replace/white.svg?react' import SaveReversedIcon from 'src/styling/icons/circle buttons/save/white.svg?react' import { ActionButton } from 'src/components/buttons' import { OVERRIDE_REJECTED, OVERRIDE_PENDING } from 'src/pages/Customers/components/consts' const ReadOnlyField = ({ field, value }) => { return (
{field.label}

{value}

) } const EditableField = ({ editing, field, value, size, ...props }) => { if (!editing) return return (
{editing && ( <> {field.label} )}
) } const EditableCard = ({ fields, save = () => {}, cancel = () => {}, authorize = () => {}, hasImage, reject = () => {}, state, title, titleIcon, children = () => {}, validationSchema, initialValues, deleteEditedData, editable, checkAgainstSanctions }) => { const formRef = useRef() const [editing, setEditing] = useState(false) const [input, setInput] = useState(null) const [error, setError] = useState(null) const triggerInput = () => input.click() const authorized = state === OVERRIDE_PENDING ? { label: 'Pending', type: 'default' } : state === OVERRIDE_REJECTED ? { label: 'Rejected', type: 'error' } : { label: 'Accepted', type: 'success' } return (
{titleIcon}

{title}

{state && authorize && }
{children(formRef.current?.values ?? {})} { save(values) setEditing(false) }} onReset={() => { setEditing(false) setError(false) }}> {({ setFieldValue }) => (
{!hasImage && fields?.map((field, idx) => { return idx >= 0 && idx < 4 ? ( !field.editable ? ( ) : ( ) ) : null })}
{!hasImage && fields?.map((field, idx) => { return idx >= 4 ? ( !field.editable ? ( ) : ( ) ) : null })}
{!editing && ( <> {checkAgainstSanctions && ( checkAgainstSanctions()}> Check against OFAC sanction list )} {editable && ( setEditing(true)}> Edit )} {!editable && authorize && authorized.label !== 'Accepted' && ( authorize()}> Authorize )} {!editable && authorize && authorized.label !== 'Rejected' && ( reject()}> Reject )} )} {editing && ( <> {hasImage && state !== OVERRIDE_PENDING && ( triggerInput()}> {
setInput(fileInput)} onChange={event => { // need to store it locally if we want to display it even after saving to db const file = R.head(event.target.files) if (!file) return setFieldValue(R.head(fields).name, file) }} /> Replace
}
)} {fields && ( Save )} cancel()} type="reset"> Cancel {authorize && authorized.label !== 'Accepted' && ( authorize()}> Authorize )} {authorize && authorized.label !== 'Rejected' && ( reject()}> Reject )} {error && ( Failed to save changes )} )}
)}
) } export default EditableCard