Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
The most significant changes involve the OrderComment function in C…
Browse files Browse the repository at this point in the history
…omment.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.
  • Loading branch information
Aloento committed Feb 14, 2024
1 parent 5eb895a commit bb76728
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
8 changes: 2 additions & 6 deletions src/Components/Order/Comment.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<IOrderComp, "Refresh">) {
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 <>
<Field label="Comment" size="large">
Expand Down
56 changes: 38 additions & 18 deletions src/ShopNet/Order/Get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICartItem[]>([]);
const [res, setRes] = useState<ICartItem[]>();

const req = this.useSWR<
{
Expand Down Expand Up @@ -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<IComment[]> {
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<IComment[]>();

const cmts = await this.GetTimeCache<number[]>(orderId, "OrderGetCmts", (x) => x.add(1, "m"), orderId);
const comments: IComment[] = [];
const req = this.useSWR<number[]>(
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
};
}
}

0 comments on commit bb76728

Please sign in to comment.