From 081223e2598294e838d230e8829010af4c791e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3n=20Bjarni=20=C3=93lafsson?= Date: Thu, 5 Dec 2024 14:10:45 +0000 Subject: [PATCH 1/2] Updated data schema to make less api calls, fixed bug when user selected a previous advert the inputs fields were not updating. Switched out controller components since we have our own. --- .../components/input/OJOIInputController.tsx | 5 +- .../components/input/OJOISelectController.tsx | 68 ++++++---- .../src/fields/Advert.tsx | 96 +++++++------- .../src/fields/AdvertModal.tsx | 122 ++++++------------ .../src/fields/Preview.tsx | 11 +- .../src/fields/Publishing.tsx | 58 ++++----- .../src/fields/Summary.tsx | 38 ++---- .../src/hooks/useTypes.ts | 38 ++++-- .../src/lib/OJOIApplication.ts | 2 +- .../src/lib/dataSchema.ts | 43 +++--- .../src/lib/types.ts | 9 +- .../src/lib/utils.ts | 6 + .../src/screens/AdvertScreen.tsx | 15 +-- 13 files changed, 234 insertions(+), 277 deletions(-) diff --git a/libs/application/templates/official-journal-of-iceland/src/components/input/OJOIInputController.tsx b/libs/application/templates/official-journal-of-iceland/src/components/input/OJOIInputController.tsx index 44bee990cc2e..ce66ffa0bc15 100644 --- a/libs/application/templates/official-journal-of-iceland/src/components/input/OJOIInputController.tsx +++ b/libs/application/templates/official-journal-of-iceland/src/components/input/OJOIInputController.tsx @@ -1,6 +1,5 @@ -import { SkeletonLoader } from '@island.is/island-ui/core' +import { Input, SkeletonLoader } from '@island.is/island-ui/core' import { useLocale } from '@island.is/localization' -import { InputController } from '@island.is/shared/form-fields' import { MessageDescriptor } from 'react-intl' import { OJOI_INPUT_HEIGHT } from '../../lib/constants' import { useApplication } from '../../hooks/useUpdateApplication' @@ -60,7 +59,7 @@ export const OJOIInputController = ({ } return ( - = { label: string - value: string + value: T } -type Props = { +type Props = { name: string label: string | MessageDescriptor placeholder: string | MessageDescriptor - options?: OJOISelectControllerOption[] - defaultValue?: string + options?: SelectOption[] + defaultValue?: T loading?: boolean applicationId: string disabled?: boolean - onChange?: (label: string, value: string) => void + onBeforeChange?: (answers: OJOIApplication['answers'], value: T) => void + onChange?: (value: T) => void } -export const OJOISelectController = ({ +export const OJOISelectController = ({ name, label, placeholder, @@ -33,30 +36,41 @@ export const OJOISelectController = ({ loading, applicationId, disabled, + onBeforeChange, onChange, -}: Props) => { +}: Props) => { const { formatMessage: f } = useLocale() - const { updateApplication, application } = useApplication({ applicationId }) + const { updateApplication, application } = useApplication({ + applicationId, + }) + + const { setValue } = useFormContext() const placeholderText = typeof placeholder === 'string' ? placeholder : f(placeholder) const labelText = typeof label === 'string' ? label : f(label) - const handleChange = (label: string, value: string) => { + const handleChange = (value: T) => { const currentAnswers = structuredClone(application.answers) const newAnswers = set(currentAnswers, name, value) + onBeforeChange && onBeforeChange(newAnswers, value) - // we must reset the selected typeId if the department changes - if (name === InputFields.advert.departmentId) { - set(newAnswers, InputFields.advert.typeId, '') - } - + setValue(name, value) updateApplication(newAnswers) - onChange && onChange(label, value) + onChange && onChange(value) } + const defaultVal = getValueViaPath(application.answers, name, defaultValue) + const defaultOpt = options?.find((opt) => { + if (isBaseEntity(opt.value) && isBaseEntity(defaultVal)) { + return opt.value.id === defaultVal.id + } + + return undefined + }) + if (loading) { return ( handleChange(opt.label, opt.value)} + defaultValue={defaultOpt} + onChange={(opt) => { + if (!opt?.value) return + return handleChange(opt.value) + }} /> ) } diff --git a/libs/application/templates/official-journal-of-iceland/src/fields/Advert.tsx b/libs/application/templates/official-journal-of-iceland/src/fields/Advert.tsx index fe9cc36440ae..9cb1b1371782 100644 --- a/libs/application/templates/official-journal-of-iceland/src/fields/Advert.tsx +++ b/libs/application/templates/official-journal-of-iceland/src/fields/Advert.tsx @@ -1,4 +1,3 @@ -import { useCallback } from 'react' import { InputFields, OJOIFieldBaseProps } from '../lib/types' import { Box } from '@island.is/island-ui/core' import { FormGroup } from '../components/form/FormGroup' @@ -15,86 +14,80 @@ import set from 'lodash/set' import { HTMLEditor } from '../components/htmlEditor/HTMLEditor' import { getAdvertMarkup } from '../lib/utils' -type Props = OJOIFieldBaseProps & { - timeStamp: string -} - -export const Advert = ({ application, timeStamp }: Props) => { +export const Advert = ({ application }: OJOIFieldBaseProps) => { const { setValue } = useFormContext() - const { application: currentApplication, updateApplication } = useApplication( - { - applicationId: application.id, - }, - ) + const { application: currentApplication } = useApplication({ + applicationId: application.id, + }) const { departments, loading: loadingDepartments } = useDepartments() const { - useLazyTypes, + getLazyTypes, types, loading: loadingTypes, } = useTypes({ - initalDepartmentId: application.answers?.advert?.departmentId, + initalDepartmentId: application.answers?.advert?.department?.id, }) - const handleDepartmentChange = useCallback( - (value: string) => { - // eslint-disable-next-line react-hooks/rules-of-hooks - useLazyTypes({ - params: { - department: value, - pageSize: 100, - }, - }) - }, - [useLazyTypes], - ) - - const updateTypeHandler = (name: string, id: string) => { - let currentAnswers = structuredClone(currentApplication.answers) - currentAnswers = set(currentAnswers, InputFields.advert.typeName, name) - - currentAnswers = set(currentAnswers, InputFields.advert.typeId, id) - - updateApplication(currentAnswers) - } - const titlePreview = getAdvertMarkup({ - type: currentApplication.answers.advert?.typeName, + type: currentApplication.answers.advert?.type?.title, title: currentApplication.answers.advert?.title, }) + const departmentOptions = departments?.map((d) => ({ + label: d.title, + value: { + id: d.id, + title: d.title, + slug: d.slug, + }, + })) + + const typeOptions = types?.map((d) => ({ + label: d.title, + value: { + id: d.id, + title: d.title, + slug: d.slug, + }, + })) + return ( <> ({ - label: d.title, - value: d.id, - }))} - onChange={(_, value) => handleDepartmentChange(value)} + options={departmentOptions} + defaultValue={application.answers?.advert?.department} + onBeforeChange={(answers) => { + setValue(InputFields.advert.type, null) + set(answers, InputFields.advert.type, null) + }} + onChange={(value) => + getLazyTypes({ + variables: { + params: { + department: value.id, + pageSize: 100, + }, + }, + }) + } /> ({ - label: d.title, - value: d.id, - }))} - onChange={(label, value) => { - updateTypeHandler(label, value) - }} + options={typeOptions} /> @@ -132,7 +125,6 @@ export const Advert = ({ application, timeStamp }: Props) => { applicationId={application.id} name={InputFields.advert.html} defaultValue={currentApplication.answers?.advert?.html} - editorKey={timeStamp} // we have use setValue from useFormContext to update the value // because this is not a controlled component onChange={(value) => setValue(InputFields.advert.html, value)} diff --git a/libs/application/templates/official-journal-of-iceland/src/fields/AdvertModal.tsx b/libs/application/templates/official-journal-of-iceland/src/fields/AdvertModal.tsx index ba50ca2999dc..8327cefa5e93 100644 --- a/libs/application/templates/official-journal-of-iceland/src/fields/AdvertModal.tsx +++ b/libs/application/templates/official-journal-of-iceland/src/fields/AdvertModal.tsx @@ -1,5 +1,4 @@ import { - AlertMessage, Box, Button, Icon, @@ -23,11 +22,10 @@ import { DEFAULT_PAGE_SIZE, OJOI_INPUT_HEIGHT, } from '../lib/constants' -import { useAdvert } from '../hooks/useAdvert' import debounce from 'lodash/debounce' -import set from 'lodash/set' import { InputFields } from '../lib/types' import { useFormContext } from 'react-hook-form' +import { OfficialJournalOfIcelandAdvert } from '@island.is/api/schema' type Props = { applicationId: string visible: boolean @@ -35,55 +33,30 @@ type Props = { onConfirmChange?: () => void } -type UpdateAdvertFields = { - title: string - departmentId: string - typeId: string - html: string - categories: string[] -} - export const AdvertModal = ({ applicationId, visible, setVisible, onConfirmChange, }: Props) => { - const [page, setPage] = useState(DEFAULT_PAGE) - const [search, setSearch] = useState('') - const [selectedAdvertId, setSelectedAdvertId] = useState(null) - const { formatMessage: f } = useLocale() const { setValue } = useFormContext() const { application, updateApplication } = useApplication({ applicationId, }) + const [page, setPage] = useState(DEFAULT_PAGE) + const [search, setSearch] = useState('') + const [selectedAdvert, setSelectedAdvert] = + useState(null) + const { adverts, paging, loading } = useAdverts({ page: page, search: search, }) - const [updateAdvertFields, setUpdateAdvertFields] = - useState(null) - - const { loading: loadingAdvert, error: advertError } = useAdvert({ - advertId: selectedAdvertId, - onCompleted: (ad) => { - setUpdateAdvertFields({ - title: ad.title, - departmentId: ad.department.id, - typeId: ad.type.id, - html: ad.document.html, - categories: ad.categories.map((c) => c.id), - }) - }, - }) - - const disableConfirmButton = !selectedAdvertId || !!advertError - - const onSelectAdvert = (advertId: string) => { - setSelectedAdvertId(advertId) + const onSelectAdvert = (advert: OfficialJournalOfIcelandAdvert) => { + setSelectedAdvert(advert) } const onSearchChange = (value: string) => { @@ -97,46 +70,41 @@ export const AdvertModal = ({ debouncedSearch(e.target.value) } - const onConfirm = () => { - if (!updateAdvertFields) { + const onConfirm = (advert: OfficialJournalOfIcelandAdvert | null) => { + if (!advert) { return } - const currentAnswers = structuredClone(application.answers) + const clean = (obj: { + __typename?: string + id: string + title: string + slug: string + }) => { + const { __typename: _, ...rest } = obj + return rest + } + + const department = clean(advert.department) + const type = clean(advert.type) - let updatedAnswers = set( - currentAnswers, - InputFields.advert.title, - updateAdvertFields.title, - ) - updatedAnswers = set( - updatedAnswers, - InputFields.advert.departmentId, - updateAdvertFields.departmentId, - ) - updatedAnswers = set( - updatedAnswers, - InputFields.advert.typeId, - updateAdvertFields.typeId, - ) - updatedAnswers = set( - updatedAnswers, - InputFields.advert.html, - updateAdvertFields.html, - ) - updatedAnswers = set( - updatedAnswers, - InputFields.advert.categories, - updateAdvertFields.categories, - ) + const categories = advert.categories.map((category) => clean(category)) - setValue(InputFields.advert.title, updateAdvertFields.title) - setValue(InputFields.advert.departmentId, updateAdvertFields.departmentId) - setValue(InputFields.advert.typeId, updateAdvertFields.typeId) - setValue(InputFields.advert.html, updateAdvertFields.html) - setValue(InputFields.advert.categories, updateAdvertFields.categories) + setValue(InputFields.advert.department, department) + setValue(InputFields.advert.type, type) + setValue(InputFields.advert.title, advert.title) + + updateApplication({ + ...application.answers, + advert: { + department, + type, + categories, + title: advert.title, + html: advert.document.html, + }, + }) - updateApplication(updatedAnswers) onConfirmChange && onConfirmChange() setVisible(false) } @@ -173,13 +141,6 @@ export const AdvertModal = ({ onChange={handleSearchChange} /> - {!!advertError && ( - - )} onSelectAdvert(advert.id)} + checked={selectedAdvert?.id === advert.id} + onChange={() => onSelectAdvert(advert)} /> ))} @@ -233,9 +194,8 @@ export const AdvertModal = ({ {f(general.cancel)} diff --git a/libs/application/templates/official-journal-of-iceland/src/fields/Preview.tsx b/libs/application/templates/official-journal-of-iceland/src/fields/Preview.tsx index 3255b2e091e5..82257eeee648 100644 --- a/libs/application/templates/official-journal-of-iceland/src/fields/Preview.tsx +++ b/libs/application/templates/official-journal-of-iceland/src/fields/Preview.tsx @@ -21,7 +21,6 @@ import { import { Routes, SignatureTypes } from '../lib/constants' import { useApplication } from '../hooks/useUpdateApplication' import { advert, error, preview, signatures } from '../lib/messages' -import { useType } from '../hooks/useType' import { previewValidationSchema, signatureValidationSchema, @@ -39,10 +38,6 @@ export const Preview = ({ application, goToScreen }: OJOIFieldBaseProps) => { const { formatMessage: f } = useLocale() - const { type } = useType({ - typeId: currentApplication.answers.advert?.typeId, - }) - const { fetchPdf, error: pdfError, @@ -60,7 +55,7 @@ export const Preview = ({ application, goToScreen }: OJOIFieldBaseProps) => { const url = URL.createObjectURL(blob) let downloadName - const type = currentApplication.answers.advert?.typeName + const type = currentApplication.answers.advert?.type?.title if (type) { downloadName = type.replace('.', '') } @@ -103,14 +98,14 @@ export const Preview = ({ application, goToScreen }: OJOIFieldBaseProps) => { }) const advertMarkup = getAdvertMarkup({ - type: type?.title, + type: currentApplication.answers.advert?.type?.title, title: currentApplication.answers.advert?.title, html: currentApplication.answers.advert?.html, }) const hasMarkup = !!currentApplication.answers.advert?.html || - type?.title || + currentApplication.answers.advert?.type?.title || currentApplication.answers.advert?.title const combinedHtml = hasMarkup diff --git a/libs/application/templates/official-journal-of-iceland/src/fields/Publishing.tsx b/libs/application/templates/official-journal-of-iceland/src/fields/Publishing.tsx index b2f7d27726a9..2b2b845b4092 100644 --- a/libs/application/templates/official-journal-of-iceland/src/fields/Publishing.tsx +++ b/libs/application/templates/official-journal-of-iceland/src/fields/Publishing.tsx @@ -8,6 +8,7 @@ import { AlertMessage, Box, Icon, + Inline, Select, SkeletonLoader, Tag, @@ -18,6 +19,8 @@ import set from 'lodash/set' import addYears from 'date-fns/addYears' import { addWeekdays, getFastTrack, getWeekendDates } from '../lib/utils' import { useState } from 'react' +import { baseEntitySchema } from '../lib/dataSchema' +import { z } from 'zod' export const Publishing = ({ application }: OJOIFieldBaseProps) => { const { formatMessage: f } = useLocale() @@ -52,7 +55,7 @@ export const Publishing = ({ application }: OJOIFieldBaseProps) => { getFastTrack(new Date(defaultDate)).fastTrack, ) - const onCategoryChange = (value?: string) => { + const onCategoryChange = (value?: z.infer) => { setIsUpdatingCategory(true) if (!value) { setIsUpdatingCategory(false) @@ -62,8 +65,8 @@ export const Publishing = ({ application }: OJOIFieldBaseProps) => { const currentAnswers = structuredClone(currentApplication.answers) const selectedCategories = currentAnswers.advert?.categories || [] - const newCategories = selectedCategories.includes(value) - ? selectedCategories.filter((c) => c !== value) + const newCategories = selectedCategories.find((cat) => cat.id === value.id) + ? selectedCategories.filter((c) => c.id !== value.id) : [...selectedCategories, value] const updatedAnswers = set( @@ -77,19 +80,12 @@ export const Publishing = ({ application }: OJOIFieldBaseProps) => { }) } - const defaultCategory = { - label: f(publishing.inputs.contentCategories.placeholder), - value: '', - } - const mappedCategories = categories?.map((c) => ({ label: c.title, - value: c.id, + value: c, })) - const selectedCategories = categories?.filter((c) => - currentApplication.answers.advert?.categories?.includes(c.id), - ) + const selectedCategories = currentApplication.answers.advert?.categories return ( @@ -124,30 +120,26 @@ export const Publishing = ({ application }: OJOIFieldBaseProps) => { size="sm" label={f(publishing.inputs.contentCategories.label)} backgroundColor="blue" - defaultValue={defaultCategory} options={mappedCategories} + defaultValue={mappedCategories?.[0]} onChange={(opt) => onCategoryChange(opt?.value)} /> - - {selectedCategories?.map((c) => ( - onCategoryChange(c.id)} - outlined - key={c.id} - > - - {c.title} - - - - ))} + + + {selectedCategories?.map((c) => ( + onCategoryChange(c)} + outlined + key={c.id} + > + + {c.title} + + + + ))} + )} diff --git a/libs/application/templates/official-journal-of-iceland/src/fields/Summary.tsx b/libs/application/templates/official-journal-of-iceland/src/fields/Summary.tsx index 946abdbf7d5b..dc1799e2a3ef 100644 --- a/libs/application/templates/official-journal-of-iceland/src/fields/Summary.tsx +++ b/libs/application/templates/official-journal-of-iceland/src/fields/Summary.tsx @@ -14,12 +14,9 @@ import { useUserInfo } from '@island.is/react-spa/bff' import { useEffect } from 'react' import { ZodCustomIssue } from 'zod' import { Property } from '../components/property/Property' -import { useCategories } from '../hooks/useCategories' -import { useDepartment } from '../hooks/useDepartment' import { usePrice } from '../hooks/usePrice' -import { useType } from '../hooks/useType' import { useApplication } from '../hooks/useUpdateApplication' -import { MINIMUM_WEEKDAYS, Routes } from '../lib/constants' +import { Routes } from '../lib/constants' import { advertValidationSchema, publishingValidationSchema, @@ -28,7 +25,7 @@ import { import { advert, error, publishing, summary } from '../lib/messages' import { signatures } from '../lib/messages/signatures' import { OJOIFieldBaseProps } from '../lib/types' -import { addWeekdays, getFastTrack, parseZodIssue } from '../lib/utils' +import { getFastTrack, parseZodIssue } from '../lib/utils' export const Summary = ({ application, @@ -42,26 +39,11 @@ export const Summary = ({ const user = useUserInfo() - const { type, loading: loadingType } = useType({ - typeId: currentApplication.answers.advert?.typeId, - }) - const { price, loading: loadingPrice } = usePrice({ applicationId: application.id, }) - const { department, loading: loadingDepartment } = useDepartment({ - departmentId: currentApplication.answers.advert?.departmentId, - }) - - const { categories, loading: loadingCategories } = useCategories() - - const selectedCategories = categories?.filter((c) => - currentApplication.answers?.advert?.categories?.includes(c.id), - ) - - const today = new Date() - const estimatedDate = addWeekdays(today, MINIMUM_WEEKDAYS) + const selectedCategories = application.answers?.advert?.categories const advertValidationCheck = advertValidationSchema.safeParse( currentApplication.answers, @@ -86,6 +68,10 @@ export const Summary = ({ } else { setSubmitButtonDisabled && setSubmitButtonDisabled(true) } + + return () => { + setSubmitButtonDisabled && setSubmitButtonDisabled(false) + } }, [ advertValidationCheck, signatureValidationCheck, @@ -227,18 +213,16 @@ export const Summary = ({ value={user.profile.name} /> c.title).join(', ')} /> @@ -289,7 +272,6 @@ export const Summary = ({ } /> diff --git a/libs/application/templates/official-journal-of-iceland/src/hooks/useTypes.ts b/libs/application/templates/official-journal-of-iceland/src/hooks/useTypes.ts index ca15965c5c74..8cd4ff5678ed 100644 --- a/libs/application/templates/official-journal-of-iceland/src/hooks/useTypes.ts +++ b/libs/application/templates/official-journal-of-iceland/src/hooks/useTypes.ts @@ -1,4 +1,4 @@ -import { NetworkStatus, useQuery } from '@apollo/client' +import { useLazyQuery, useQuery } from '@apollo/client' import { OfficialJournalOfIcelandAdvertsTypesResponse } from '@island.is/api/schema' import { TYPES_QUERY } from '../graphql/queries' @@ -40,21 +40,35 @@ export const useTypes = ({ params.pageSize = 1000 } - const { data, loading, error, refetch, networkStatus } = useQuery< - TypesResponse, - TypesVariables - >(TYPES_QUERY, { - variables: { - params: params, + const { data, loading, error } = useQuery( + TYPES_QUERY, + { + variables: { + params: params, + }, + onCompleted: onCompleted, }, - notifyOnNetworkStatusChange: true, - onCompleted: onCompleted, + ) + + const [ + getLazyTypes, + { data: lazyTypes, loading: lazyTypesLoading, error: lazyTypesError }, + ] = useLazyQuery(TYPES_QUERY, { + fetchPolicy: 'network-only', }) + const currentTypes = lazyTypes + ? lazyTypes.officialJournalOfIcelandTypes.types + : data?.officialJournalOfIcelandTypes.types + return { - useLazyTypes: refetch, - types: data?.officialJournalOfIcelandTypes.types, - loading: loading || networkStatus === NetworkStatus.refetch, + lazyTypes: lazyTypes?.officialJournalOfIcelandTypes.types, + lazyTypesLoading, + lazyTypesError, + getLazyTypes, + types: currentTypes, + initalTypes: data?.officialJournalOfIcelandTypes.types, + loading: loading || lazyTypesLoading, error, } } diff --git a/libs/application/templates/official-journal-of-iceland/src/lib/OJOIApplication.ts b/libs/application/templates/official-journal-of-iceland/src/lib/OJOIApplication.ts index 3e7cc7fc9289..5baf45b63a8d 100644 --- a/libs/application/templates/official-journal-of-iceland/src/lib/OJOIApplication.ts +++ b/libs/application/templates/official-journal-of-iceland/src/lib/OJOIApplication.ts @@ -41,7 +41,7 @@ const getApplicationName = (application: Application) => { const type = getValueViaPath( application.answers, - InputFields.advert.typeName, + `${InputFields.advert.type}.title`, '', ) diff --git a/libs/application/templates/official-journal-of-iceland/src/lib/dataSchema.ts b/libs/application/templates/official-journal-of-iceland/src/lib/dataSchema.ts index f617921467b5..fed28c133461 100644 --- a/libs/application/templates/official-journal-of-iceland/src/lib/dataSchema.ts +++ b/libs/application/templates/official-journal-of-iceland/src/lib/dataSchema.ts @@ -43,6 +43,12 @@ export const regularSignatureSchema = z .array(regularSignatureItemSchema) .optional() +export const baseEntitySchema = z.object({ + id: z.string(), + title: z.string(), + slug: z.string(), +}) + export const signatureInstitutionSchema = z.enum(['institution', 'date']) export const committeeSignatureSchema = regularSignatureItemSchema @@ -61,13 +67,12 @@ export const channelSchema = z const advertSchema = z .object({ - departmentId: z.string().optional(), - typeName: z.string().optional(), - typeId: z.string().optional(), + department: baseEntitySchema.optional(), + type: baseEntitySchema.optional().nullable(), title: z.string().optional(), html: z.string().optional(), requestedDate: z.string().optional(), - categories: z.array(z.string()).optional(), + categories: z.array(baseEntitySchema).optional(), channels: z.array(channelSchema).optional(), message: z.string().optional(), additions: additionSchema.optional(), @@ -108,16 +113,16 @@ export const partialSchema = z.object({ // We make properties optional to throw custom error messages export const advertValidationSchema = z.object({ advert: z.object({ - departmentId: z - .string() + department: baseEntitySchema .optional() - .refine((value) => value && value.length > 0, { + .nullable() + .refine((value) => value !== null && value !== undefined, { params: error.missingDepartment, }), - typeId: z - .string() + type: baseEntitySchema .optional() - .refine((value) => value && value.length > 0, { + .nullable() + .refine((value) => value !== null && value !== undefined, { params: error.missingType, }), title: z @@ -137,17 +142,17 @@ export const advertValidationSchema = z.object({ export const previewValidationSchema = z.object({ advert: z.object({ - departmentId: z - .string() + department: baseEntitySchema .optional() - .refine((value) => value && value.length > 0, { - params: error.missingPreviewDepartment, + .nullable() + .refine((value) => value !== null && value !== undefined, { + params: error.missingDepartment, }), - typeId: z - .string() + type: baseEntitySchema .optional() - .refine((value) => value && value.length > 0, { - params: error.missingPreviewType, + .nullable() + .refine((value) => value !== null && value !== undefined, { + params: error.missingType, }), title: z .string() @@ -173,7 +178,7 @@ export const publishingValidationSchema = z.object({ params: error.missingRequestedDate, }), categories: z - .array(z.string()) + .array(baseEntitySchema) .optional() .refine((value) => Array.isArray(value) && value.length > 0, { params: error.noCategorySelected, diff --git a/libs/application/templates/official-journal-of-iceland/src/lib/types.ts b/libs/application/templates/official-journal-of-iceland/src/lib/types.ts index ccae26abdb38..385aec190a50 100644 --- a/libs/application/templates/official-journal-of-iceland/src/lib/types.ts +++ b/libs/application/templates/official-journal-of-iceland/src/lib/types.ts @@ -11,9 +11,8 @@ export const InputFields = { approveExternalData: 'requirements.approveExternalData', }, [Routes.ADVERT]: { - departmentId: 'advert.departmentId', - typeName: 'advert.typeName', - typeId: 'advert.typeId', + department: 'advert.department', + type: 'advert.type', title: 'advert.title', html: 'advert.html', requestedDate: 'advert.requestedDate', @@ -39,8 +38,8 @@ export const InputFields = { export const RequiredInputFieldsNames = { [Routes.ADVERT]: { - departmentId: 'Deild', - typeId: 'Tegund', + department: 'Deild', + type: 'Tegund', title: 'Titill', html: 'Auglýsing', requestedDate: 'Útgáfudagur', diff --git a/libs/application/templates/official-journal-of-iceland/src/lib/utils.ts b/libs/application/templates/official-journal-of-iceland/src/lib/utils.ts index 2e6f17b53156..96d1b2fb0893 100644 --- a/libs/application/templates/official-journal-of-iceland/src/lib/utils.ts +++ b/libs/application/templates/official-journal-of-iceland/src/lib/utils.ts @@ -3,6 +3,7 @@ import addYears from 'date-fns/addYears' import { z } from 'zod' import { additionSchema, + baseEntitySchema, committeeSignatureSchema, memberItemSchema, partialSchema, @@ -130,6 +131,11 @@ export const getSignatureDefaultValues = (signature: any, index?: number) => { return { institution: signature.institution, date: signature.date } } +export const isBaseEntity = ( + entity: unknown, +): entity is z.infer => + baseEntitySchema.safeParse(entity).success + export const isAddition = ( addition: unknown, ): addition is z.infer => diff --git a/libs/application/templates/official-journal-of-iceland/src/screens/AdvertScreen.tsx b/libs/application/templates/official-journal-of-iceland/src/screens/AdvertScreen.tsx index 56a115f61241..92d9a3911d77 100644 --- a/libs/application/templates/official-journal-of-iceland/src/screens/AdvertScreen.tsx +++ b/libs/application/templates/official-journal-of-iceland/src/screens/AdvertScreen.tsx @@ -11,13 +11,6 @@ export const AdvertScreen = (props: OJOIFieldBaseProps) => { const { formatMessage: f } = useLocale() const [modalVisible, setModalVisability] = useState(false) - const generateTimestamp = () => new Date().toISOString() - - /** - * This state here is for force rerendering of the HTML editor when a value is received from the modal - */ - const [timestamp, setTimestamp] = useState(generateTimestamp()) - return ( { } > - + setTimestamp(generateTimestamp())} + onConfirmChange={() => { + setTimeout(() => { + props.refetch && props.refetch() + }, 300) + }} /> ) From 3bda5cf202aaffcd462d876beb7b4ebd9c11eb7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3n=20Bjarni=20=C3=93lafsson?= Date: Thu, 5 Dec 2024 15:05:27 +0000 Subject: [PATCH 2/2] Returning false instead of undefined. --- .../src/components/input/OJOISelectController.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/application/templates/official-journal-of-iceland/src/components/input/OJOISelectController.tsx b/libs/application/templates/official-journal-of-iceland/src/components/input/OJOISelectController.tsx index c022017709cb..a16aa3cc38ad 100644 --- a/libs/application/templates/official-journal-of-iceland/src/components/input/OJOISelectController.tsx +++ b/libs/application/templates/official-journal-of-iceland/src/components/input/OJOISelectController.tsx @@ -68,7 +68,7 @@ export const OJOISelectController = ({ return opt.value.id === defaultVal.id } - return undefined + return false }) if (loading) {