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

Commit 842939a

Browse files
authored
[Back] OrderPostClose / Cancel: string - [Front] useOrder replace IOrderRef | useSWR refresh | mutate (#109)
* Update OrderGetItems and OrderGetCmts methods * Update Nav function calls in Order components * Update order handling methods and components 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.
1 parent a5be856 commit 842939a

File tree

19 files changed

+160
-141
lines changed

19 files changed

+160
-141
lines changed

SoarCraft.AwaiShop/AdminHub/Order/Post.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ await this.Db.Comments.AddAsync(new() {
4141
* <remarks>
4242
* @author Aloento
4343
* @since 0.5.0
44-
* @version 1.2.0
44+
* @version 1.3.0
4545
* </remarks>
4646
*/
47-
public async Task<bool> OrderPostClose(uint orderId, string reason) {
47+
public async Task<string> OrderPostClose(uint orderId, string reason) {
4848
var valid = typeof(Comment)
4949
.GetProperty(nameof(Comment.Content))!
5050
.GetCustomAttribute<StringLengthAttribute>()!;
@@ -72,7 +72,9 @@ await this.Db.Comments.AddAsync(new() {
7272
Order = order,
7373
});
7474

75-
return await this.Db.SaveChangesAsync() > 0;
75+
await this.Db.SaveChangesAsync();
76+
77+
return order.Status.ToString();
7678
}
7779

7880
/**

SoarCraft.AwaiShop/Hub/Order/Delete.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal partial class ShopHub {
1313
* </remarks>
1414
*/
1515
[Authorize]
16-
public async Task<bool> OrderDeleteCancel(uint orderId) {
16+
public async Task<bool> OrderDeleteCancelled(uint orderId) {
1717
var row = await this.Db.Orders
1818
.Where(x => x.UserId == this.UserId)
1919
.Where(x => x.OrderId == orderId)

SoarCraft.AwaiShop/Hub/Order/Get.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ await this.Db.Orders
3232
[Authorize]
3333
public async Task<dynamic> OrderGetItems(uint orderId) =>
3434
await this.Db.OrderCombos
35-
.Where(x => x.Order.UserId == this.UserId)
3635
.Where(x => x.OrderId == orderId)
36+
.Where(x => x.Order.UserId == this.UserId)
3737
.Select(x => new {
3838
x.Quantity,
3939
Types = x.Combo.Types.Select(t => t.TypeId).ToArray()
@@ -50,8 +50,8 @@ await this.Db.OrderCombos
5050
[Authorize]
5151
public Task<uint[]> OrderGetCmts(uint orderId) =>
5252
this.Db.Comments
53-
.Where(x => x.Order.UserId == this.UserId)
5453
.Where(x => x.OrderId == orderId)
54+
.Where(x => x.Order.UserId == this.UserId)
5555
.Select(x => x.CommentId)
5656
.ToArrayAsync();
5757
}

SoarCraft.AwaiShop/Hub/Order/Post.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ await this.Db.Comments.AddAsync(new() {
105105
* <remarks>
106106
* @author Aloento
107107
* @since 0.5.0
108-
* @version 1.2.0
108+
* @version 1.3.0
109109
* </remarks>
110110
*/
111111
[Authorize]
112-
public async Task<bool> OrderPostCancel(uint orderId, string reason) {
112+
public async Task<string> OrderPostCancel(uint orderId, string reason) {
113113
var valid = typeof(Comment)
114114
.GetProperty(nameof(Comment.Content))!
115115
.GetCustomAttribute<StringLengthAttribute>()!;
@@ -118,8 +118,8 @@ public async Task<bool> OrderPostCancel(uint orderId, string reason) {
118118
throw new HubException(valid.FormatErrorMessage("Reason"));
119119

120120
var order = await this.Db.Orders
121-
.Where(x => x.UserId == this.UserId)
122121
.Where(x => x.OrderId == orderId)
122+
.Where(x => x.UserId == this.UserId)
123123
.Where(x => x.Status != OrderStatus.Cancelled)
124124
.Where(x => x.Status != OrderStatus.Finished)
125125
.Include(x => x.OrderCombos)
@@ -139,7 +139,9 @@ await this.Db.Comments.AddAsync(new() {
139139
Order = order
140140
});
141141

142-
return await this.Db.SaveChangesAsync() > 0;
142+
await this.Db.SaveChangesAsync();
143+
144+
return order.Status.ToString();
143145
}
144146

145147
/**

SoarCraft.AwaiShop/SoarCraft.AwaiShop.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3939
</PackageReference>
4040
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.0" />
41-
<PackageReference Include="Microsoft.Identity.Web" Version="2.16.1" />
41+
<PackageReference Include="Microsoft.Identity.Web" Version="2.17.0" />
4242
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
4343
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.2" />
44-
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="8.101.2.1" />
44+
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="8.102.1" />
4545
</ItemGroup>
4646

4747
<ItemGroup>

src/Components/Order/Append.tsx

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import { Button, Field, Textarea, Toast, ToastTitle, makeStyles } from "@fluentu
22
import { useConst } from "@fluentui/react-hooks";
33
import { useState } from "react";
44
import { Flex } from "~/Helpers/Styles";
5-
import { useSWR } from "~/Helpers/useSWR";
65
import { useErrorToast } from "~/Helpers/useToast";
76
import { Hub } from "~/ShopNet";
87
import { AdminHub } from "~/ShopNet/Admin";
9-
import { SignalR } from "~/ShopNet/SignalR";
10-
import { IOrderRef } from ".";
8+
import { IOrderComp } from ".";
9+
import { useOrder } from "./useOrder";
1110

1211
/**
1312
* @author Aloento
@@ -24,19 +23,17 @@ const useStyles = makeStyles({
2423
/**
2524
* @author Aloento
2625
* @since 0.5.0
27-
* @version 1.2.0
26+
* @version 1.3.0
2827
*/
29-
export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderRef) {
28+
export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderComp & { Refresh: () => void }) {
3029
const log = useConst(() => ParentLog.With("Append"));
3130

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

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

37-
const hub = (Admin ? AdminHub : Hub).Order.Post as typeof AdminHub.Order.Post & typeof Hub.Order.Post;
38-
39-
const { run: append } = hub.useAppend({
36+
const { run: append, loading } = (Admin ? AdminHub : Hub).Order.Post.useAppend({
4037
manual: true,
4138
onError(e, req) {
4239
dispatch({
@@ -54,10 +51,13 @@ export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderRef)
5451
);
5552

5653
Refresh();
54+
setCmt("");
5755
}
5856
});
5957

60-
const { run: cancel } = (Admin ? hub.useClose : hub.useCancel)({
58+
const { data: order, mutate } = useOrder(OrderId, Admin);
59+
60+
const { run: cancel, loading: submit } = (Admin ? AdminHub : Hub).Order.Post.useCancel({
6161
manual: true,
6262
onError(e, params) {
6363
dispatch({
@@ -66,26 +66,21 @@ export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderRef)
6666
Error: e
6767
});
6868
},
69-
onSuccess() {
69+
onSuccess(data) {
7070
dispatchToast(
7171
<Toast>
7272
<ToastTitle>Order {Admin ? "Closed" : "Cancelled"}</ToastTitle>
7373
</Toast>,
7474
{ intent: "success" }
7575
);
7676

77-
Refresh();
77+
mutate((old) => ({
78+
...old!,
79+
Status: data
80+
}));
7881
}
7982
});
8083

81-
const index = useConst(() => SignalR.Index(OrderId, Hub.Order.Get.order));
82-
83-
const { data: order } = useSWR(
84-
index,
85-
() => (Admin ? AdminHub : Hub).Order.Get.Order(OrderId),
86-
{ useMemory: true }
87-
);
88-
8984
switch (order?.Status) {
9085
case "Cancelled":
9186
case "Finished":
@@ -100,7 +95,10 @@ export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderRef)
10095
<div className={style.body}>
10196
{
10297
!(order?.Status === "Finished" || order?.Status === "Returning") &&
103-
<Button onClick={() => cancel(OrderId, cmt!)}>
98+
<Button
99+
onClick={() => cancel(OrderId, cmt!)}
100+
disabled={submit}
101+
>
104102
{
105103
Admin
106104
? "Force Close"
@@ -109,7 +107,11 @@ export function CommentAppend({ OrderId, Refresh, Admin, ParentLog }: IOrderRef)
109107
</Button>
110108
}
111109

112-
<Button appearance="primary" onClick={() => append(OrderId, cmt!)}>
110+
<Button
111+
appearance="primary"
112+
onClick={() => append(OrderId, cmt!)}
113+
disabled={loading}
114+
>
113115
Add Comment
114116
</Button>
115117
</div>

src/Components/Order/Comment.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface IComment {
2222
*/
2323
export function OrderComment({ OrderId, Admin, ParentLog }: IOrderComp) {
2424
const log = useConst(() => ParentLog.With("Comment"));
25-
const { data, run } = Hub.Order.Get.useCmts(OrderId, log, Admin);
25+
const { data, refresh } = Hub.Order.Get.useCmts(OrderId, log, Admin);
2626

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

41-
<CommentAppend OrderId={OrderId} Refresh={run} ParentLog={log} Admin={Admin} />
41+
<CommentAppend OrderId={OrderId} Refresh={refresh} ParentLog={log} Admin={Admin} />
4242
</>;
4343
}

src/Components/Order/Drawer.tsx

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import { Body1Strong, Caption1, DataGridCell, DataGridHeaderCell, Link, TableColumnDefinition, createTableColumn, makeStyles, tokens } from "@fluentui/react-components";
2-
import { useConst } from "@fluentui/react-hooks";
32
import { DelegateDataGrid } from "~/Components/DataGrid";
43
import { OrderComment } from "~/Components/Order/Comment";
54
import { OrderInfo } from "~/Components/Order/Info";
65
import { ICartItem } from "~/Components/ShopCart";
76
import { MakeCoverCol } from "~/Helpers/CoverCol";
87
import { ColFlex } from "~/Helpers/Styles";
9-
import { useSWR } from "~/Helpers/useSWR";
108
import { AdminOrderAction } from "~/Pages/Admin/Order/Action";
119
import { AdminOrderList } from "~/Pages/Admin/Order/List";
1210
import { Shipment } from "~/Pages/Admin/Order/Ship";
1311
import { Hub } from "~/ShopNet";
14-
import { AdminHub } from "~/ShopNet/Admin";
15-
import { SignalR } from "~/ShopNet/SignalR";
1612
import { IOrderComp } from ".";
1713
import { OrderAction } from "../../Pages/History/Action";
18-
import { useRouter } from "../Router";
1914

2015
/**
2116
* @author Aloento
@@ -25,7 +20,8 @@ import { useRouter } from "../Router";
2520
const useStyles = makeStyles({
2621
body: {
2722
...ColFlex,
28-
rowGap: tokens.spacingVerticalL
23+
rowGap: tokens.spacingVerticalL,
24+
paddingBottom: tokens.spacingVerticalXXL
2925
},
3026
prod: {
3127
...ColFlex,
@@ -82,38 +78,22 @@ const columns: TableColumnDefinition<ICartItem>[] = [
8278
/**
8379
* @author Aloento
8480
* @since 1.3.5
85-
* @version 1.3.0
81+
* @version 1.4.0
8682
*/
8783
export function OrderDetailDrawer({ OrderId, Admin, ParentLog }: IOrderComp) {
8884
const style = useStyles();
89-
90-
const { Nav } = useRouter();
91-
const index = useConst(() => SignalR.Index(OrderId, Hub.Order.Get.order));
92-
93-
const { data: order, run } = useSWR(
94-
index,
95-
() => (Admin ? AdminHub : Hub).Order.Get.Order(OrderId),
96-
{
97-
onError(e) {
98-
Nav("History");
99-
ParentLog.error(e);
100-
},
101-
useMemory: true
102-
}
103-
);
104-
10585
const { data: cart } = Hub.Order.Get.useItems(OrderId, ParentLog, Admin);
10686

10787
return (
10888
<div className={style.body}>
109-
<OrderInfo OrderId={OrderId} Order={order} Admin={Admin} ParentLog={ParentLog} />
89+
<OrderInfo OrderId={OrderId} Admin={Admin} ParentLog={ParentLog} />
11090

11191
{
11292
Admin
11393
?
11494
<>
11595
<AdminOrderList Items={cart} />
116-
<Shipment OrderId={OrderId} TrackingNumber={order?.TrackingNumber} Refresh={run} />
96+
<Shipment OrderId={OrderId} />
11797
</>
11898
:
11999
<DelegateDataGrid
@@ -127,9 +107,9 @@ export function OrderDetailDrawer({ OrderId, Admin, ParentLog }: IOrderComp) {
127107
{
128108
Admin
129109
?
130-
<AdminOrderAction OrderId={OrderId} Status={order?.Status} Refresh={run} ParentLog={ParentLog} />
110+
<AdminOrderAction OrderId={OrderId} />
131111
:
132-
<OrderAction OrderId={OrderId} Status={order?.Status} Refresh={run} ParentLog={ParentLog} />
112+
<OrderAction OrderId={OrderId} />
133113
}
134114
</div>
135115
);

src/Components/Order/Info.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { useRequest } from "ahooks";
44
import { ColFlex, Flex } from "~/Helpers/Styles";
55
import { Hub } from "~/ShopNet";
66
import { AdminHub } from "~/ShopNet/Admin";
7-
import type { OrderEntity } from "~/ShopNet/Order/Entity";
87
import type { IOrderComp } from ".";
8+
import { useRouter } from "../Router";
9+
import { useOrder } from "./useOrder";
910

1011
/**
1112
* @author Aloento
@@ -21,19 +22,23 @@ const useStyles = makeStyles({
2122
},
2223
});
2324

24-
interface IOrderInfo extends IOrderComp {
25-
Order?: OrderEntity.Order;
26-
}
27-
2825
/**
2926
* @author Aloento
3027
* @since 0.5.0
31-
* @version 1.0.0
28+
* @version 1.1.0
3229
*/
33-
export function OrderInfo({ OrderId, Order, Admin, ParentLog }: IOrderInfo) {
30+
export function OrderInfo({ OrderId, Admin, ParentLog }: IOrderComp) {
3431
const log = useConst(() => ParentLog.With("Info"));
3532
const style = useStyles();
3633

34+
const { Nav } = useRouter();
35+
const { data: order } = useOrder(OrderId, Admin, {
36+
onError(e) {
37+
Nav(Admin ? "Admin/Order" : "History");
38+
ParentLog.error(e);
39+
}
40+
});
41+
3742
const { data: admin } = useRequest(() => AdminHub.User.Get.OrderUser(OrderId), {
3843
manual: !Admin,
3944
onError: log.error
@@ -61,13 +66,13 @@ export function OrderInfo({ OrderId, Order, Admin, ParentLog }: IOrderInfo) {
6166
<div className={style.flex}>
6267
<div className={style.box}>
6368
<Field label="Order Date" size="large">
64-
<Label>{Order?.CreateAt.toLocaleDateString()}</Label>
69+
<Label>{order?.CreateAt.toLocaleDateString()}</Label>
6570
</Field>
6671
</div>
6772

6873
<div className={style.box}>
6974
<Field label="Status" size="large">
70-
<Label>{Order?.Status}</Label>
75+
<Label>{order?.Status}</Label>
7176
</Field>
7277
</div>
7378
</div>
@@ -83,7 +88,7 @@ export function OrderInfo({ OrderId, Order, Admin, ParentLog }: IOrderInfo) {
8388
!Admin &&
8489
<div className={style.box}>
8590
<Field label="Tracking Number" size="large">
86-
<Label>{Order?.TrackingNumber}</Label>
91+
<Label>{order?.TrackingNumber}</Label>
8792
</Field>
8893
</div>
8994
}

0 commit comments

Comments
 (0)