From 551a7e401b055cbac1e9a7a0e8cc5a6c8aa16a2b Mon Sep 17 00:00:00 2001 From: "cyril.tsui" Date: Mon, 7 Jul 2025 17:38:10 +0800 Subject: [PATCH] Update search function & utils --- src/app/utils/commonUtil.ts | 31 ++++++++++++++----- src/app/utils/formatUtil.ts | 6 +++- src/components/SearchBox/SearchBox.tsx | 16 ++++++++-- .../SearchResults/SearchResults.tsx | 2 +- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/app/utils/commonUtil.ts b/src/app/utils/commonUtil.ts index c73ace6..21d02f4 100644 --- a/src/app/utils/commonUtil.ts +++ b/src/app/utils/commonUtil.ts @@ -1,9 +1,26 @@ - +import { defaultPagingController } from './../../components/SearchResults/SearchResults'; +import { isNullOrUndefined } from "html5-qrcode/esm/core"; +import { isEmpty } from "lodash"; export const downloadFile = (blobData: Uint8Array, filename: string) => { - const url = URL.createObjectURL(new Blob([blobData])); - const link = document.createElement("a"); - link.href = url; - link.setAttribute("download", filename); - link.click(); - } \ No newline at end of file + const url = URL.createObjectURL(new Blob([blobData])); + const link = document.createElement("a"); + link.href = url; + link.setAttribute("download", filename); + link.click(); +} + +export const convertObjToURLSearchParams = (data: T | null): string => { + if (isEmpty(data)) { + return '' + } + + const params = new URLSearchParams() + Object.entries(data).forEach(([key, value]) => { + if (!isNullOrUndefined(value)) { + params.append(key, String(value)) + } + }) + + return params.toString() +} \ No newline at end of file diff --git a/src/app/utils/formatUtil.ts b/src/app/utils/formatUtil.ts index 1272c67..29f0c7c 100644 --- a/src/app/utils/formatUtil.ts +++ b/src/app/utils/formatUtil.ts @@ -1,4 +1,4 @@ -import dayjs, { ConfigType } from "dayjs"; +import dayjs, { ConfigType, Dayjs } from "dayjs"; import { Uom } from "../api/settings/uom"; import { ListIterateeCustom, every, isArray, isNaN, isNull, isUndefined, take } from "lodash"; @@ -54,6 +54,10 @@ export const dateTimeStringToDayjs = (dateTime: string) => { return dayjs(dateTime, `${OUTPUT_DATE_FORMAT} ${OUTPUT_TIME_FORMAT}`) } +export const dayjsToDateString = (date: Dayjs) => { + return date.format(OUTPUT_DATE_FORMAT) +} + export const stockInLineStatusMap: { [status: string]: number } = { "draft": 0, "pending": 1, diff --git a/src/components/SearchBox/SearchBox.tsx b/src/components/SearchBox/SearchBox.tsx index 34281e5..d386fcf 100644 --- a/src/components/SearchBox/SearchBox.tsx +++ b/src/components/SearchBox/SearchBox.tsx @@ -111,11 +111,21 @@ function SearchBox({ () => criteria.reduce>( (acc, c) => { - return { + var tempCriteria = { ...acc, [c.paramName]: (c.type === "select" || c.type === "select-labelled" || (c.type === "autocomplete" && !Boolean(c.multiple)) ? "All" : (c.type === "autocomplete" && Boolean(c.multiple)) ? [defaultAll.value]: "") - }; + } + + if (c.type === "dateRange") { + tempCriteria = { + ...tempCriteria, + [c.paramName]: "", + [`${c.paramName}To`]: "" + } + } + + return tempCriteria; }, {} as Record, ), @@ -316,6 +326,7 @@ function SearchBox({ ({ diff --git a/src/components/SearchResults/SearchResults.tsx b/src/components/SearchResults/SearchResults.tsx index 9cc7fdb..05f3d70 100644 --- a/src/components/SearchResults/SearchResults.tsx +++ b/src/components/SearchResults/SearchResults.tsx @@ -203,7 +203,7 @@ function SearchResults({ if (setPagingController) { setPagingController({ ...pagingController, - pageNum: +event.target.value, + pageNum: 1, }); } };