@@ -1,8 +1,9 @@
|
|||||||
import { useState, type FC } from 'react'
|
import { useState, type FC } from 'react'
|
||||||
import { useParams } from 'react-router-dom'
|
import { useParams } from 'react-router-dom'
|
||||||
import { useFormik } from 'formik'
|
import { useFormik } from 'formik'
|
||||||
|
import * as Yup from 'yup'
|
||||||
import { useGetSections } from '../print/hooks/usePrintData'
|
import { useGetSections } from '../print/hooks/usePrintData'
|
||||||
import { useGetOrderPrint, useSubmitOrderPrint } from './hooks/useOrderData'
|
import { useGetOrderDetails, useGetOrderPrint, useSubmitOrderPrint } from './hooks/useOrderData'
|
||||||
import type { OrderPrintFormType } from './types/Types'
|
import type { OrderPrintFormType } from './types/Types'
|
||||||
import PrintSectionCard from './components/PrintSectionCard'
|
import PrintSectionCard from './components/PrintSectionCard'
|
||||||
import PrintSectionNav from './components/PrintSectionNav'
|
import PrintSectionNav from './components/PrintSectionNav'
|
||||||
@@ -10,6 +11,7 @@ import { ORDER_PRINT_STORAGE_KEY } from './PrintPreview'
|
|||||||
import Button from '@/components/Button'
|
import Button from '@/components/Button'
|
||||||
import BackButton from '@/components/BackButton'
|
import BackButton from '@/components/BackButton'
|
||||||
import RefreshButton from '@/components/RefreshButton'
|
import RefreshButton from '@/components/RefreshButton'
|
||||||
|
import Input from '@/components/Input'
|
||||||
import { TickSquare } from 'iconsax-react'
|
import { TickSquare } from 'iconsax-react'
|
||||||
import { toast } from '@/shared/toast'
|
import { toast } from '@/shared/toast'
|
||||||
import { extractErrorMessage } from '@/config/func'
|
import { extractErrorMessage } from '@/config/func'
|
||||||
@@ -19,6 +21,7 @@ const OrderPrint: FC = () => {
|
|||||||
const { id: orderId } = useParams()
|
const { id: orderId } = useParams()
|
||||||
const { data, refetch: refetchSections, isFetching: isSectionsFetching } = useGetSections()
|
const { data, refetch: refetchSections, isFetching: isSectionsFetching } = useGetSections()
|
||||||
const { isPending, mutate } = useSubmitOrderPrint()
|
const { isPending, mutate } = useSubmitOrderPrint()
|
||||||
|
const { data: order, refetch: refetchOrder, isFetching: isOrderFetching } = useGetOrderDetails(orderId!)
|
||||||
const { data: print, refetch: refetchPrint, isFetching: isPrintFetching } = useGetOrderPrint(orderId!)
|
const { data: print, refetch: refetchPrint, isFetching: isPrintFetching } = useGetOrderPrint(orderId!)
|
||||||
|
|
||||||
const sections = data?.data ?? []
|
const sections = data?.data ?? []
|
||||||
@@ -27,9 +30,13 @@ const OrderPrint: FC = () => {
|
|||||||
|
|
||||||
const formik = useFormik<OrderPrintFormType>({
|
const formik = useFormik<OrderPrintFormType>({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
|
title: print?.data?.title || order?.data?.title || '',
|
||||||
fields: print?.data?.fileds ?? []
|
fields: print?.data?.fileds ?? []
|
||||||
},
|
},
|
||||||
enableReinitialize: true,
|
enableReinitialize: true,
|
||||||
|
validationSchema: Yup.object({
|
||||||
|
title: Yup.string().required('این فیلد اجباری می باشد.')
|
||||||
|
}),
|
||||||
onSubmit: (values) => {
|
onSubmit: (values) => {
|
||||||
if (!orderId) return
|
if (!orderId) return
|
||||||
mutate(
|
mutate(
|
||||||
@@ -87,7 +94,7 @@ const OrderPrint: FC = () => {
|
|||||||
|
|
||||||
sessionStorage.setItem(
|
sessionStorage.setItem(
|
||||||
ORDER_PRINT_STORAGE_KEY(orderId),
|
ORDER_PRINT_STORAGE_KEY(orderId),
|
||||||
JSON.stringify({ fields: formik.values.fields })
|
JSON.stringify({ title: formik.values.title, fields: formik.values.fields })
|
||||||
)
|
)
|
||||||
|
|
||||||
const sectionIds = Array.from(selectedSections).join(',')
|
const sectionIds = Array.from(selectedSections).join(',')
|
||||||
@@ -96,10 +103,11 @@ const OrderPrint: FC = () => {
|
|||||||
|
|
||||||
const handleRefresh = () => {
|
const handleRefresh = () => {
|
||||||
refetchSections()
|
refetchSections()
|
||||||
|
refetchOrder()
|
||||||
refetchPrint()
|
refetchPrint()
|
||||||
}
|
}
|
||||||
|
|
||||||
const isRefreshing = isSectionsFetching || isPrintFetching
|
const isRefreshing = isSectionsFetching || isOrderFetching || isPrintFetching
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mt-5">
|
<div className="mt-5">
|
||||||
@@ -125,6 +133,13 @@ const OrderPrint: FC = () => {
|
|||||||
|
|
||||||
<div className="flex gap-6 mt-6 items-start">
|
<div className="flex gap-6 mt-6 items-start">
|
||||||
<div className="flex-1 flex flex-col gap-5 min-w-0">
|
<div className="flex-1 flex flex-col gap-5 min-w-0">
|
||||||
|
<div className="bg-white p-6 rounded-3xl">
|
||||||
|
<Input
|
||||||
|
{...formik.getFieldProps('title')}
|
||||||
|
label="عنوان"
|
||||||
|
error_text={formik.touched.title && formik.errors.title ? formik.errors.title : ''}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
{sections.map((section) => (
|
{sections.map((section) => (
|
||||||
<PrintSectionCard
|
<PrintSectionCard
|
||||||
key={section.id}
|
key={section.id}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useMemo, type FC } from 'react'
|
import { useEffect, useMemo, type FC } from 'react'
|
||||||
import { useParams, useSearchParams } from 'react-router-dom'
|
import { useParams, useSearchParams } from 'react-router-dom'
|
||||||
import { useGetSections } from '@/pages/print/hooks/usePrintData'
|
import { useGetSections } from '@/pages/print/hooks/usePrintData'
|
||||||
import { useGetOrderPrint } from './hooks/useOrderData'
|
import { useGetOrderDetails, useGetOrderPrint } from './hooks/useOrderData'
|
||||||
import PrintFieldDisplay from './components/PrintFieldDisplay'
|
import PrintFieldDisplay from './components/PrintFieldDisplay'
|
||||||
import Button from '@/components/Button'
|
import Button from '@/components/Button'
|
||||||
import { Printer } from 'iconsax-react'
|
import { Printer } from 'iconsax-react'
|
||||||
@@ -12,6 +12,7 @@ const OrderPrintPreview: FC = () => {
|
|||||||
const { id: orderId } = useParams()
|
const { id: orderId } = useParams()
|
||||||
const [searchParams] = useSearchParams()
|
const [searchParams] = useSearchParams()
|
||||||
const { data: sectionsData } = useGetSections()
|
const { data: sectionsData } = useGetSections()
|
||||||
|
const { data: orderData } = useGetOrderDetails(orderId!)
|
||||||
const { data: printData } = useGetOrderPrint(orderId!)
|
const { data: printData } = useGetOrderPrint(orderId!)
|
||||||
|
|
||||||
const selectedSectionIds = useMemo(() => {
|
const selectedSectionIds = useMemo(() => {
|
||||||
@@ -19,18 +20,24 @@ const OrderPrintPreview: FC = () => {
|
|||||||
return param ? param.split(',').filter(Boolean) : []
|
return param ? param.split(',').filter(Boolean) : []
|
||||||
}, [searchParams])
|
}, [searchParams])
|
||||||
|
|
||||||
const fieldValues = useMemo(() => {
|
const draft = useMemo(() => {
|
||||||
const stored = sessionStorage.getItem(ORDER_PRINT_STORAGE_KEY(orderId!))
|
const stored = sessionStorage.getItem(ORDER_PRINT_STORAGE_KEY(orderId!))
|
||||||
if (stored) {
|
if (stored) {
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(stored) as { fields: { fieldId: string; value: string }[] }
|
return JSON.parse(stored) as {
|
||||||
return parsed.fields ?? []
|
title?: string
|
||||||
|
fields: { fieldId: string; value: string }[]
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// fall through to API data
|
// fall through to API data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return printData?.data?.fileds ?? []
|
return null
|
||||||
}, [orderId, printData?.data?.fileds])
|
}, [orderId])
|
||||||
|
|
||||||
|
const printTitle =
|
||||||
|
draft?.title || printData?.data?.title || orderData?.data?.title || 'فرم چاپ سفارش'
|
||||||
|
const fieldValues = draft?.fields ?? printData?.data?.fileds ?? []
|
||||||
|
|
||||||
const sections = useMemo(
|
const sections = useMemo(
|
||||||
() =>
|
() =>
|
||||||
@@ -41,7 +48,7 @@ const OrderPrintPreview: FC = () => {
|
|||||||
const handlePrint = () => window.print()
|
const handlePrint = () => window.print()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
document.title = 'چاپ فرم سفارش'
|
document.title = printTitle
|
||||||
|
|
||||||
const html = document.documentElement
|
const html = document.documentElement
|
||||||
const root = document.getElementById('root')
|
const root = document.getElementById('root')
|
||||||
@@ -67,7 +74,7 @@ const OrderPrintPreview: FC = () => {
|
|||||||
resetScroll(document.body)
|
resetScroll(document.body)
|
||||||
if (root) resetScroll(root)
|
if (root) resetScroll(root)
|
||||||
}
|
}
|
||||||
}, [])
|
}, [printTitle])
|
||||||
|
|
||||||
if (!sections.length) {
|
if (!sections.length) {
|
||||||
return (
|
return (
|
||||||
@@ -96,7 +103,7 @@ const OrderPrintPreview: FC = () => {
|
|||||||
|
|
||||||
<div className="max-w-[210mm] mx-auto p-8 print:p-0">
|
<div className="max-w-[210mm] mx-auto p-8 print:p-0">
|
||||||
<header className="text-center mb-8 pb-4 border-b-2 border-gray-800">
|
<header className="text-center mb-8 pb-4 border-b-2 border-gray-800">
|
||||||
<h1 className="text-2xl font-bold text-gray-900">فرم چاپ سفارش</h1>
|
<h1 className="text-2xl font-bold text-gray-900">{printTitle}</h1>
|
||||||
<p className="text-sm text-gray-500 mt-2">
|
<p className="text-sm text-gray-500 mt-2">
|
||||||
{new Date().toLocaleDateString('fa-IR')}
|
{new Date().toLocaleDateString('fa-IR')}
|
||||||
</p>
|
</p>
|
||||||
@@ -124,7 +131,7 @@ const OrderPrintPreview: FC = () => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<footer className="mt-8 pt-4 border-t border-gray-200 text-center text-xs text-gray-400 print:mt-6">
|
<footer className="mt-8 pt-4 border-t border-gray-200 text-center text-xs text-gray-400 print:mt-6">
|
||||||
نگاره — فرم چاپ سفارش
|
نگاره — {printTitle}
|
||||||
</footer>
|
</footer>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -272,11 +272,13 @@ export type OrderPrintFieldType = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type OrderPrintFormType = {
|
export type OrderPrintFormType = {
|
||||||
|
title: string;
|
||||||
fields: OrderPrintFieldType[];
|
fields: OrderPrintFieldType[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type OrderPrintDataType = {
|
export type OrderPrintDataType = {
|
||||||
id: string;
|
id: string;
|
||||||
|
title: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
deletedAt: string | null;
|
deletedAt: string | null;
|
||||||
fileds: OrderPrintFieldType[];
|
fileds: OrderPrintFieldType[];
|
||||||
|
|||||||
Reference in New Issue
Block a user