34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
import type { DocumentType } from '@/api/ius/types';
|
|
import type { PageUrlParams } from '@/utils/url';
|
|
import type { PropsWithChildren } from 'react';
|
|
import { createContext, useMemo, useState } from 'react';
|
|
|
|
type FormStatus = 'pending' | 'edit' | 'success' | 'error';
|
|
type FormState = { status: FormStatus; text?: string };
|
|
|
|
type FormFile = DocumentType & { file: File };
|
|
|
|
type ContextType = {
|
|
readonly formFiles: FormFile[];
|
|
readonly formState: FormState;
|
|
readonly pageUrlParams: PageUrlParams;
|
|
readonly setFormFiles: (files: FormFile[]) => void;
|
|
readonly setFormState: (formState: FormState) => void;
|
|
};
|
|
|
|
export const FormContext = createContext<ContextType>({} as ContextType);
|
|
|
|
export function FormContextProvider({
|
|
children,
|
|
...initialData
|
|
}: PropsWithChildren & Pick<ContextType, 'pageUrlParams'>) {
|
|
const [formState, setFormState] = useState<FormState>({ status: 'edit' });
|
|
const [formFiles, setFormFiles] = useState<FormFile[]>([]);
|
|
const value = useMemo(
|
|
() => ({ ...initialData, formFiles, formState, setFormFiles, setFormState }),
|
|
[formFiles, formState, initialData]
|
|
);
|
|
|
|
return <FormContext.Provider value={value}>{children}</FormContext.Provider>;
|
|
}
|