Create print
This commit is contained in:
@@ -14,7 +14,7 @@ const UpdateSection: FC = () => {
|
||||
|
||||
const { id } = useParams()
|
||||
const navigate = useNavigate()
|
||||
const { data } = useGetSectionsDetail(+id!)
|
||||
const { data } = useGetSectionsDetail(id!)
|
||||
const { isPending, mutate } = useUpdateSection()
|
||||
|
||||
const formik = useFormik<CreateSectionType>({
|
||||
@@ -26,7 +26,7 @@ const UpdateSection: FC = () => {
|
||||
title: Yup.string().required('این فیلد اجباری می باشد.')
|
||||
}),
|
||||
onSubmit: (values) => {
|
||||
mutate({ id: +id!, params: values }, {
|
||||
mutate({ id: id!, params: values }, {
|
||||
onSuccess: () => {
|
||||
toast('با موفقیت ثبت شد', 'success')
|
||||
navigate(-1)
|
||||
|
||||
@@ -10,25 +10,29 @@ import type { FormikProps } from 'formik'
|
||||
import type { CreatePrintFormType } from '../types/Types'
|
||||
import type { EntityType } from '@/pages/formBuilder/types/Types'
|
||||
import { FieldTypeEnum } from '@/pages/formBuilder/enum/Enum'
|
||||
import type { OrderPrintFormType } from '@/pages/order/types/Types'
|
||||
|
||||
type Props = {
|
||||
attributes?: EntityType[],
|
||||
formik: FormikProps<CreatePrintFormType>
|
||||
attributes?: EntityType[]
|
||||
formik: FormikProps<CreatePrintFormType> | FormikProps<OrderPrintFormType>
|
||||
formFieldKey?: keyof CreatePrintFormType | keyof OrderPrintFormType
|
||||
}
|
||||
|
||||
const ManageForms: FC<Props> = (props) => {
|
||||
|
||||
const { attributes, formik } = props
|
||||
const { attributes, formik, formFieldKey = 'printAttributes' } = props
|
||||
|
||||
const fields = ((formik.values as Record<string, { fieldId: string; value: string }[]>)[formFieldKey] ?? [])
|
||||
|
||||
const handleChange = (attributeId: string, value: string) => {
|
||||
const attribute = formik.values.printAttributes
|
||||
const attribute = [...fields]
|
||||
const index: number = attribute.findIndex(o => o.fieldId === attributeId)
|
||||
if (index > -1) {
|
||||
attribute[index].value = value
|
||||
} else {
|
||||
attribute.push({ fieldId: attributeId, value: value })
|
||||
}
|
||||
formik.setFieldValue('printAttributes', attribute)
|
||||
formik.setFieldValue(formFieldKey as string, attribute)
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +43,7 @@ const ManageForms: FC<Props> = (props) => {
|
||||
)}>
|
||||
{
|
||||
attributes?.map((item) => {
|
||||
const value = formik.values.printAttributes?.find(o => o.fieldId === item.id)
|
||||
const value = fields.find(o => o.fieldId === item.id)
|
||||
if (item?.type === FieldTypeEnum.select)
|
||||
return (
|
||||
<div key={item.id}>
|
||||
|
||||
@@ -9,7 +9,7 @@ export const useGetSections = () => {
|
||||
});
|
||||
};
|
||||
|
||||
export const useGetSectionsDetail = (id: number) => {
|
||||
export const useGetSectionsDetail = (id: string) => {
|
||||
return useQuery({
|
||||
queryKey: ["section", id],
|
||||
queryFn: () => api.getSectionDetail(id),
|
||||
@@ -31,7 +31,7 @@ export const useCreateSection = () => {
|
||||
export const useUpdateSection = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ id, params }: { id: number; params: CreateSectionType }) =>
|
||||
mutationFn: ({ id, params }: { id: string; params: CreateSectionType }) =>
|
||||
api.updateSection(id, params),
|
||||
onSuccess: (_, params) => {
|
||||
queryClient.refetchQueries({
|
||||
@@ -47,7 +47,7 @@ export const useUpdateSection = () => {
|
||||
export const useDeleteSection = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => api.deleteSection(id),
|
||||
mutationFn: (id: string) => api.deleteSection(id),
|
||||
onSuccess: () => {
|
||||
queryClient.refetchQueries({
|
||||
queryKey: ["sections"],
|
||||
|
||||
@@ -16,19 +16,19 @@ export const createSection = async (params: CreateSectionType) => {
|
||||
return data;
|
||||
};
|
||||
|
||||
export const updateSection = async (id: number, params: CreateSectionType) => {
|
||||
export const updateSection = async (id: string, params: CreateSectionType) => {
|
||||
const { data } = await axios.patch(`/admin/section/${id}`, params);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getSectionDetail = async (id: number) => {
|
||||
export const getSectionDetail = async (id: string) => {
|
||||
const { data } = await axios.get<SectionDetailResponseType>(
|
||||
`/admin/section/${id}`,
|
||||
);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const deleteSection = async (id: number) => {
|
||||
export const deleteSection = async (id: string) => {
|
||||
const { data } = await axios.delete(`/admin/section/${id}`);
|
||||
return data;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user