Skip to content

Commit

Permalink
[Back | Admin] Split OrderGetDetail - [Front] useSWR | useMe | useCmt…
Browse files Browse the repository at this point in the history
…s | useTimeCache | OrderDetail | OrderDetailDrawer
  • Loading branch information
Aloento committed Feb 19, 2024
1 parent 12ca2c7 commit c4acd7e
Show file tree
Hide file tree
Showing 26 changed files with 549 additions and 635 deletions.
22 changes: 12 additions & 10 deletions TSystems.LoveOTC/AdminHub/Order/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,28 @@ await this.Db.Orders
* <remarks>
* @author Aloento
* @since 1.0.0
* @version 1.0.0
* @version 1.1.0
* </remarks>
*/
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)
.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.0.0
* @version 1.1.0
* </remarks>
*/
public Task<uint[]> OrderGetCmts(uint orderId) =>
this.Db.Comments
.Where(x => x.OrderId == orderId)
.Select(x => x.CommentId)
.ToArrayAsync();

return new {
Items = items,
Comments = cmts
};
}
}
6 changes: 4 additions & 2 deletions TSystems.LoveOTC/Hub/Order/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ await this.Db.Orders
[Authorize]
public async Task<dynamic> OrderGetItems(uint orderId) =>
await this.Db.OrderCombos
.Where(x => x.OrderId == orderId && x.Order.UserId == this.UserId)
.Where(x => x.Order.UserId == this.UserId)
.Where(x => x.OrderId == orderId)
.Select(x => new {
x.Quantity,
Types = x.Combo.Types.Select(t => t.TypeId).ToArray()
Expand All @@ -49,7 +50,8 @@ await this.Db.OrderCombos
[Authorize]
public Task<uint[]> OrderGetCmts(uint orderId) =>
this.Db.Comments
.Where(x => x.OrderId == orderId && x.Order.UserId == this.UserId)
.Where(x => x.Order.UserId == this.UserId)
.Where(x => x.OrderId == orderId)
.Select(x => x.CommentId)
.ToArrayAsync();
}
2 changes: 1 addition & 1 deletion src/Components/AvatarMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function AvatarMenu() {
OnNewUserSubject.subscribe(x => setMount(!x));
}, []);

const data = Hub.User.Get.useMe(log);
const { data } = Hub.User.Get.useMe(log);

const name = auth.user?.profile.preferred_username;

Expand Down
22 changes: 16 additions & 6 deletions src/Components/Order/Append.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ 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 { IOrderComp } from "./Comment";
import { SignalR } from "~/ShopNet/SignalR";
import { IOrderRef } from ".";

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

const style = useStyles();
Expand Down Expand Up @@ -76,7 +78,15 @@ export function CommentAppend({ OrderId, Refresh, ParentLog, Status, Admin }: IO
}
});

switch (Status) {
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":
return null;
Expand All @@ -89,12 +99,12 @@ export function CommentAppend({ OrderId, Refresh, ParentLog, Status, Admin }: IO

<div className={style.body}>
{
!(Status === "Finished" || Status === "Returning") &&
!(order?.Status === "Finished" || order?.Status === "Returning") &&
<Button onClick={() => cancel(OrderId, cmt!)}>
{
Admin
? "Force Close"
: Status === "Shipping" ? "Ask Return" : "Cancel Order"
: order?.Status === "Shipping" ? "Ask Return" : "Cancel Order"
} with Reason
</Button>
}
Expand Down
27 changes: 5 additions & 22 deletions src/Components/Order/Comment.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
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 { IOrderComp } from ".";
import { CommentAppend } from "./Append";

/**
Expand All @@ -16,30 +15,14 @@ export interface IComment {
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
* @version 1.1.0
*/
export function OrderComment({ OrderId, Refresh, ParentLog, Status, Admin }: IOrderComp) {
export function OrderComment({ OrderId, Admin, ParentLog }: IOrderComp) {
const log = useConst(() => ParentLog.With("Comment"));

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

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

<CommentAppend OrderId={OrderId} Status={Status} Refresh={run} ParentLog={log} />
<CommentAppend OrderId={OrderId} Refresh={run} ParentLog={log} Admin={Admin} />
</>;
}
136 changes: 136 additions & 0 deletions src/Components/Order/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
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
* @since 0.5.0
* @version 0.1.0
*/
const useStyles = makeStyles({
body: {
...ColFlex,
rowGap: tokens.spacingVerticalL
},
prod: {
...ColFlex,
alignItems: "flex-start",
justifyContent: "center",
},
qua: {
flexBasis: "10%",
flexGrow: 0,
justifyContent: "center"
},
});

/**
* @author Aloento
* @since 0.5.0
* @version 0.1.1
*/
const columns: TableColumnDefinition<ICartItem>[] = [
createTableColumn<ICartItem>({
columnId: "Product",
renderHeaderCell() {
return <DataGridHeaderCell>Product Name & Types</DataGridHeaderCell>;
},
renderCell(item) {
return (
<DataGridCell className={useStyles().prod}>
<Link href={`/Product/${item.ProdId}`} appearance="subtle">
<Body1Strong>{item.Name}</Body1Strong>
</Link>

<Caption1>
{Object.values(item.Type).reduce((prev, curr) => `${prev} ${curr},`, "")}
</Caption1>
</DataGridCell>
);
}
}),
createTableColumn<ICartItem>({
columnId: "Quantity",
renderHeaderCell() {
return <DataGridHeaderCell className={useStyles().qua}>Quantity</DataGridHeaderCell>;
},
renderCell(item) {
return (
<DataGridCell className={useStyles().qua}>
{item.Quantity}
</DataGridCell>
);
}
})
];

/**
* @author Aloento
* @since 1.3.5
* @version 1.3.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("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} />

{
Admin
?
<>
<AdminOrderList Items={cart} />
<Shipment OrderId={OrderId} TrackingNumber={order?.TrackingNumber} Refresh={run} />
</>
:
<DelegateDataGrid
Items={cart}
Columns={[MakeCoverCol(44, ParentLog), ...columns]}
/>
}

<OrderComment OrderId={OrderId} ParentLog={ParentLog} Admin={Admin} />

{
Admin
?
<AdminOrderAction OrderId={OrderId} Status={order?.Status} Refresh={run} ParentLog={ParentLog} />
:
<OrderAction OrderId={OrderId} Status={order?.Status} Refresh={run} ParentLog={ParentLog} />
}
</div>
);
}
23 changes: 8 additions & 15 deletions src/Components/OrderInfo.tsx → src/Components/Order/Info.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Field, Label, makeStyles, tokens } from "@fluentui/react-components";
import { useConst } from "@fluentui/react-hooks";
import { useRequest } from "ahooks";
import { Logger } from "~/Helpers/Logger";
import { ColFlex, Flex } from "~/Helpers/Styles";
import { Hub } from "~/ShopNet";
import { AdminHub } from "~/ShopNet/Admin";
import { OrderEntity } from "~/ShopNet/Order/Entity";
import type { OrderEntity } from "~/ShopNet/Order/Entity";
import type { IOrderComp } from ".";

/**
* @author Aloento
Expand All @@ -20,33 +21,25 @@ const useStyles = makeStyles({
},
});

/**
* @author Aloento
* @since 1.0.0
* @version 0.1.1
*/
interface IOrderInfo {
OrderId: number;
interface IOrderInfo extends IOrderComp {
Order?: OrderEntity.Order;
Admin?: true;
}

const log = new Logger("Order", "Info");

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

const { data: admin } = useRequest(() => AdminHub.User.Get.OrderUser(OrderId), {
manual: !Admin,
onError: log.error
});

const me = Hub.User.Get.useMe(log);
const { data: me } = Hub.User.Get.useMe(log, Admin);

const data = Admin ? admin : me;

Expand Down
Loading

0 comments on commit c4acd7e

Please sign in to comment.