From bb76728aa3b753137678bbe20aff9a1e281a3390 Mon Sep 17 00:00:00 2001 From: Aloento <11802769+Aloento@users.noreply.github.com> Date: Wed, 14 Feb 2024 14:11:51 +0100 Subject: [PATCH] The most significant changes involve the `OrderComment` function in Comment.tsx and the `Cmts` function in Get.ts. In Comment.tsx, the `useRequest` import was removed and replaced with `Hub.Order.Get.useCmts` in the `OrderComment` function. The version annotation was also updated. In Get.ts, the `Cmts` function was transformed from an asynchronous function to a hook, using `useSWR` to fetch data and `useAsyncEffect` to process it. The initial state of `res` in the `useItems` function was also changed. 1. The `useRequest` import from "ahooks" was removed from Comment.tsx. This suggests a shift away from using this library in the codebase. 2. The version annotation in the `OrderComment` function was updated from 0.1.0 to 1.0.0 in Comment.tsx, indicating a major version change. 3. The `useRequest` function was replaced with `Hub.Order.Get.useCmts` in the `OrderComment` function in Comment.tsx, suggesting a change in the way comments are fetched. 4. The initial state of `res` was changed from an empty array to undefined in the `useItems` function in Get.ts, which could affect how the function handles initial data. 5. The `Cmts` function in Get.ts was updated from an asynchronous function to a hook. This change allows the function to use `useSWR` to fetch data and `useAsyncEffect` to process the data. 6. The `Cmts` function now returns an object containing the request and the processed data, providing more information to the caller. 7. The `since` annotation in the `Cmts` function was updated from 1.3.0 to 1.3.5 in Get.ts, indicating a minor version change. --- src/Components/Order/Comment.tsx | 8 ++--- src/ShopNet/Order/Get.ts | 56 ++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/Components/Order/Comment.tsx b/src/Components/Order/Comment.tsx index bd654ac..76e7a64 100644 --- a/src/Components/Order/Comment.tsx +++ b/src/Components/Order/Comment.tsx @@ -1,6 +1,5 @@ import { Body1, Caption1Stronger, Field, Label } from "@fluentui/react-components"; import { useConst } from "@fluentui/react-hooks"; -import { useRequest } from "ahooks"; import { ICompLog } from "~/Helpers/Logger"; import { Hub } from "~/ShopNet"; import { CommentAppend } from "./Append"; @@ -31,14 +30,11 @@ export interface IOrderComp extends ICompLog { /** * @author Aloento * @since 1.0.0 - * @version 0.1.0 + * @version 1.0.0 */ export function OrderComment({ OrderId, Status, Admin, ParentLog }: Omit) { const log = useConst(() => ParentLog.With("Comment")); - - const { data, run } = useRequest(() => Hub.Order.Get.Cmts(OrderId, log), { - onError: log.error - }); + const { data, run } = Hub.Order.Get.useCmts(OrderId, log); return <> diff --git a/src/ShopNet/Order/Get.ts b/src/ShopNet/Order/Get.ts index b9bdf05..a437eee 100644 --- a/src/ShopNet/Order/Get.ts +++ b/src/ShopNet/Order/Get.ts @@ -79,7 +79,7 @@ export abstract class OrderGet extends OrderEntity { */ public static useItems(orderId: number, pLog: Logger) { const log = useConst(() => pLog.With(...this.Log, "Items")); - const [res, setRes] = useState([]); + const [res, setRes] = useState(); const req = this.useSWR< { @@ -159,30 +159,50 @@ export abstract class OrderGet extends OrderEntity { /** * @author Aloento - * @since 1.3.0 + * @since 1.3.5 * @version 0.1.0 */ - public static async Cmts(orderId: number, pLog: Logger): Promise { - const log = pLog.With(...this.Log, "Cmts"); + public static useCmts(orderId: number, pLog: Logger) { + const log = useConst(() => pLog.With(...this.Log, "Cmts")); + const [res, setRes] = useState(); - const cmts = await this.GetTimeCache(orderId, "OrderGetCmts", (x) => x.add(1, "m"), orderId); - const comments: IComment[] = []; + const req = this.useSWR( + orderId, + "OrderGetCmts", + { + defaultParams: [orderId], + onError: log.error + } + ); - for (const cmtId of cmts) { - const cmt = await this.Comment(cmtId); + useAsyncEffect(async () => { + const cmts = req.data; + if (!cmts) + return; - if (!cmt) { - log.warn(`[Mismatch] Comment ${cmtId} not found. Order : ${orderId}`); - continue; + const comments: IComment[] = []; + + for (const cmtId of cmts) { + const cmt = await this.Comment(cmtId); + + if (!cmt) { + log.warn(`[Mismatch] Comment ${cmtId} not found. Order : ${orderId}`); + continue; + } + + comments.push({ + Content: cmt.Content, + Time: cmt.CreateAt, + User: cmt.Name || "You" + }); } - comments.push({ - Content: cmt.Content, - Time: cmt.CreateAt, - User: cmt.Name || "You" - }); - } + setRes(comments.sort((a, b) => a.Time.getTime() - b.Time.getTime())); + }, [req.data]); - return comments.sort((a, b) => a.Time.getTime() - b.Time.getTime()); + return { + ...req, + data: res + }; } }