|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | + |
| 3 | +import _ from 'lodash'; |
| 4 | +import { useTranslations } from 'next-intl'; |
| 5 | +import { useRouter } from 'next/router'; |
| 6 | +import { |
| 7 | + Locale, |
| 8 | + PydanticForm, |
| 9 | + PydanticFormApiResponseType, |
| 10 | + PydanticFormFieldFormat, |
| 11 | + PydanticFormFieldType, |
| 12 | +} from 'pydantic-forms'; |
| 13 | +import type { |
| 14 | + PydanticFormApiProvider, |
| 15 | + PydanticFormConfig, |
| 16 | + PydanticFormDefinitionResponse, |
| 17 | + PydanticFormSuccessResponse, |
| 18 | +} from 'pydantic-forms'; |
| 19 | + |
| 20 | +import { EuiSpacer } from '@elastic/eui'; |
| 21 | + |
| 22 | +import { WfoContentHeader, WfoLoading } from '@/components'; |
| 23 | + |
| 24 | +export const WfoScheduleTaskFormPage = () => { |
| 25 | + const t = useTranslations('metadata.scheduledTasks'); |
| 26 | + const router = useRouter(); |
| 27 | + const onSuccess = () => { |
| 28 | + console.log('SUCCESS!!!'); |
| 29 | + }; |
| 30 | + |
| 31 | + const createScheduledTask = ( |
| 32 | + formInput: unknown, |
| 33 | + ): PydanticFormSuccessResponse => ({ |
| 34 | + type: PydanticFormApiResponseType.SUCCESS, |
| 35 | + data: formInput as object, |
| 36 | + status: 222, |
| 37 | + }); |
| 38 | + |
| 39 | + const validateForm = (formInput: unknown): boolean => { |
| 40 | + if ( |
| 41 | + !_.isEmpty(formInput) && |
| 42 | + _.isArray(formInput) && |
| 43 | + formInput.length === 2 |
| 44 | + ) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + return false; |
| 48 | + }; |
| 49 | + |
| 50 | + const validateStep1 = (formInput: unknown): boolean => { |
| 51 | + return !_.isEmpty(formInput); |
| 52 | + }; |
| 53 | + |
| 54 | + const formStep1: PydanticFormDefinitionResponse = { |
| 55 | + type: PydanticFormApiResponseType.FORM_DEFINITION, |
| 56 | + form: { |
| 57 | + type: PydanticFormFieldType.OBJECT, |
| 58 | + properties: { |
| 59 | + task: { |
| 60 | + type: PydanticFormFieldType.STRING, |
| 61 | + format: PydanticFormFieldFormat.DROPDOWN, |
| 62 | + $ref: '', |
| 63 | + }, |
| 64 | + taskType: { |
| 65 | + type: PydanticFormFieldType.STRING, |
| 66 | + format: PydanticFormFieldFormat.RADIO, |
| 67 | + $ref: '', |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + meta: { |
| 72 | + hasNext: true, |
| 73 | + }, |
| 74 | + }; |
| 75 | + |
| 76 | + const formStep2: PydanticFormDefinitionResponse = { |
| 77 | + type: PydanticFormApiResponseType.FORM_DEFINITION, |
| 78 | + form: { |
| 79 | + type: PydanticFormFieldType.OBJECT, |
| 80 | + properties: { |
| 81 | + firstDate: { |
| 82 | + type: PydanticFormFieldType.DATETIME, |
| 83 | + format: PydanticFormFieldFormat.LONG, |
| 84 | + $ref: '', |
| 85 | + }, |
| 86 | + schedule: { |
| 87 | + type: PydanticFormFieldType.STRING, |
| 88 | + format: PydanticFormFieldFormat.DROPDOWN, |
| 89 | + $ref: '', |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + meta: { |
| 94 | + hasNext: false, |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + const pydanticFormProvider: PydanticFormApiProvider = ({ |
| 99 | + _, |
| 100 | + requestBody = [], |
| 101 | + }) => { |
| 102 | + return new Promise<Record<string, unknown>>((resolve) => { |
| 103 | + if (validateForm(requestBody)) { |
| 104 | + const successResponse = createScheduledTask(requestBody); |
| 105 | + console.log('successPresone', successResponse); |
| 106 | + return resolve(successResponse); |
| 107 | + } else if (validateStep1(requestBody)) { |
| 108 | + resolve(formStep2); |
| 109 | + } |
| 110 | + resolve(formStep1); |
| 111 | + }).then((formDefinition) => { |
| 112 | + return formDefinition; |
| 113 | + }); |
| 114 | + }; |
| 115 | + |
| 116 | + const getConfig = (): PydanticFormConfig => { |
| 117 | + const getLocale = () => { |
| 118 | + if (router.locale) { |
| 119 | + return router.locale as Locale; |
| 120 | + } |
| 121 | + return Locale.enGB; // Default to enGB if no locale is set |
| 122 | + }; |
| 123 | + |
| 124 | + return { |
| 125 | + apiProvider: pydanticFormProvider, |
| 126 | + // footerRenderer: (props) => <Footer {...props} isTask={isTask} />, |
| 127 | + // headerRenderer: Header, |
| 128 | + // componentMatcherExtender: wfoComponentMatcherExtender, |
| 129 | + // labelProvider: pydanticLabelProvider, |
| 130 | + // rowRenderer: Row, |
| 131 | + // customTranslations: customTranslations, |
| 132 | + loadingComponent: <WfoLoading />, |
| 133 | + locale: getLocale(), |
| 134 | + }; |
| 135 | + }; |
| 136 | + |
| 137 | + return ( |
| 138 | + <> |
| 139 | + <WfoContentHeader title={t('addSchedule')} /> |
| 140 | + <EuiSpacer size="l" /> |
| 141 | + <PydanticForm |
| 142 | + formKey="add-schedule" |
| 143 | + formId="add-schedule" |
| 144 | + onSuccess={onSuccess} |
| 145 | + title="TEMP TITLE" |
| 146 | + config={getConfig()} |
| 147 | + /> |
| 148 | + </> |
| 149 | + ); |
| 150 | +}; |
0 commit comments