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

Commit

Permalink
[Back] Use TypeId | ProductGetTypes | ProductGetVariants | ProductPat…
Browse files Browse the repository at this point in the history
…chType | 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`.
  • Loading branch information
Aloento authored Mar 2, 2024
1 parent eeb956f commit 838d284
Show file tree
Hide file tree
Showing 24 changed files with 441 additions and 194 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## AwaiShop

A Simple E-Commerce System with C# & SignalR & FluentUI3 & AzureAD

## Basic Requirements

- .NET 8
- Node.js 17+
- Visual Studio 2022 Preview
- Visual Studio Code
6 changes: 3 additions & 3 deletions SoarCraft.AwaiShop/AdminHub/Product/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ private async Task deleteType(Type type) {
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.2.0
* @version 0.3.0
* </remarks>
*/
public async Task<bool> ProductDeleteType(uint variantId, string reqType) {
public async Task<bool> ProductDeleteType(uint typeId) {
await this.deleteType(
await this.Db.Types
.Where(x => x.VariantId == variantId && x.Name == reqType)
.Where(x => x.TypeId == typeId)
.IncludeOptimized(x => x.Combos)
.SingleAsync()
);
Expand Down
24 changes: 17 additions & 7 deletions SoarCraft.AwaiShop/AdminHub/Product/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,25 @@ await this.Db.Products
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 1.0.0
* @version 1.1.0
* </remarks>
*/
public async Task<dynamic[]> ProductGetVariants(uint prodId) =>
await this.Db.Variants
public Task<uint[]> ProductGetVariants(uint prodId) =>
this.Db.Variants
.Where(x => x.ProductId == prodId)
.Select(x => new {
x.VariantId,
Types = x.Types.Select(t => t.TypeId).ToArray()
})
.Select(x => x.VariantId)
.ToArrayAsync();

/**
* <remarks>
* @author Aloento
* @since 1.3.0
* @version 0.1.0
* </remarks>
*/
public Task<uint[]> ProductGetTypes(uint variantId) =>
this.Db.Types
.Where(x => x.VariantId == variantId)
.Select(x => x.TypeId)
.ToArrayAsync();
}
6 changes: 3 additions & 3 deletions SoarCraft.AwaiShop/AdminHub/Product/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ private async Task<List<Combo>> archiveCombos(ICollection<Combo> oldCombos) {
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 1.0.0
* @version 1.1.0
* </remarks>
*/
public async Task<bool> ProductPatchType(uint variantId, string oldName, string newName) {
public async Task<bool> ProductPatchType(uint typeId, string newName) {
var valid = typeof(Type)
.GetProperty(nameof(Type.Name))!
.GetCustomAttribute<StringLengthAttribute>()!;
Expand All @@ -240,7 +240,7 @@ public async Task<bool> ProductPatchType(uint variantId, string oldName, string
throw new HubException(valid.FormatErrorMessage("Name"));

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

var any = await type
.SelectMany(x => x.Combos)
Expand Down
8 changes: 4 additions & 4 deletions SoarCraft.AwaiShop/AdminHub/Product/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public async Task<uint> ProductPostPhoto(uint prodId, IAsyncEnumerable<byte[]> i
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 0.1.1
* </remarks>
*/
public async Task<uint> ProductPostVariant(uint prodId, string name) {
Expand Down Expand Up @@ -138,14 +138,14 @@ public async Task<uint> ProductPostVariant(uint prodId, string name) {
});

await this.Db.SaveChangesAsync();
return temp.Entity.ProductId;
return temp.Entity.VariantId;
}

/**
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 0.1.1
* </remarks>
*/
public async Task<uint> ProductPostType(uint variantId, string name) {
Expand Down Expand Up @@ -173,7 +173,7 @@ public async Task<uint> ProductPostType(uint variantId, string name) {
});

await this.Db.SaveChangesAsync();
return temp.Entity.VariantId;
return temp.Entity.TypeId;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion SoarCraft.AwaiShop/Helpers/LoggerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ namespace SoarCraft.AwaiShop.Helpers;
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
internal static partial class LoggerExtension {
[LoggerMessage(
EventId = 1001,
Level = LogLevel.Debug,
Message = "Guest : Visit from [{ip}]"
)]
private static partial void guestVisit(ILogger logger, string? ip);

public static void GuestVisit(this ILogger logger, HubCallerContext ctx) =>
guestVisit(logger, ctx.GetHttpContext()?.Connection.RemoteIpAddress?.ToString());

[LoggerMessage(
EventId = 2001,
Level = LogLevel.Information,
Message = "User {name} : [{uid}] Logged from [{ip}]"
)]
private static partial void userLogin(ILogger logger, string? name, string? uid, string? ip);
Expand Down
5 changes: 3 additions & 2 deletions SoarCraft.AwaiShop/Hub/ShopHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal partial class ShopHub(ShopContext db, ILogger<ShopHub> logger) : CraftH
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 0.1.1
* </remarks>
*/
public override async Task OnConnectedAsync() {
Expand All @@ -34,7 +34,8 @@ public override async Task OnConnectedAsync() {
await this.Clients.Caller.OnNewUser();
this.Context.Items.TryAdd("NewUser", true);
}
}
} else
this.Logger.GuestVisit(this.Context);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "awaishop",
"private": true,
"version": "1.4.0",
"version": "1.4.5",
"type": "module",
"author": {
"name": "Aloento",
Expand Down
10 changes: 7 additions & 3 deletions src/Pages/Admin/Order/Ship.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Field, Input, Toast, ToastTitle } from "@fluentui/react-components";
import { EditRegular, SendRegular } from "@fluentui/react-icons";
import { useBoolean } from "ahooks";
import { useState } from "react";
import { useEffect, useState } from "react";
import { useOrder } from "~/Components/Order/useOrder";
import { Logger } from "~/Helpers/Logger";
import { useErrorToast } from "~/Helpers/useToast";
Expand All @@ -12,14 +12,18 @@ const log = new Logger("Admin", "Order", "Detail", "Shipment");
/**
* @author Aloento
* @since 0.5.0
* @version 0.3.0
* @version 0.3.1
*/
export function Shipment({ OrderId }: { OrderId: number }) {
const [edit, { setTrue, setFalse }] = useBoolean();
const { dispatch, dispatchToast } = useErrorToast(log);

const { data: order, mutate } = useOrder(OrderId, true);
const [track, setTrack] = useState(order?.TrackingNumber);
const [track, setTrack] = useState<string>("");

useEffect(() => {
order?.TrackingNumber && setTrack(order?.TrackingNumber);
}, [order]);

const { run } = AdminHub.Order.Post.useShip({
manual: true,
Expand Down
52 changes: 43 additions & 9 deletions src/Pages/Admin/Product/Combo/Detail.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Button, Combobox, DataGridCell, DataGridHeaderCell, Dialog, DialogBody, DialogContent, DialogSurface, DialogTitle, DialogTrigger, Label, Option, SpinButton, TableColumnDefinition, Toast, ToastTitle, createTableColumn, makeStyles, tokens } from "@fluentui/react-components";
import { DismissRegular, EditRegular } from "@fluentui/react-icons";
import { useBoolean, useRequest } from "ahooks";
import { useAsyncEffect, useBoolean } from "ahooks";
import { useState } from "react";
import { DelegateDataGrid } from "~/Components/DataGrid";
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 { IComboItem } from ".";
import { IVariantItem } from "../Variant";
import { IUpdateComboItem, IVariantItem } from "./New";

/**
* @author Aloento
* @since 0.5.0
* @version 0.1.0
*/
interface IEditComboItem extends IVariantItem {
interface IEditComboItem extends IUpdateComboItem {
Current: string;
Update: (type: string) => void;
}

/**
Expand Down Expand Up @@ -80,6 +80,7 @@ const useStyles = makeStyles({
*/
export interface IDetailComboItem extends IComboItem {
ProdId: number;
/** @deprecated */
Refresh: () => void;
}

Expand All @@ -88,21 +89,48 @@ const log = new Logger("Admin", "Product", "Detail", "Combo", "Detail");
/**
* @author Aloento
* @since 0.5.0
* @version 0.2.3
* @version 0.3.0
*/
export function AdminProductComboDetail({ Id, ProdId, Combo, Stock, Refresh }: IDetailComboItem) {
const [open, { toggle }] = useBoolean();
const [combo, setCombo] = useState(Combo);
const [stock, setStock] = useState(Stock);

const { data: varis } = useRequest(() => AdminHub.Product.Get.Variants(ProdId, log), {
const [varis, setVaris] = useState<IVariantItem[]>([]);
const { data: varIds } = AdminHub.Product.Get.useVariants(ProdId, {
onError: log.error
});

useAsyncEffect(async () => {
if (!varIds)
return;

const varis: IVariantItem[] = [];

for (const i of varIds) {
const typeIds = await AdminHub.Product.Get.Types(i);
const types = [];

for (const typeId of typeIds) {
const type = await Hub.Product.Get.Type(typeId);
types.push(type);
}

const { Name } = await Hub.Product.Get.Variant(i);

varis.push({
Id: i,
Name: Name,
Types: types.map(x => x.Name)
});
}

setVaris(varis);
}, [varIds]);

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

const { run } = AdminHub.Product.Patch.useCombo({
manual: true,
const { run, loading } = AdminHub.Product.Patch.useCombo({
onError(e, req) {
dispatch({
Message: "Failed Update Combo",
Expand Down Expand Up @@ -167,7 +195,13 @@ export function AdminProductComboDetail({ Id, ProdId, Combo, Stock, Refresh }: I
setStock(val);
}} />

<Button appearance="primary" onClick={() => run(Id, combo, stock)}>Submit</Button>
<Button
disabled={loading}
appearance="primary"
onClick={() => run(Id, combo, stock)}
>
Submit
</Button>
</div>
</DialogContent>
</DialogBody>
Expand Down
Loading

0 comments on commit 838d284

Please sign in to comment.