|
| 1 | +import React from 'react'; |
| 2 | +import { Form, validate, FieldProps, renderFields } from '@tpr/forms'; |
| 3 | +import { useInHouseAdminContext } from '../../context'; |
| 4 | +import { Footer } from '../../../components/card'; |
| 5 | +import { Content } from '../../../components/content'; |
| 6 | +import { ArrowButton } from '../../../../buttons/buttons'; |
| 7 | +import { InHouseAdminI18nProps } from '../../i18n'; |
| 8 | +import { RecursivePartial } from '../../context'; |
| 9 | + |
| 10 | +const getFields = ( |
| 11 | + fields: RecursivePartial<InHouseAdminI18nProps['contacts']['fields']>, |
| 12 | +): FieldProps[] => [ |
| 13 | + { |
| 14 | + type: 'text', |
| 15 | + name: 'telephoneNumber', |
| 16 | + label: fields.telephone.label, |
| 17 | + inputWidth: 2, |
| 18 | + error: fields.telephone.error, |
| 19 | + cfg: { mb: 3 }, |
| 20 | + }, |
| 21 | + { |
| 22 | + type: 'email', |
| 23 | + name: 'emailAddress', |
| 24 | + label: fields.email.label, |
| 25 | + inputWidth: 6, |
| 26 | + error: fields.email.error, |
| 27 | + }, |
| 28 | +]; |
| 29 | + |
| 30 | +export const Contacts: React.FC = () => { |
| 31 | + const { current, send, i18n } = useInHouseAdminContext(); |
| 32 | + const { inHouseAdmin } = current.context; |
| 33 | + const fields = getFields(i18n?.contacts?.fields); |
| 34 | + |
| 35 | + const onSubmit = (values) => { |
| 36 | + send('SAVE', { values }); |
| 37 | + }; |
| 38 | + |
| 39 | + const loading = false; |
| 40 | + |
| 41 | + return ( |
| 42 | + <Content |
| 43 | + type="inHouseAdmin" |
| 44 | + title={i18n.contacts.title} |
| 45 | + subtitle={i18n.contacts.subtitle} |
| 46 | + loading={loading} |
| 47 | + > |
| 48 | + <Form |
| 49 | + onSubmit={onSubmit} |
| 50 | + initialValues={{ |
| 51 | + telephoneNumber: inHouseAdmin.telephoneNumber, |
| 52 | + emailAddress: inHouseAdmin.emailAddress, |
| 53 | + }} |
| 54 | + validate={validate(fields)} |
| 55 | + > |
| 56 | + {({ handleSubmit }) => ( |
| 57 | + <form onSubmit={handleSubmit}> |
| 58 | + {renderFields(fields)} |
| 59 | + <Footer> |
| 60 | + <ArrowButton |
| 61 | + intent="special" |
| 62 | + pointsTo="up" |
| 63 | + iconSide="right" |
| 64 | + type="submit" |
| 65 | + title="Save and close" |
| 66 | + disabled={loading} |
| 67 | + /> |
| 68 | + </Footer> |
| 69 | + </form> |
| 70 | + )} |
| 71 | + </Form> |
| 72 | + </Content> |
| 73 | + ); |
| 74 | +}; |
0 commit comments