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

Commit

Permalink
Update order handling methods and components
Browse files Browse the repository at this point in the history
Updated various methods and components related to order handling. The return type of `OrderPostClose` and `OrderPostCancel` methods in `Post.cs` and `useClose` and `useCancel` methods in `OrderPost` and `AdminOrderPost` classes have been changed from `Task<bool>` to `Task<string>`. Renamed `OrderDeleteCancel` method in `Delete.cs` to `OrderDeleteCancelled` and updated `useDelete` method in `OrderDelete` class to invoke the renamed method. Added a `refresh` method to `useSWR` function in `useSWR.ts` to clear cache or remove item from local storage before refreshing data. Removed `Order`, `Status`, and `Refresh` props from `OrderDetailDrawer`, `OrderInfo`, `AdminOrderAction`, and `OrderAction` components in `Drawer.tsx`, `Info.tsx`, and `Action.tsx` respectively, and replaced them with `useOrder` hook. Updated `useItems` method in `OrderGet` class in `Get.ts` to fetch product photo only if `admin` parameter is not true.
  • Loading branch information
Aloento committed Feb 19, 2024
1 parent 03d9925 commit a763c73
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 135 deletions.
8 changes: 5 additions & 3 deletions SoarCraft.AwaiShop/AdminHub/Order/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ await this.Db.Comments.AddAsync(new() {
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 1.2.0
* @version 1.3.0
* </remarks>
*/
public async Task<bool> OrderPostClose(uint orderId, string reason) {
public async Task<string> OrderPostClose(uint orderId, string reason) {
var valid = typeof(Comment)
.GetProperty(nameof(Comment.Content))!
.GetCustomAttribute<StringLengthAttribute>()!;
Expand Down Expand Up @@ -72,7 +72,9 @@ await this.Db.Comments.AddAsync(new() {
Order = order,
});

return await this.Db.SaveChangesAsync() > 0;
await this.Db.SaveChangesAsync();

return order.Status.ToString();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion SoarCraft.AwaiShop/Hub/Order/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal partial class ShopHub {
* </remarks>
*/
[Authorize]
public async Task<bool> OrderDeleteCancel(uint orderId) {
public async Task<bool> OrderDeleteCancelled(uint orderId) {
var row = await this.Db.Orders
.Where(x => x.UserId == this.UserId)
.Where(x => x.OrderId == orderId)
Expand Down
10 changes: 6 additions & 4 deletions SoarCraft.AwaiShop/Hub/Order/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ await this.Db.Comments.AddAsync(new() {
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 1.2.0
* @version 1.3.0
* </remarks>
*/
[Authorize]
public async Task<bool> OrderPostCancel(uint orderId, string reason) {
public async Task<string> OrderPostCancel(uint orderId, string reason) {
var valid = typeof(Comment)
.GetProperty(nameof(Comment.Content))!
.GetCustomAttribute<StringLengthAttribute>()!;
Expand All @@ -118,8 +118,8 @@ public async Task<bool> OrderPostCancel(uint orderId, string reason) {
throw new HubException(valid.FormatErrorMessage("Reason"));

var order = await this.Db.Orders
.Where(x => x.UserId == this.UserId)
.Where(x => x.OrderId == orderId)
.Where(x => x.UserId == this.UserId)
.Where(x => x.Status != OrderStatus.Cancelled)
.Where(x => x.Status != OrderStatus.Finished)
.Include(x => x.OrderCombos)
Expand All @@ -139,7 +139,9 @@ await this.Db.Comments.AddAsync(new() {
Order = order
});

return await this.Db.SaveChangesAsync() > 0;
await this.Db.SaveChangesAsync();

return order.Status.ToString();
}

/**
Expand Down
44 changes: 23 additions & 21 deletions src/Components/Order/Append.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { Button, Field, Textarea, Toast, ToastTitle, makeStyles } from "@fluentu
import { useConst } from "@fluentui/react-hooks";
import { useState } from "react";
import { Flex } from "~/Helpers/Styles";
import { useSWR } from "~/Helpers/useSWR";
import { useErrorToast } from "~/Helpers/useToast";
import { Hub } from "~/ShopNet";
import { AdminHub } from "~/ShopNet/Admin";
import { SignalR } from "~/ShopNet/SignalR";
import { IOrderRef } from ".";
import { IOrderComp } from ".";
import { useOrder } from "./useOrder";

/**
* @author Aloento
Expand All @@ -24,19 +23,17 @@ const useStyles = makeStyles({
/**
* @author Aloento
* @since 0.5.0
* @version 1.2.0
* @version 1.3.0
*/
export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderRef) {
export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderComp & { Refresh: () => void }) {
const log = useConst(() => ParentLog.With("Append"));

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

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

const hub = (Admin ? AdminHub : Hub).Order.Post as typeof AdminHub.Order.Post & typeof Hub.Order.Post;

const { run: append } = hub.useAppend({
const { run: append, loading } = (Admin ? AdminHub : Hub).Order.Post.useAppend({
manual: true,
onError(e, req) {
dispatch({
Expand All @@ -54,10 +51,13 @@ export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderRef)
);

Refresh();
setCmt("");
}
});

const { run: cancel } = (Admin ? hub.useClose : hub.useCancel)({
const { data: order, mutate } = useOrder(OrderId, Admin);

const { run: cancel, loading: submit } = (Admin ? AdminHub : Hub).Order.Post.useCancel({
manual: true,
onError(e, params) {
dispatch({
Expand All @@ -66,26 +66,21 @@ export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderRef)
Error: e
});
},
onSuccess() {
onSuccess(data) {
dispatchToast(
<Toast>
<ToastTitle>Order {Admin ? "Closed" : "Cancelled"}</ToastTitle>
</Toast>,
{ intent: "success" }
);

Refresh();
mutate((old) => ({
...old!,
Status: data
}));
}
});

const index = useConst(() => SignalR.Index(OrderId, Hub.Order.Get.order));

const { data: order } = useSWR(
index,
() => (Admin ? AdminHub : Hub).Order.Get.Order(OrderId),
{ useMemory: true }
);

switch (order?.Status) {
case "Cancelled":
case "Finished":
Expand All @@ -100,7 +95,10 @@ export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderRef)
<div className={style.body}>
{
!(order?.Status === "Finished" || order?.Status === "Returning") &&
<Button onClick={() => cancel(OrderId, cmt!)}>
<Button
onClick={() => cancel(OrderId, cmt!)}
disabled={submit}
>
{
Admin
? "Force Close"
Expand All @@ -109,7 +107,11 @@ export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderRef)
</Button>
}

<Button appearance="primary" onClick={() => append(OrderId, cmt!)}>
<Button
appearance="primary"
onClick={() => append(OrderId, cmt!)}
disabled={loading}
>
Add Comment
</Button>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Order/Comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface IComment {
*/
export function OrderComment({ OrderId, Admin, ParentLog }: IOrderComp) {
const log = useConst(() => ParentLog.With("Comment"));
const { data, run } = Hub.Order.Get.useCmts(OrderId, log, Admin);
const { data, refresh } = Hub.Order.Get.useCmts(OrderId, log, Admin);

return <>
<Field label="Comment" size="large">
Expand All @@ -38,6 +38,6 @@ export function OrderComment({ OrderId, Admin, ParentLog }: IOrderComp) {
)}
</Field>

<CommentAppend OrderId={OrderId} Refresh={run} ParentLog={log} Admin={Admin} />
<CommentAppend OrderId={OrderId} Refresh={refresh} ParentLog={log} Admin={Admin} />
</>;
}
34 changes: 7 additions & 27 deletions src/Components/Order/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { Body1Strong, Caption1, DataGridCell, DataGridHeaderCell, Link, TableColumnDefinition, createTableColumn, makeStyles, tokens } from "@fluentui/react-components";
import { useConst } from "@fluentui/react-hooks";
import { DelegateDataGrid } from "~/Components/DataGrid";
import { OrderComment } from "~/Components/Order/Comment";
import { OrderInfo } from "~/Components/Order/Info";
import { ICartItem } from "~/Components/ShopCart";
import { MakeCoverCol } from "~/Helpers/CoverCol";
import { ColFlex } from "~/Helpers/Styles";
import { useSWR } from "~/Helpers/useSWR";
import { AdminOrderAction } from "~/Pages/Admin/Order/Action";
import { AdminOrderList } from "~/Pages/Admin/Order/List";
import { Shipment } from "~/Pages/Admin/Order/Ship";
import { Hub } from "~/ShopNet";
import { AdminHub } from "~/ShopNet/Admin";
import { SignalR } from "~/ShopNet/SignalR";
import { IOrderComp } from ".";
import { OrderAction } from "../../Pages/History/Action";
import { useRouter } from "../Router";

/**
* @author Aloento
Expand All @@ -25,7 +20,8 @@ import { useRouter } from "../Router";
const useStyles = makeStyles({
body: {
...ColFlex,
rowGap: tokens.spacingVerticalL
rowGap: tokens.spacingVerticalL,
paddingBottom: tokens.spacingVerticalXXL
},
prod: {
...ColFlex,
Expand Down Expand Up @@ -82,38 +78,22 @@ const columns: TableColumnDefinition<ICartItem>[] = [
/**
* @author Aloento
* @since 1.3.5
* @version 1.3.0
* @version 1.4.0
*/
export function OrderDetailDrawer({ OrderId, Admin, ParentLog }: IOrderComp) {
const style = useStyles();

const { Nav } = useRouter();
const index = useConst(() => SignalR.Index(OrderId, Hub.Order.Get.order));

const { data: order, run } = useSWR(
index,
() => (Admin ? AdminHub : Hub).Order.Get.Order(OrderId),
{
onError(e) {
Nav(Admin ? "Admin/Order" : "History");
ParentLog.error(e);
},
useMemory: true
}
);

const { data: cart } = Hub.Order.Get.useItems(OrderId, ParentLog, Admin);

return (
<div className={style.body}>
<OrderInfo OrderId={OrderId} Order={order} Admin={Admin} ParentLog={ParentLog} />
<OrderInfo OrderId={OrderId} Admin={Admin} ParentLog={ParentLog} />

{
Admin
?
<>
<AdminOrderList Items={cart} />
<Shipment OrderId={OrderId} TrackingNumber={order?.TrackingNumber} Refresh={run} />
<Shipment OrderId={OrderId} />
</>
:
<DelegateDataGrid
Expand All @@ -127,9 +107,9 @@ export function OrderDetailDrawer({ OrderId, Admin, ParentLog }: IOrderComp) {
{
Admin
?
<AdminOrderAction OrderId={OrderId} Status={order?.Status} Refresh={run} ParentLog={ParentLog} />
<AdminOrderAction OrderId={OrderId} />
:
<OrderAction OrderId={OrderId} Status={order?.Status} Refresh={run} ParentLog={ParentLog} />
<OrderAction OrderId={OrderId} />
}
</div>
);
Expand Down
25 changes: 15 additions & 10 deletions src/Components/Order/Info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { useRequest } from "ahooks";
import { ColFlex, Flex } from "~/Helpers/Styles";
import { Hub } from "~/ShopNet";
import { AdminHub } from "~/ShopNet/Admin";
import type { OrderEntity } from "~/ShopNet/Order/Entity";
import type { IOrderComp } from ".";
import { useRouter } from "../Router";
import { useOrder } from "./useOrder";

/**
* @author Aloento
Expand All @@ -21,19 +22,23 @@ const useStyles = makeStyles({
},
});

interface IOrderInfo extends IOrderComp {
Order?: OrderEntity.Order;
}

/**
* @author Aloento
* @since 0.5.0
* @version 1.0.0
* @version 1.1.0
*/
export function OrderInfo({ OrderId, Order, Admin, ParentLog }: IOrderInfo) {
export function OrderInfo({ OrderId, Admin, ParentLog }: IOrderComp) {
const log = useConst(() => ParentLog.With("Info"));
const style = useStyles();

const { Nav } = useRouter();
const { data: order } = useOrder(OrderId, Admin, {
onError(e) {
Nav(Admin ? "Admin/Order" : "History");
ParentLog.error(e);
}
});

const { data: admin } = useRequest(() => AdminHub.User.Get.OrderUser(OrderId), {
manual: !Admin,
onError: log.error
Expand Down Expand Up @@ -61,13 +66,13 @@ export function OrderInfo({ OrderId, Order, Admin, ParentLog }: IOrderInfo) {
<div className={style.flex}>
<div className={style.box}>
<Field label="Order Date" size="large">
<Label>{Order?.CreateAt.toLocaleDateString()}</Label>
<Label>{order?.CreateAt.toLocaleDateString()}</Label>
</Field>
</div>

<div className={style.box}>
<Field label="Status" size="large">
<Label>{Order?.Status}</Label>
<Label>{order?.Status}</Label>
</Field>
</div>
</div>
Expand All @@ -83,7 +88,7 @@ export function OrderInfo({ OrderId, Order, Admin, ParentLog }: IOrderInfo) {
!Admin &&
<div className={style.box}>
<Field label="Tracking Number" size="large">
<Label>{Order?.TrackingNumber}</Label>
<Label>{order?.TrackingNumber}</Label>
</Field>
</div>
}
Expand Down
9 changes: 0 additions & 9 deletions src/Components/Order/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ export interface IOrderComp extends ICompLog {
Admin?: true;
}

/**
* @author Aloento
* @since 0.5.0
* @version 0.2.0
*/
export interface IOrderRef extends IOrderComp {
Refresh: () => void;
}

/**
* @author Aloento
* @since 0.5.0
Expand Down
25 changes: 25 additions & 0 deletions src/Components/Order/useOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useConst } from "@fluentui/react-hooks";
import { Options } from "ahooks/lib/useRequest/src/types";
import { useSWR } from "~/Helpers/useSWR";
import { Hub } from "~/ShopNet";
import { AdminHub } from "~/ShopNet/Admin";
import { OrderEntity } from "~/ShopNet/Order/Entity";
import { SignalR } from "~/ShopNet/SignalR";

/**
* @author Aloento
* @since 1.3.5
* @version 0.1.0
*/
export function useOrder(orderId: number, admin?: true, options?: Options<OrderEntity.Order, []>) {
const index = useConst(() => SignalR.Index(orderId, Hub.Order.Get.order));

return useSWR(
index,
() => (admin ? AdminHub : Hub).Order.Get.Order(orderId),
{
...options,
useMemory: true
}
);
}
Loading

0 comments on commit a763c73

Please sign in to comment.