edit print form
This commit is contained in:
@@ -64,8 +64,16 @@ export const useGetProducts = () => {
|
|||||||
|
|
||||||
export const useGetAttributes = (id?: number) => {
|
export const useGetAttributes = (id?: number) => {
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: ["attributes"],
|
queryKey: ["attributes", id],
|
||||||
queryFn: () => api.getAttributes(id),
|
queryFn: () => api.getAttributes(id),
|
||||||
enabled: !!id,
|
enabled: !!id,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useGetOrderItem = (id?: number) => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ["order-item", id],
|
||||||
|
queryFn: () => api.getOrderItem(id),
|
||||||
|
enabled: !!id,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import type {
|
|||||||
AddTicketType,
|
AddTicketType,
|
||||||
CreateFinalOrderType,
|
CreateFinalOrderType,
|
||||||
OrderDetailResponseType,
|
OrderDetailResponseType,
|
||||||
|
OrderItemResponseType,
|
||||||
OrderResponseType,
|
OrderResponseType,
|
||||||
TicketsResponseType,
|
TicketsResponseType,
|
||||||
} from "../types/Types";
|
} from "../types/Types";
|
||||||
@@ -62,3 +63,10 @@ export const getAttributes = async (productId?: number) => {
|
|||||||
);
|
);
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getOrderItem = async (itemId?: number) => {
|
||||||
|
const { data } = await axios.get<OrderItemResponseType>(
|
||||||
|
`/admin/orders/item/${itemId}`
|
||||||
|
);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|||||||
@@ -40,7 +40,10 @@ export interface OrderItem {
|
|||||||
adminDescription: string | null;
|
adminDescription: string | null;
|
||||||
attachments: string[];
|
attachments: string[];
|
||||||
printAttachments: null;
|
printAttachments: null;
|
||||||
printAttributes: null;
|
printAttributes?: {
|
||||||
|
value: string;
|
||||||
|
fieldId: number;
|
||||||
|
}[];
|
||||||
confirmedAt: string | null;
|
confirmedAt: string | null;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
deletedAt: string | null;
|
deletedAt: string | null;
|
||||||
@@ -48,6 +51,8 @@ export interface OrderItem {
|
|||||||
total: string;
|
total: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type OrderItemResponseType = BaseResponse<OrderItem>;
|
||||||
|
|
||||||
export interface Product {
|
export interface Product {
|
||||||
id: string;
|
id: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { type FC } from 'react'
|
import { useEffect, type FC } from 'react'
|
||||||
import { useNavigate, useParams } from 'react-router-dom'
|
import { useNavigate, useParams } from 'react-router-dom'
|
||||||
import { useCreatePrintForm, useGetSections } from './hooks/usePrintData'
|
import { useCreatePrintForm, useGetSections } from './hooks/usePrintData'
|
||||||
import { useFormik } from 'formik'
|
import { useFormik } from 'formik'
|
||||||
@@ -8,14 +8,17 @@ import { toast } from '@/shared/toast'
|
|||||||
import { extractErrorMessage } from '@/config/func'
|
import { extractErrorMessage } from '@/config/func'
|
||||||
import Button from '@/components/Button'
|
import Button from '@/components/Button'
|
||||||
import { TickSquare } from 'iconsax-react'
|
import { TickSquare } from 'iconsax-react'
|
||||||
|
import { useGetOrderItem } from '../order/hooks/useOrderData'
|
||||||
|
|
||||||
const PrintForm: FC = () => {
|
const PrintForm: FC = () => {
|
||||||
|
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const { itemId, orderId } = useParams()
|
const { itemId, orderId } = useParams()
|
||||||
|
const { data: item } = useGetOrderItem(+itemId!)
|
||||||
const { isPending, mutate } = useCreatePrintForm()
|
const { isPending, mutate } = useCreatePrintForm()
|
||||||
const { data } = useGetSections()
|
const { data } = useGetSections()
|
||||||
|
|
||||||
|
|
||||||
const formik = useFormik<CreatePrintFormType>({
|
const formik = useFormik<CreatePrintFormType>({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
printAttributes: []
|
printAttributes: []
|
||||||
@@ -33,6 +36,17 @@ const PrintForm: FC = () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
|
||||||
|
if (item?.data && item?.data?.printAttributes?.length) {
|
||||||
|
formik.setValues({
|
||||||
|
printAttributes: item?.data?.printAttributes
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [item?.data])
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='mt-5'>
|
<div className='mt-5'>
|
||||||
|
|||||||
@@ -34,11 +34,12 @@ const ManageForms: FC<Props> = (props) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={clx(
|
<div className={clx(
|
||||||
'grid grid-cols-1 md:grid-cols-2 gap-6',
|
'grid grid-cols-1 md:grid-cols-2 gap-6 mt-4',
|
||||||
!attributes && 'hidden'
|
!attributes && 'hidden'
|
||||||
)}>
|
)}>
|
||||||
{
|
{
|
||||||
attributes?.map((item) => {
|
attributes?.map((item) => {
|
||||||
|
const value = formik.values.printAttributes?.find(o => o.fieldId === item.id)
|
||||||
if (item?.type === FieldTypeEnum.select)
|
if (item?.type === FieldTypeEnum.select)
|
||||||
return (
|
return (
|
||||||
<div key={item.id}>
|
<div key={item.id}>
|
||||||
@@ -52,6 +53,7 @@ const ManageForms: FC<Props> = (props) => {
|
|||||||
}
|
}
|
||||||
})}
|
})}
|
||||||
onChange={(e) => handleChange(item.id, e.target.value)}
|
onChange={(e) => handleChange(item.id, e.target.value)}
|
||||||
|
value={value?.value}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
@@ -62,13 +64,12 @@ const ManageForms: FC<Props> = (props) => {
|
|||||||
<div className='flex gap-5 flex-wrap'>
|
<div className='flex gap-5 flex-wrap'>
|
||||||
{
|
{
|
||||||
item.options?.map((value) => {
|
item.options?.map((value) => {
|
||||||
const object = formik.values.printAttributes.find(o => o.fieldId === item.id)
|
|
||||||
return (
|
return (
|
||||||
<div key={value.id} className='flex gap-2 items-center'>
|
<div key={value.id} className='flex gap-2 items-center'>
|
||||||
<div>{value.value}</div>
|
<div>{value.value}</div>
|
||||||
<Checkbox
|
<Checkbox
|
||||||
name={item.id + ''}
|
name={item.id + ''}
|
||||||
checked={object?.value === value.value}
|
checked={value?.value === value.value}
|
||||||
onCheckedChange={(checked) => handleChange(item.id, checked ? value.value : '')}
|
onCheckedChange={(checked) => handleChange(item.id, checked ? value.value : '')}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@@ -79,7 +80,6 @@ const ManageForms: FC<Props> = (props) => {
|
|||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
else if (item.type === FieldTypeEnum.radio) {
|
else if (item.type === FieldTypeEnum.radio) {
|
||||||
const object = formik.values.printAttributes.find(o => o.fieldId === item.id)
|
|
||||||
return (
|
return (
|
||||||
<div key={item.id}>
|
<div key={item.id}>
|
||||||
<div className='text-sm mb-3'>{item.name}</div>
|
<div className='text-sm mb-3'>{item.name}</div>
|
||||||
@@ -91,7 +91,7 @@ const ManageForms: FC<Props> = (props) => {
|
|||||||
}
|
}
|
||||||
})}
|
})}
|
||||||
onChange={(v) => handleChange(item.id, v)}
|
onChange={(v) => handleChange(item.id, v)}
|
||||||
selected={object?.value + '' || ''}
|
selected={value?.value + '' || ''}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
@@ -104,6 +104,7 @@ const ManageForms: FC<Props> = (props) => {
|
|||||||
onChange={(date) => handleChange(item.id, date)}
|
onChange={(date) => handleChange(item.id, date)}
|
||||||
placeholder=''
|
placeholder=''
|
||||||
label={item.name}
|
label={item.name}
|
||||||
|
defaultValue={value?.value}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
@@ -114,6 +115,7 @@ const ManageForms: FC<Props> = (props) => {
|
|||||||
label={item.name}
|
label={item.name}
|
||||||
type={item.type}
|
type={item.type}
|
||||||
onChange={(e) => handleChange(item.id, e.target.value)}
|
onChange={(e) => handleChange(item.id, e.target.value)}
|
||||||
|
value={value?.value}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
@@ -123,6 +125,7 @@ const ManageForms: FC<Props> = (props) => {
|
|||||||
<Textarea
|
<Textarea
|
||||||
label={item.name}
|
label={item.name}
|
||||||
onChange={(e) => handleChange(item.id, e.target.value)}
|
onChange={(e) => handleChange(item.id, e.target.value)}
|
||||||
|
value={value?.value}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user