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

Commit 838d284

Browse files
authored
[Back] Use TypeId | ProductGetTypes | ProductGetVariants | ProductPatchType | GuestVisit - [Front] AdminProductComboDetail | AdminProductNewCombo | useVariants | useTypes | useTypeList | useVariantName | useType | useVariant (#113)
* Updated logging, deprecated function, and version changes Updated `LoggerExtension.cs` to include a new logging method for guest visits and incremented its version to 0.2.0. `ShopHub.cs` now calls this new logging method when a guest visits the shop and its version has been updated to 0.1.1. The application version in `package.json` has been updated from 1.4.0 to 1.4.5. Marked the `refreshVariant` function in `index.tsx` as deprecated. * Updated README, modified imports, and improved state management Updated the README.md file to include a project description and requirements. Modified the import statement in Ship.tsx to include useEffect from the 'react' library. Updated the version annotation in the Shipment function. Changed the initial useState hook for 'track' to an empty string and added a useEffect hook to update 'track' state when 'order' changes. * Updated methods and return types in CRUD files In this commit, several changes have been made to the CRUD files. In `Delete.cs`, `Get.cs`, `Patch.cs`, and `Post.cs`, methods have been updated to take or return different parameters or types. Specifically, `ProductDeleteType`, `ProductPatchType`, `ProductGetVariants`, `ProductPostVariant`, and `ProductPostType` have been modified. The query filters in `Delete.cs` and `Patch.cs` have also been updated. A new method, `ProductGetTypes`, has been added to `Get.cs`. Lastly, version numbers in the comments have been updated across all files. * Refactor methods and update classes in product-related files Several classes in product-related files have been updated. In `Delete.ts`, `Get.ts`, `Patch.ts`, `Post.ts`, and `Data.ts`, methods like `useVariant`, `useType`, and `useRequest` have been refactored to take in different parameters. The cache time in `useList` method in `Get.ts` has been changed from 1 minute to 5 seconds. Deprecated `Variants` method in `Get.ts` has been replaced with `useVariants`, `useTypes`, and `useTypeList`. The `mutate` function in `Patch.ts` and `Post.ts` is used to update the name or list of variants or types. The `Variant` type in `Data.ts` now includes `TypeIds`. New methods `useType` and `useVariant` have been added in `Data.ts` to get the type and variant data. The `usePhotoList` method in `Get.ts` now takes an array of numbers as default parameters. * Summary: Updated UI and fixed bugs in login module Full Summary: This commit includes significant updates to the user interface, enhancing the overall user experience. Additionally, several bugs in the login module have been identified and fixed. These bugs were causing login failures for some users. The changes should improve the stability and reliability of the login process. * Replace `useRequest` with `useAsyncEffect` and refactor interfaces Replaced `useRequest` hook with `useAsyncEffect` in `Detail.tsx`, `New.tsx`, `Delete.tsx`, and `index.tsx` for better handling of asynchronous operations. Added `Hub` import from `~/ShopNet` to several files. Introduced `IUpdateComboItem` interface in `Detail.tsx` and `New.tsx`, extending `IVariantItem` interface. Moved `IVariantItem` interface from `index.tsx` to `New.tsx`. Updated functions in `Detail.tsx` and `New.tsx` to use new hook and interface. Updated `AdminProductVariantDelete` function in `Delete.tsx` to use `useVariant` hook. Refactored `AdminProductGet` class in `Get.ts` by removing `Variants` method and adding `Types` method. Removed `TypeIds` property from `Variant` type in `Data.ts`.
1 parent eeb956f commit 838d284

File tree

24 files changed

+441
-194
lines changed

24 files changed

+441
-194
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## AwaiShop
2+
3+
A Simple E-Commerce System with C# & SignalR & FluentUI3 & AzureAD
4+
5+
## Basic Requirements
6+
7+
- .NET 8
8+
- Node.js 17+
9+
- Visual Studio 2022 Preview
10+
- Visual Studio Code

SoarCraft.AwaiShop/AdminHub/Product/Delete.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ private async Task deleteType(Type type) {
106106
* <remarks>
107107
* @author Aloento
108108
* @since 0.1.0
109-
* @version 0.2.0
109+
* @version 0.3.0
110110
* </remarks>
111111
*/
112-
public async Task<bool> ProductDeleteType(uint variantId, string reqType) {
112+
public async Task<bool> ProductDeleteType(uint typeId) {
113113
await this.deleteType(
114114
await this.Db.Types
115-
.Where(x => x.VariantId == variantId && x.Name == reqType)
115+
.Where(x => x.TypeId == typeId)
116116
.IncludeOptimized(x => x.Combos)
117117
.SingleAsync()
118118
);

SoarCraft.AwaiShop/AdminHub/Product/Get.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,25 @@ await this.Db.Products
3636
* <remarks>
3737
* @author Aloento
3838
* @since 0.1.0
39-
* @version 1.0.0
39+
* @version 1.1.0
4040
* </remarks>
4141
*/
42-
public async Task<dynamic[]> ProductGetVariants(uint prodId) =>
43-
await this.Db.Variants
42+
public Task<uint[]> ProductGetVariants(uint prodId) =>
43+
this.Db.Variants
4444
.Where(x => x.ProductId == prodId)
45-
.Select(x => new {
46-
x.VariantId,
47-
Types = x.Types.Select(t => t.TypeId).ToArray()
48-
})
45+
.Select(x => x.VariantId)
46+
.ToArrayAsync();
47+
48+
/**
49+
* <remarks>
50+
* @author Aloento
51+
* @since 1.3.0
52+
* @version 0.1.0
53+
* </remarks>
54+
*/
55+
public Task<uint[]> ProductGetTypes(uint variantId) =>
56+
this.Db.Types
57+
.Where(x => x.VariantId == variantId)
58+
.Select(x => x.TypeId)
4959
.ToArrayAsync();
5060
}

SoarCraft.AwaiShop/AdminHub/Product/Patch.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ private async Task<List<Combo>> archiveCombos(ICollection<Combo> oldCombos) {
228228
* <remarks>
229229
* @author Aloento
230230
* @since 0.1.0
231-
* @version 1.0.0
231+
* @version 1.1.0
232232
* </remarks>
233233
*/
234-
public async Task<bool> ProductPatchType(uint variantId, string oldName, string newName) {
234+
public async Task<bool> ProductPatchType(uint typeId, string newName) {
235235
var valid = typeof(Type)
236236
.GetProperty(nameof(Type.Name))!
237237
.GetCustomAttribute<StringLengthAttribute>()!;
@@ -240,7 +240,7 @@ public async Task<bool> ProductPatchType(uint variantId, string oldName, string
240240
throw new HubException(valid.FormatErrorMessage("Name"));
241241

242242
var type = this.Db.Types
243-
.Where(x => x.VariantId == variantId && x.Name == oldName);
243+
.Where(x => x.TypeId == typeId);
244244

245245
var any = await type
246246
.SelectMany(x => x.Combos)

SoarCraft.AwaiShop/AdminHub/Product/Post.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public async Task<uint> ProductPostPhoto(uint prodId, IAsyncEnumerable<byte[]> i
110110
* <remarks>
111111
* @author Aloento
112112
* @since 0.5.0
113-
* @version 0.1.0
113+
* @version 0.1.1
114114
* </remarks>
115115
*/
116116
public async Task<uint> ProductPostVariant(uint prodId, string name) {
@@ -138,14 +138,14 @@ public async Task<uint> ProductPostVariant(uint prodId, string name) {
138138
});
139139

140140
await this.Db.SaveChangesAsync();
141-
return temp.Entity.ProductId;
141+
return temp.Entity.VariantId;
142142
}
143143

144144
/**
145145
* <remarks>
146146
* @author Aloento
147147
* @since 0.5.0
148-
* @version 0.1.0
148+
* @version 0.1.1
149149
* </remarks>
150150
*/
151151
public async Task<uint> ProductPostType(uint variantId, string name) {
@@ -173,7 +173,7 @@ public async Task<uint> ProductPostType(uint variantId, string name) {
173173
});
174174

175175
await this.Db.SaveChangesAsync();
176-
return temp.Entity.VariantId;
176+
return temp.Entity.TypeId;
177177
}
178178

179179
/**

SoarCraft.AwaiShop/Helpers/LoggerExtension.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ namespace SoarCraft.AwaiShop.Helpers;
66
* <remarks>
77
* @author Aloento
88
* @since 0.5.0
9-
* @version 0.1.0
9+
* @version 0.2.0
1010
* </remarks>
1111
*/
1212
internal static partial class LoggerExtension {
1313
[LoggerMessage(
1414
EventId = 1001,
1515
Level = LogLevel.Debug,
16+
Message = "Guest : Visit from [{ip}]"
17+
)]
18+
private static partial void guestVisit(ILogger logger, string? ip);
19+
20+
public static void GuestVisit(this ILogger logger, HubCallerContext ctx) =>
21+
guestVisit(logger, ctx.GetHttpContext()?.Connection.RemoteIpAddress?.ToString());
22+
23+
[LoggerMessage(
24+
EventId = 2001,
25+
Level = LogLevel.Information,
1626
Message = "User {name} : [{uid}] Logged from [{ip}]"
1727
)]
1828
private static partial void userLogin(ILogger logger, string? name, string? uid, string? ip);

SoarCraft.AwaiShop/Hub/ShopHub.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal partial class ShopHub(ShopContext db, ILogger<ShopHub> logger) : CraftH
1818
* <remarks>
1919
* @author Aloento
2020
* @since 0.5.0
21-
* @version 0.1.0
21+
* @version 0.1.1
2222
* </remarks>
2323
*/
2424
public override async Task OnConnectedAsync() {
@@ -34,7 +34,8 @@ public override async Task OnConnectedAsync() {
3434
await this.Clients.Caller.OnNewUser();
3535
this.Context.Items.TryAdd("NewUser", true);
3636
}
37-
}
37+
} else
38+
this.Logger.GuestVisit(this.Context);
3839
}
3940

4041
/**

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "awaishop",
33
"private": true,
4-
"version": "1.4.0",
4+
"version": "1.4.5",
55
"type": "module",
66
"author": {
77
"name": "Aloento",

src/Pages/Admin/Order/Ship.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Button, Field, Input, Toast, ToastTitle } from "@fluentui/react-components";
22
import { EditRegular, SendRegular } from "@fluentui/react-icons";
33
import { useBoolean } from "ahooks";
4-
import { useState } from "react";
4+
import { useEffect, useState } from "react";
55
import { useOrder } from "~/Components/Order/useOrder";
66
import { Logger } from "~/Helpers/Logger";
77
import { useErrorToast } from "~/Helpers/useToast";
@@ -12,14 +12,18 @@ const log = new Logger("Admin", "Order", "Detail", "Shipment");
1212
/**
1313
* @author Aloento
1414
* @since 0.5.0
15-
* @version 0.3.0
15+
* @version 0.3.1
1616
*/
1717
export function Shipment({ OrderId }: { OrderId: number }) {
1818
const [edit, { setTrue, setFalse }] = useBoolean();
1919
const { dispatch, dispatchToast } = useErrorToast(log);
2020

2121
const { data: order, mutate } = useOrder(OrderId, true);
22-
const [track, setTrack] = useState(order?.TrackingNumber);
22+
const [track, setTrack] = useState<string>("");
23+
24+
useEffect(() => {
25+
order?.TrackingNumber && setTrack(order?.TrackingNumber);
26+
}, [order]);
2327

2428
const { run } = AdminHub.Order.Post.useShip({
2529
manual: true,

src/Pages/Admin/Product/Combo/Detail.tsx

+43-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { Button, Combobox, DataGridCell, DataGridHeaderCell, Dialog, DialogBody, DialogContent, DialogSurface, DialogTitle, DialogTrigger, Label, Option, SpinButton, TableColumnDefinition, Toast, ToastTitle, createTableColumn, makeStyles, tokens } from "@fluentui/react-components";
22
import { DismissRegular, EditRegular } from "@fluentui/react-icons";
3-
import { useBoolean, useRequest } from "ahooks";
3+
import { useAsyncEffect, useBoolean } from "ahooks";
44
import { useState } from "react";
55
import { DelegateDataGrid } from "~/Components/DataGrid";
66
import { Logger } from "~/Helpers/Logger";
77
import { Flex } from "~/Helpers/Styles";
88
import { useErrorToast } from "~/Helpers/useToast";
9+
import { Hub } from "~/ShopNet";
910
import { AdminHub } from "~/ShopNet/Admin";
1011
import { IComboItem } from ".";
11-
import { IVariantItem } from "../Variant";
12+
import { IUpdateComboItem, IVariantItem } from "./New";
1213

1314
/**
1415
* @author Aloento
1516
* @since 0.5.0
1617
* @version 0.1.0
1718
*/
18-
interface IEditComboItem extends IVariantItem {
19+
interface IEditComboItem extends IUpdateComboItem {
1920
Current: string;
20-
Update: (type: string) => void;
2121
}
2222

2323
/**
@@ -80,6 +80,7 @@ const useStyles = makeStyles({
8080
*/
8181
export interface IDetailComboItem extends IComboItem {
8282
ProdId: number;
83+
/** @deprecated */
8384
Refresh: () => void;
8485
}
8586

@@ -88,21 +89,48 @@ const log = new Logger("Admin", "Product", "Detail", "Combo", "Detail");
8889
/**
8990
* @author Aloento
9091
* @since 0.5.0
91-
* @version 0.2.3
92+
* @version 0.3.0
9293
*/
9394
export function AdminProductComboDetail({ Id, ProdId, Combo, Stock, Refresh }: IDetailComboItem) {
9495
const [open, { toggle }] = useBoolean();
9596
const [combo, setCombo] = useState(Combo);
9697
const [stock, setStock] = useState(Stock);
9798

98-
const { data: varis } = useRequest(() => AdminHub.Product.Get.Variants(ProdId, log), {
99+
const [varis, setVaris] = useState<IVariantItem[]>([]);
100+
const { data: varIds } = AdminHub.Product.Get.useVariants(ProdId, {
99101
onError: log.error
100102
});
101103

104+
useAsyncEffect(async () => {
105+
if (!varIds)
106+
return;
107+
108+
const varis: IVariantItem[] = [];
109+
110+
for (const i of varIds) {
111+
const typeIds = await AdminHub.Product.Get.Types(i);
112+
const types = [];
113+
114+
for (const typeId of typeIds) {
115+
const type = await Hub.Product.Get.Type(typeId);
116+
types.push(type);
117+
}
118+
119+
const { Name } = await Hub.Product.Get.Variant(i);
120+
121+
varis.push({
122+
Id: i,
123+
Name: Name,
124+
Types: types.map(x => x.Name)
125+
});
126+
}
127+
128+
setVaris(varis);
129+
}, [varIds]);
130+
102131
const { dispatch, dispatchToast } = useErrorToast(log);
103132

104-
const { run } = AdminHub.Product.Patch.useCombo({
105-
manual: true,
133+
const { run, loading } = AdminHub.Product.Patch.useCombo({
106134
onError(e, req) {
107135
dispatch({
108136
Message: "Failed Update Combo",
@@ -167,7 +195,13 @@ export function AdminProductComboDetail({ Id, ProdId, Combo, Stock, Refresh }: I
167195
setStock(val);
168196
}} />
169197

170-
<Button appearance="primary" onClick={() => run(Id, combo, stock)}>Submit</Button>
198+
<Button
199+
disabled={loading}
200+
appearance="primary"
201+
onClick={() => run(Id, combo, stock)}
202+
>
203+
Submit
204+
</Button>
171205
</div>
172206
</DialogContent>
173207
</DialogBody>

0 commit comments

Comments
 (0)