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

Commit

Permalink
[Back] Split OrderGetDetail - [Front] Merge OrderAppend | Split Order…
Browse files Browse the repository at this point in the history
…Comment & OrderDetail | useItems | useSWR (#107)

* Update dependencies in package.json and pnpm-lock.yaml

* Update package.json and vite.config.ts

* Update vite.config.ts with splitVendorChunkPlugin

* The most significant changes involve renaming and replacing functions and components in `Append.tsx` and `Detail.tsx`. The `Order` property in the `IOrderInfo` interface has also been made optional, and a `@liveSafe` comment has been added to the `OrderEntity` class.

1. The `Order` property in the `IOrderInfo` interface in `OrderInfo.tsx` has been made optional. This change allows for instances where an order may not be present or necessary. (OrderInfo.tsx)

2. The `AdminOrderAppend` function in `Append.tsx` has been renamed to `AdminCommentAppend`. This change reflects a shift in functionality from appending orders to appending comments. (Append.tsx)

3. The `AdminOrderAppend` component in `Detail.tsx` has been replaced with `AdminCommentAppend`. This change ensures that the updated function is correctly referenced and used in the `Detail.tsx` file. (Detail.tsx)

4. The `OrderAppend` function in `Append.tsx` has been renamed to `CommentAppend`. Similar to the previous renaming, this change reflects a shift in functionality from appending orders to appending comments. (Append.tsx)

5. The `OrderAppend` component in `Detail.tsx` has been replaced with `CommentAppend`. This change ensures that the updated function is correctly referenced and used in the `Detail.tsx` file. (Detail.tsx)

6. The `OrderEntity` class in `Entity.ts` has been updated with a `@liveSafe` comment for the `Order` and `Comment` methods. This comment likely indicates that these methods have been reviewed and deemed safe for live deployment. (Entity.ts)

* The most significant changes involve the removal of the `AdminCommentAppend` component from `Append.tsx` and its replacement with the `CommentAppend` component in `Detail.tsx`. Additionally, the `AdminHub` import was added to `Append.tsx` and the `IOrderAppend` interface was updated with the `Admin` prop. The `CommentAppend` function and `run: cancel` function in `Append.tsx` were also updated to use either `AdminHub` or `Hub` and `hub.useClose` or `hub.useCancel` respectively, based on the `Admin` prop. Lastly, the `Message` and `ToastTitle` in `Append.tsx` were updated to display either "Close" or "Cancel" based on the `Admin` prop.

Changes:

1. Removed `AdminCommentAppend` component from `Append.tsx` along with its related imports and styles.
2. Replaced `AdminCommentAppend` import with `CommentAppend` import from a different path in `Detail.tsx`.
3. Replaced `AdminCommentAppend` component usage with `CommentAppend` component in `Detail.tsx`.
4. Added `AdminHub` import to `Append.tsx`.
5. Moved `Status` prop and added `Admin` prop in the `IOrderAppend` interface in `Append.tsx`.
6. Updated `CommentAppend` function in `Append.tsx` to include the `Admin` prop and to use either `AdminHub` or `Hub` based on the `Admin` prop.
7. Updated `run: cancel` function in `Append.tsx` to use either `hub.useClose` or `hub.useCancel` based on the `Admin` prop.
8. Updated `Message` and `ToastTitle` in `Append.tsx` to display either "Close" or "Cancel" based on the `Admin` prop.

* Add "Admin" prop to CommentAppend component

* The most significant changes involve the refactoring of the `OrderGetDetail` method in the `ShopHub` and `OrderGet` classes. This method has been split into two separate methods: `OrderGetItems` and `OrderGetCmts`, improving the separation of concerns in the code. The `OrderGet` class in `Get.ts` has been updated to reflect these changes. Additionally, a new `OrderComment` component has been added to `index.tsx` to display a list of comments related to an order.

Here is a list of the changes:

1. The `OrderGetDetail` method in the `ShopHub` class has been split into `OrderGetItems` and `OrderGetCmts` methods. This change improves the separation of concerns in the code.
2. The `OrderGetDetail` method in the `OrderGet` class has been renamed to `OrderGetItems` and now returns an array of `ICartItem` objects.
3. A new method `OrderGetCmts` has been added to the `OrderGet` class. This method retrieves comments related to an order and returns an array of `IComment` objects.
4. The `Detail` method in the `OrderDetail` component has been updated to use the `OrderGetItems` method instead of the old `OrderGetDetail` method.
5. The `CommentAppend` component has been updated to include a condition for the admin user. If the user is an admin, the button text changes to "Force Close".
6. A new `OrderComment` component has been added to `index.tsx`. This component displays a list of comments related to an order.
7. The `IOrderDetail` import has been removed from `Get.ts` as it is no longer needed.
8. The `OrderGet` class in `Get.ts` has been updated to reflect the changes in the `ShopHub` class. The `Detail` method has been renamed to `Items` and a new `Cmts` method has been added.
9. The `OrderGet` class in `Get.ts` has been updated to use the new `OrderGetItems` and `OrderGetCmts` methods from the `ShopHub` class. The `Detail` method has been removed.
10. The `OrderGet` class in `Get.ts` has been updated to return the items and comments separately, instead of returning them together in a single object.

* The most significant changes involve the introduction of a new interface `IOrderComp` and the refactoring of several functions to use this interface. The `OrderComment`, `OrderAction`, and `CommentAppend` functions now all take an `IOrderComp` object as a parameter. The `OrderDetail` function was also updated to include `OrderComment` and `OrderAction` components. Additionally, the `useRequest` hook was introduced in several places, and the `CommentAppend` component was moved to a different location.

1. New imports were added to `index.tsx`, including `useConst`, `useRequest`, `ICompLog`, `Hub`, and `CommentAppend` (index.tsx).
2. A new interface `IOrderComp` was added to `index.tsx`, extending `ICompLog` and adding new properties (index.tsx).
3. The `OrderComment` function was updated to take an `IOrderComp` object as a parameter and use the `useConst` and `useRequest` hooks (index.tsx).
4. The import of `CommentAppend` in `Detail.tsx` was updated (Detail.tsx).
5. The `IOrderAction` interface was removed and the `OrderAction` function was updated to take an `IOrderComp` object as a parameter (Action.tsx).
6. The entire contents of `Append.tsx` were replaced with new code defining a `CommentAppend` function (Append.tsx).
7. The entire contents of `Comment.tsx` were removed (Comment.tsx).
8. The `OrderDetail` function was updated to use the `useRequest` hook twice and include `OrderComment` and `OrderAction` components (Detail.tsx).
9. The import of `IComment` in `Get.ts` was updated (Get.ts).

* Refactor Comment component and its dependencies

* The most significant changes involve modifications to the import statements and function calls in Detail.tsx, Get.ts, and SignalR.ts. In Detail.tsx, the import path of `OrderComment` was changed and the `useRequest` function call was replaced with `Hub.Order.Get.useItems(OrderId, log)`. In Get.ts, new import statements were added and the `Items` method was refactored to use `useItems` instead of `async Items`. In SignalR.ts, new import statements were added and a new method `useSWR` was added to the `SignalR` class.

1. The import path of `OrderComment` was changed in Detail.tsx. (Detail.tsx)
2. The `useRequest` function call was replaced with `Hub.Order.Get.useItems(OrderId, log)` in Detail.tsx. (Detail.tsx)
3. The `runItem()` function call was removed from the `run` function in Detail.tsx. (Detail.tsx)
4. New import statements were added in Get.ts to import `useConst` and `useLiveQuery`. (Get.ts)
5. The `Items` method in the `OrderGet` class was refactored to use `useItems` instead of `async Items` in Get.ts. (Get.ts)
6. The `PhotoList` method in the `ProductGet` class was modified to use a constant `photoList` instead of a string literal in Get.ts. (Get.ts)
7. New import statements were added in SignalR.ts to import `useRequest` and `Options`. (SignalR.ts)
8. A new method `useSWR` was added to the `SignalR` class in SignalR.ts. (SignalR.ts)
9. A comment was added in SignalR.ts indicating a potential issue with the `LiveQuery` causing infinite refreshes. (SignalR.ts)

* The most significant changes involve the replacement of the `dexie-react-hooks` library with the `useState` hook from React in the `Get.ts` file, and the replacement of the `useLiveQuery` hook with a manual implementation of state management and data fetching. These changes were made to have more control over state management and data fetching. Additionally, the `Promise.resolve` method was replaced with `Dexie.waitFor` in several files to ensure promises are resolved before proceeding with the next operation.

Here are the changes in detail:

1. The `dexie-react-hooks` library was replaced with the `useState` hook from React in the `Get.ts` file for better state management within the component.
2. The `useLiveQuery` hook was removed and replaced with a manual implementation of state management and data fetching for more control over the data fetching process.
3. The `Promise.resolve` method was replaced with `Dexie.waitFor` in the `SignalR.ts` and `Table.ts` files to ensure promises are resolved before proceeding with the next operation.
4. The `Shared.Sto.delete` method was updated to be awaited in the `SignalR.ts` file to ensure the delete operation completes before proceeding.
5. The version annotation for the `useSWR` method in the `SignalR.ts` file was updated from `1.3.0` to `1.3.5`.
6. The `Table.ts` file was updated to use `Dexie.waitFor` instead of `Promise.resolve` when setting the value in the table to ensure the value is set before proceeding.
7. The `Table.ts` file was updated to format the object being put into the store in a more readable way to improve code readability.
8. The `Table.ts` file was updated to throw a `RangeError` if the expire time is less than the current time to prevent setting an expire time in the past.

* The most significant changes involve the introduction of new hooks and the refactoring of the `DrawerBody` and `OrderGet` components. The `useEffect` and `useBoolean` hooks from the "ahooks" library have been updated, with the former now setting the state of `open` based on the comparison of `curr` and `OrderId`, and the latter now only using the `set` method. The `useInViewport` and `useRef` hooks have been added from the "ahooks" and "react" libraries respectively, and are used to create a reference to the `DrawerBody` component and check if it's in the viewport.

The `DrawerBody` component has been updated to use a `ref` attribute and conditionally render the `DeferredBody` component if it's in the viewport. A new function `DeferredBody` has been added, which contains the previous logic for fetching and displaying order details. It uses the `useRequest` hook to fetch order and cart data and displays it using various components.

The `useItems` method in the `OrderGet` class has been updated to use the `useSWR` hook to fetch data and returns an object containing the request and the data. Lastly, the version number in the `OrderDetail` function comment has been updated from 0.4.0 to 1.0.0.

Changes:
1. Updated `useEffect` and `useBoolean` hooks from "ahooks" library.
2. Added `useInViewport` and `useRef` hooks from "ahooks" and "react" libraries.
3. Updated `DrawerBody` component to use a `ref` attribute and conditionally render `DeferredBody`.
4. Added new function `DeferredBody` for fetching and displaying order details.
5. Updated `useItems` method in `OrderGet` class to use `useSWR` hook.
6. Updated version number in `OrderDetail` function comment.

* The most significant changes were made to the `Detail.tsx`, `Get.ts`, and `SignalR.ts` files. In `Detail.tsx`, the import statement was updated to include `SkeletonItem` from `@fluentui/react-components` and a `loading` state was added to the `DeferredBody` function. The `Get.ts` file was updated to use `useAsyncEffect` from `ahooks` for handling asynchronous operations. The `SignalR.ts` file saw changes in the `update` function, including the addition of a `setCache` function and changes to the conditions for updating the cache.

1. In `Detail.tsx`, the import statement was updated to include `SkeletonItem` from `@fluentui/react-components`. The `DeferredBody` function was updated to include a `loading` state from `Hub.Order.Get.useItems`. A `run` function was also added to run `runItems` and `runOrder` together. A `SkeletonItem` component was added to the JSX returned by the `DeferredBody` function to display a loading state.

2. In `Get.ts`, the import statement was updated to include `useAsyncEffect` from `ahooks`. The `OrderGet` class was updated to use `useAsyncEffect` to handle the asynchronous operations previously handled in the `onSuccess` callback. The logic within the `onSuccess` callback was moved into the `useAsyncEffect` hook.

3. In `SignalR.ts`, the `update` function within the `SignalR` class was updated to include a `setCache` function that sets a cache with a `QueryExp` value 10 seconds in the future. The `setCache` function was also updated to delete the cache if the response is null. The condition to update the cache was changed from `find.QueryExp <= dayjs().unix()` to `find.QueryExp < dayjs().unix()`. The `setCache` function that was previously outside the `update` function was removed. The `useRequest` function was updated to have a `staleTime` of 5000 instead of 1000.
  • Loading branch information
Aloento authored Feb 13, 2024
1 parent 10b6d78 commit 27adc5e
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 384 deletions.
23 changes: 13 additions & 10 deletions SoarCraft.AwaiShop/Hub/Order/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,30 @@ await this.Db.Orders
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 1.0.0
* @version 1.1.0
* </remarks>
*/
[Authorize]
public async Task<dynamic> OrderGetDetail(uint orderId) {
var items = await this.Db.OrderCombos
public async Task<dynamic> OrderGetItems(uint orderId) =>
await this.Db.OrderCombos
.Where(x => x.OrderId == orderId && x.Order.UserId == this.UserId)
.Select(x => new {
x.Quantity,
Types = x.Combo.Types.Select(t => t.TypeId).ToArray()
})
.ToArrayAsync();

var cmts = await this.Db.Comments
/**
* <remarks>
* @author Aloento
* @since 1.3.0
* @version 0.1.0
* </remarks>
*/
[Authorize]
public Task<uint[]> OrderGetCmts(uint orderId) =>
this.Db.Comments
.Where(x => x.OrderId == orderId && x.Order.UserId == this.UserId)
.Select(x => x.CommentId)
.ToArrayAsync();

return new {
Items = items,
Comments = cmts
};
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@azure/msal-react": "^2.0.11",
"@fluentui/react-components": "^9.46.4",
"@fluentui/react-hooks": "^8.6.36",
"@fluentui/react-icons": "^2.0.226",
"@fluentui/react-icons": "^2.0.227",
"@griffel/react": "^1.5.20",
"@lexical/clipboard": "0.10.0",
"@lexical/code": "0.10.0",
Expand Down Expand Up @@ -57,6 +57,6 @@
"@types/react-dom": "^18.2.19",
"@vitejs/plugin-react-swc": "^3.6.0",
"typescript": "^5.3.3",
"vite": "^5.1.0"
"vite": "^5.1.1"
}
}
182 changes: 91 additions & 91 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

35 changes: 15 additions & 20 deletions src/Pages/History/Append.tsx → src/Components/Order/Append.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Button, Field, Textarea, Toast, ToastTitle, makeStyles } from "@fluentui/react-components";
import { useConst } from "@fluentui/react-hooks";
import { useState } from "react";
import { Logger } from "~/Helpers/Logger";
import { Flex } from "~/Helpers/Styles";
import { useErrorToast } from "~/Helpers/useToast";
import { Hub } from "~/ShopNet";
import { AdminHub } from "~/ShopNet/Admin";
import { IOrderComp } from "./Comment";

/**
* @author Aloento
Expand All @@ -21,29 +22,19 @@ const useStyles = makeStyles({
/**
* @author Aloento
* @since 0.5.0
* @version 0.1.1
* @version 1.0.0
*/
interface IOrderAppend {
OrderId: number;
Status?: string;
Refresh: () => void;
ParentLog: Logger;
}

/**
* @author Aloento
* @since 0.5.0
* @version 0.4.2
*/
export function OrderAppend({ OrderId, Status, Refresh, ParentLog }: IOrderAppend) {
export function CommentAppend({ OrderId, Refresh, ParentLog, Status, Admin }: IOrderComp) {
const log = useConst(() => ParentLog.With("Append"));

const style = useStyles();
const [cmt, setCmt] = useState<string>();

const { dispatch, dispatchToast } = useErrorToast(log);

const { run: append } = Hub.Order.Post.useAppend({
const hub = (Admin ? AdminHub : Hub).Order.Post as typeof AdminHub.Order.Post & typeof Hub.Order.Post;

const { run: append } = hub.useAppend({
manual: true,
onError(e, req) {
dispatch({
Expand All @@ -64,19 +55,19 @@ export function OrderAppend({ OrderId, Status, Refresh, ParentLog }: IOrderAppen
}
});

const { run: cancel } = Hub.Order.Post.useCancel({
const { run: cancel } = (Admin ? hub.useClose : hub.useCancel)({
manual: true,
onError(e, params) {
dispatch({
Message: "Failed Cancel Order",
Message: `Failed ${Admin ? "Close" : "Cancel"} Order`,
Request: params,
Error: e
});
},
onSuccess() {
dispatchToast(
<Toast>
<ToastTitle>Order Canceled</ToastTitle>
<ToastTitle>Order {Admin ? "Closed" : "Cancelled"}</ToastTitle>
</Toast>,
{ intent: "success" }
);
Expand All @@ -100,7 +91,11 @@ export function OrderAppend({ OrderId, Status, Refresh, ParentLog }: IOrderAppen
{
!(Status === "Finished" || Status === "Returning") &&
<Button onClick={() => cancel(OrderId, cmt!)}>
{Status === "Shipping" ? "Ask Return" : "Cancel Order"} with Reason
{
Admin
? "Force Close"
: Status === "Shipping" ? "Ask Return" : "Cancel Order"
} with Reason
</Button>
}

Expand Down
60 changes: 60 additions & 0 deletions src/Components/Order/Comment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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";

/**
* @author Aloento
* @since 1.0.0
* @version 0.1.0
*/
export interface IComment {
Content: string;
Time: Date;
User: string;
}

/**
* @author Aloento
* @since 0.5.0
* @version 0.2.0
*/
export interface IOrderComp extends ICompLog {
OrderId: number;
Refresh: () => void;
Status?: string;
Admin?: true;
}

/**
* @author Aloento
* @since 1.0.0
* @version 0.1.0
*/
export function OrderComment({ OrderId, Refresh, ParentLog, Status, Admin }: IOrderComp) {
const log = useConst(() => ParentLog.With("Comment"));

const { data, run } = useRequest(() => Hub.Order.Get.Cmts(OrderId, log), {
manual: true,
onError: log.error
});

return <>
<Field label="Comment" size="large">
{data?.length === 0
?
<Label>No Comment</Label>
:
data?.map((v, i) => <div key={i}>
<Caption1Stronger>{v.User} {v.Time.toLocaleString()}</Caption1Stronger>
<br />
<Body1>{v.Content}</Body1>
</div>
)}
</Field>

<CommentAppend OrderId={OrderId} Status={Status} Refresh={run} ParentLog={log} />
</>;
}
2 changes: 1 addition & 1 deletion src/Components/OrderInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const useStyles = makeStyles({
*/
interface IOrderInfo {
OrderId: number;
Order: OrderEntity.Order;
Order?: OrderEntity.Order;
Admin?: true;
}

Expand Down
95 changes: 0 additions & 95 deletions src/Pages/Admin/Order/Append.tsx

This file was deleted.

8 changes: 4 additions & 4 deletions src/Pages/Admin/Order/Detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { Drawer, DrawerBody, DrawerHeader, DrawerHeaderTitle } from "@fluentui/r
import { DismissRegular, OpenRegular } from "@fluentui/react-icons";
import { useBoolean, useRequest } from "ahooks";
import { useEffect } from "react";
import { CommentAppend } from "~/Components/Order/Append";
import { OrderComment } from "~/Components/Order/Comment";
import { OrderInfo } from "~/Components/OrderInfo";
import { useRouter } from "~/Components/Router";
import { Logger } from "~/Helpers/Logger";
import { ColFlex } from "~/Helpers/Styles";
import { OrderComment } from "~/Pages/History/Comment";
import { AdminHub } from "~/ShopNet/Admin";
import { AdminOrderAction } from "./Action";
import { AdminOrderAppend } from "./Append";
import { AdminOrderList } from "./List";
import { Shipment } from "./Ship";

Expand All @@ -31,7 +31,7 @@ const log = new Logger("Admin", "Order", "Detail");
/**
* @author Aloento
* @since 0.5.0
* @version 0.3.1
* @version 0.4.0
*/
export function AdminOrderDetail({ OrderId }: { OrderId: number; }) {
const style = useStyles();
Expand Down Expand Up @@ -104,7 +104,7 @@ export function AdminOrderDetail({ OrderId }: { OrderId: number; }) {

<OrderComment Comments={data?.Comments} />

<AdminOrderAppend OrderId={OrderId} Refresh={run} />
<CommentAppend OrderId={OrderId} Status={order?.Status} Admin Refresh={run} ParentLog={log} />

<AdminOrderAction OrderId={OrderId} Status={order?.Status} Refresh={run} />
</DrawerBody>
Expand Down
15 changes: 2 additions & 13 deletions src/Pages/History/Action.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Field, Toast, ToastTitle, makeStyles } from "@fluentui/react-components";
import { useConst } from "@fluentui/react-hooks";
import { IOrderComp } from "~/Components/Order/Comment";
import { useRouter } from "~/Components/Router";
import { ICompLog } from "~/Helpers/Logger";
import { ColFlex } from "~/Helpers/Styles";
import { useErrorToast } from "~/Helpers/useToast";
import { Hub } from "~/ShopNet";
Expand All @@ -18,23 +18,12 @@ const useStyles = makeStyles({
},
});

/**
* @author Aloento
* @since 1.0.0
* @version 0.1.1
*/
interface IOrderAction extends ICompLog {
OrderId: number;
Status?: string;
Refresh: () => void;
}

/**
* @author Aloento
* @since 1.0.0
* @version 0.1.2
*/
export function OrderAction({ OrderId, Status, Refresh, ParentLog }: IOrderAction) {
export function OrderAction({ OrderId, Status, Refresh, ParentLog }: IOrderComp) {
const log = useConst(() => ParentLog.With("Action"));

const style = useStyles();
Expand Down
Loading

0 comments on commit 27adc5e

Please sign in to comment.