From 158ef2cb1dd4b27d577ae183875263a1826cdaa3 Mon Sep 17 00:00:00 2001 From: Mohammad Jalali Date: Sun, 21 Jan 2024 15:59:35 +0330 Subject: [PATCH 1/2] complete adding filter of veihcle --- .../passenger-boss/Buttons/printFormA.jsx | 80 ++++++++++++++----- .../passenger-boss/Prints/form-a/index.jsx | 7 +- 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/components/dashboard/passenger-boss/Buttons/printFormA.jsx b/src/components/dashboard/passenger-boss/Buttons/printFormA.jsx index fa1e882..6e0405e 100644 --- a/src/components/dashboard/passenger-boss/Buttons/printFormA.jsx +++ b/src/components/dashboard/passenger-boss/Buttons/printFormA.jsx @@ -3,9 +3,9 @@ import {useRouter} from "next/router"; import {useState} from "react"; import useRequest from "@/lib/app/hooks/useRequest"; import usePrint from "@/lib/app/hooks/usePrint"; -import {GET_PASSENGER_BOSS} from "@/core/data/apiRoutes"; import {Button, CircularProgress} from "@mui/material"; import PrintIcon from "@mui/icons-material/Print"; +import {GET_PASSENGER_BOSS} from "@/core/data/apiRoutes"; const PrintFormA = () => { const t = useTranslations(); @@ -13,26 +13,64 @@ const PrintFormA = () => { const [loading, setLoading] = useState(false) const requestServer = useRequest({auth: true, pending: false, success: {notification: {show: false}}}) const {setPrintPage, setPrintTitle} = usePrint() - const clickHandler = () => { - setLoading(true) - const params = new URLSearchParams(); - params.set("start", '0'); - params.set("filters", '[]'); - params.set("sorting", '[]'); - requestServer(`${GET_PASSENGER_BOSS}?${params}`, 'get').then((response) => { - const _chunkSize = 6 - const _data = response.data.data - const chunk = Array.from({length: Math.ceil(_data.length / _chunkSize)}, (v, i) => - _data.slice(i * _chunkSize, i * _chunkSize + _chunkSize) - ); - setPrintPage(chunk.length) - setPrintTitle(t('PassengerBoss.print_form_a')) - sessionStorage.setItem('form-a-print', JSON.stringify(chunk)) - router.push('/dashboard/passenger-boss/prints/form-a') - }).catch(() => { - setLoading(false) - }) - } + + const filterRequest = async (filterArr) => { + setLoading(true); + + const requests = filterArr.map(async (item) => { + try { + const params = new URLSearchParams(); + params.set("start", '0'); + params.set("filters", JSON.stringify([item])); + params.set("sorting", '[]'); + + const response = await requestServer(`${GET_PASSENGER_BOSS}?${params}`, 'get'); + const _chunkSize = 6; + const _data = response.data.data; + const chunk = Array.from({length: Math.ceil(_data.length / _chunkSize)}, (v, i) => + _data.slice(i * _chunkSize, i * _chunkSize + _chunkSize) + ); + return chunk; + } catch (error) { + setLoading(false); + throw error; // Propagate the error + } + }); + + try { + const chunks = await Promise.all(requests); + setLoading(false); + return chunks.flat(); // Flatten the chunks into a single array + } catch (error) { + console.error("Error processing requests:", error); + throw error; + } + }; + + const clickHandler = async () => { + try { + const chunks = await filterRequest([ + { + id: "vehicle_type", + value: "اتوبوس", + fn: "contains", + datatype: "text", + }, + { + id: "vehicle_type", + value: "مینی بوس", + fn: "contains", + datatype: "text", + }, + ]); + setPrintPage(chunks.length); + setPrintTitle(t('PassengerBoss.print_form_a')); + sessionStorage.setItem('form-a-print', JSON.stringify(chunks)); + router.push('/dashboard/passenger-boss/prints/form-a'); + } catch (error) { + console.error("Error in clickHandler:", error); + } + }; return (