From 6b554bb72366fca44b3f471b3a2583f019227336 Mon Sep 17 00:00:00 2001 From: Michael Schramm Date: Tue, 2 Jun 2020 12:25:11 +0200 Subject: [PATCH] fix #6 Date min and Max fields, with general support of Object keys in admin form fields --- components/form/admin/types/date.type.tsx | 7 ++- components/form/types/date.type.tsx | 27 +++++++++-- graphql/fragment/admin.form.fragment.ts | 7 ++- graphql/fragment/form.fragment.ts | 2 +- pages/admin/forms/[id]/index.tsx | 55 +++++++++++++++++++---- 5 files changed, 81 insertions(+), 17 deletions(-) diff --git a/components/form/admin/types/date.type.tsx b/components/form/admin/types/date.type.tsx index 4740f96..3c9b91a 100644 --- a/components/form/admin/types/date.type.tsx +++ b/components/form/admin/types/date.type.tsx @@ -17,26 +17,25 @@ export const DateType: React.FC = ({field, form}) => { format={'YYYY-MM-DD'} /> - {/* TODO add options e.format('YYYY-MM-DD')} getValueProps={e => ({value: e ? moment(e) : undefined})} > + e.format('YYYY-MM-DD')} getValueProps={e => ({value: e ? moment(e) : undefined})} > - */} ) } diff --git a/components/form/types/date.type.tsx b/components/form/types/date.type.tsx index 21207ec..61131a6 100644 --- a/components/form/types/date.type.tsx +++ b/components/form/types/date.type.tsx @@ -1,12 +1,24 @@ import {Form} from 'antd' +import dayjs, {Dayjs} from 'dayjs' import moment from 'moment' -import React from 'react' +import React, {useEffect, useState} from 'react' import {StyledDateInput} from '../../styled/date.input' import {FieldTypeProps} from './type.props' export const DateType: React.FC = ({ field, design}) => { - // TODO check min and max - // TODO if default is passed, then the changing should not be required + const [min, setMin] = useState() + const [max, setMax] = useState() + + useEffect(() => { + field.options.forEach(option => { + if (option.key === 'min') { + setMin(dayjs(option.value)) + } + if (option.key === 'max') { + setMax(dayjs(option.value)) + } + }) + }, [field]) return (
@@ -23,6 +35,15 @@ export const DateType: React.FC = ({ field, design}) => { size={'large'} design={design} autoFocus + disabledDate={(d: any) => { + if (min && min.isAfter(d)) { + return true + } + if (max && max.isBefore(d)) { + return true + } + return false + }} />
diff --git a/graphql/fragment/admin.form.fragment.ts b/graphql/fragment/admin.form.fragment.ts index 97f657e..2b1c2c6 100644 --- a/graphql/fragment/admin.form.fragment.ts +++ b/graphql/fragment/admin.form.fragment.ts @@ -15,11 +15,15 @@ export interface AdminFormPageFragment { } export interface AdminFormFieldOptionFragment { - key?: number + key?: string title?: string value: string } +export interface AdminFormFieldOptionKeysFragment { + [key: string]: string +} + export interface AdminFormFieldLogicJumpFragment { fieldA?: string valueB?: string @@ -37,6 +41,7 @@ export interface AdminFormFieldFragment { value: string options: AdminFormFieldOptionFragment[] + optionKeys?: AdminFormFieldOptionKeysFragment logicJump: AdminFormFieldLogicJumpFragment diff --git a/graphql/fragment/form.fragment.ts b/graphql/fragment/form.fragment.ts index 03dd46a..21eca0d 100644 --- a/graphql/fragment/form.fragment.ts +++ b/graphql/fragment/form.fragment.ts @@ -16,7 +16,7 @@ export interface FormPageFragment { } export interface FormFieldOptionFragment { - key?: number + key?: string title?: string value: string } diff --git a/pages/admin/forms/[id]/index.tsx b/pages/admin/forms/[id]/index.tsx index 335d0d2..123a7b3 100644 --- a/pages/admin/forms/[id]/index.tsx +++ b/pages/admin/forms/[id]/index.tsx @@ -11,7 +11,11 @@ import {SelfNotificationsTab} from 'components/form/admin/self.notifications.tab import {StartPageTab} from 'components/form/admin/start.page.tab' import Structure from 'components/structure' import {withAuth} from 'components/with.auth' -import {AdminFormFieldFragment} from 'graphql/fragment/admin.form.fragment' +import { + AdminFormFieldFragment, + AdminFormFieldOptionFragment, + AdminFormFieldOptionKeysFragment +} from 'graphql/fragment/admin.form.fragment' import { ADMIN_FORM_UPDATE_MUTATION, AdminFormUpdateMutationData, @@ -32,11 +36,29 @@ const Index: NextPage = () => { const [fields, setFields] = useState([]) const [update] = useMutation(ADMIN_FORM_UPDATE_MUTATION) + const processNext = (next: AdminFormQueryData): AdminFormQueryData => { + next.form.fields = next.form.fields.map(field => { + const keys: AdminFormFieldOptionKeysFragment = {} + + field.options.forEach(option => { + if (option.key) { + keys[option.key] = option.value + } + }) + + field.optionKeys = keys + return field + }) + + return next + } + const {data, loading, error} = useQuery(ADMIN_FORM_QUERY, { variables: { id: router.query.id as string }, onCompleted: next => { + next = processNext(next) form.setFieldsValue(next) setFields(next.form.fields) } @@ -45,12 +67,27 @@ const Index: NextPage = () => { const save = async (formData: AdminFormQueryData) => { setSaving(true) - formData.form.fields = formData.form.fields.filter(e => e && e.type) + formData.form.fields = formData.form.fields.filter(e => e && e.type).map(({optionKeys, ...field}) => { + if (optionKeys) { + // + return { + ...field, + options: Object.keys(optionKeys).map((key): AdminFormFieldOptionFragment => { + return { + value: optionKeys[key], + key, + } + }).filter(e => !!e.value) + } + } + + return field + }) try { - const next = (await update({ + const next = processNext((await update({ variables: cleanInput(formData), - })).data + })).data) form.setFieldsValue(next) setFields(next.form.fields) @@ -76,10 +113,12 @@ const Index: NextPage = () => { { href: '/admin/forms', name: t('admin:forms') }, ]} extra={[ - - ,