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

Commit

Permalink
[Front | Fix] Infinite ErrorToast due to NewUser (#94)
Browse files Browse the repository at this point in the history
* The most significant changes include the modification of the `AdminHub.cs` file to remove the `System` namespace import and the update of the `Delete.cs` file to include additional properties in the `product` object. The `NewUser.tsx` file has been simplified and the `Get.tsx` file has been updated to import new functions and update the `UserGet` class.

1. The `AdminHub.cs` file has been modified to remove the `System` namespace import. This change simplifies the code and removes unnecessary dependencies.

2. The `Delete.cs` file has been updated to include the `Variants`, `Types`, and `Combos` in the `product` object. The `IsArchived` property of the `product` object is set to `true` and the `deleteVariant` function is called for each variant in the `product` object. This change enhances the functionality of the `product` object and allows for more efficient deletion of variants.

3. In the `NewUser.tsx` file, the `useMount` function has been simplified to a single line. The `OnNewUserSubject.subscribe` function is called when the component is mounted and the `toggle` function is called if the subscription returns `true`. This change simplifies the code and improves the efficiency of the `NewUser` component.

4. The `Get.tsx` file has been updated to import the `useBoolean`, `useMount` functions from `ahooks` and the `OnNewUserSubject` from `~/Components/NewUser`. The `UserGet` class has been updated to version `0.4.1`. A new boolean state `onNew` has been added and the `OnNewUserSubject.subscribe` function is called when the component is mounted to set the `onNew` state. The error handling in the `useLiveQuery` function has been updated to log the error if `onNew` is `true` or the error is an instance of `NotLoginError`. This change improves error handling and updates the `UserGet` class to the latest version.

* The most significant changes involve the introduction of a new error class `EmptyResponseError` and the modification of error handling in `SignalR.ts` and `Get.tsx`. The `EmptyResponseError` class is used to handle cases where the server returns an empty response. In `SignalR.ts`, this new error class replaces the previously used `TypeError`. In `Get.tsx`, the error handling has been restructured with a `try-catch` block that handles different types of errors differently. The version number in the `UserGet` class in `Get.tsx` was also updated.

Changes:
1. A new error class `EmptyResponseError` was added to `Exceptions.ts`. This class is used to throw an error when the server returns an empty response.
2. The `EmptyResponseError` class was imported into `SignalR.ts` and `Get.tsx` files.
3. In `SignalR.ts`, the `TypeError` that was previously thrown when the server returned an empty response was replaced with the new `EmptyResponseError`.
4. In `Get.tsx`, the `useBoolean` and `useMount` hooks were removed, and the `OnNewUserSubject` subscription was also removed. Instead, a `try-catch` block was added to handle errors when getting user information.
5. The version number in the `UserGet` class in `Get.tsx` was updated from `0.4.1` to `0.4.2`.
  • Loading branch information
Aloento authored Jan 17, 2024
1 parent 00eeff2 commit 170165a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
1 change: 0 additions & 1 deletion SoarCraft.AwaiShop/AdminHub/AdminHub.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace SoarCraft.AwaiShop.AdminHub;

using System;
using Helpers;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authorization;
Expand Down
8 changes: 4 additions & 4 deletions SoarCraft.AwaiShop/AdminHub/Product/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ public async Task<bool> ProductDeleteProduct(uint prodId) {
return await prod.ExecuteDeleteAsync() > 0;

var product = await prod
.Include(x => x.Variants)
.ThenInclude(x => x.Types)
.ThenInclude(x => x.Combos)
.SingleAsync();
.Include(x => x.Variants)
.ThenInclude(x => x.Types)
.ThenInclude(x => x.Combos)
.SingleAsync();

product.IsArchived = true;

Expand Down
5 changes: 1 addition & 4 deletions src/Components/NewUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export const OnNewUserSubject = new Subject<boolean>();
*/
export function NewUser() {
const [open, { toggle }] = useBoolean();

useMount(() => {
OnNewUserSubject.subscribe(x => x && toggle());
});
useMount(() => OnNewUserSubject.subscribe(x => x && toggle()));

return (
<AuthenticatedTemplate>
Expand Down
11 changes: 11 additions & 0 deletions src/Helpers/Exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ export class NotTrueError extends Error {
super("Server Returned False");
}
}

/**
* @author Aloento
* @since 1.3.0
* @version 0.1.0
*/
export class EmptyResponseError extends Error {
public constructor() {
super("Server Returned Empty Response");
}
}
4 changes: 2 additions & 2 deletions src/ShopNet/SignalR.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HubConnectionState } from "@microsoft/signalr";
import dayjs, { Dayjs } from "dayjs";
import { Subject } from "rxjs";
import { NotLoginError, NotTrueError } from "~/Helpers/Exceptions";
import { EmptyResponseError, NotLoginError, NotTrueError } from "~/Helpers/Exceptions";
import type { Logger } from "~/Helpers/Logger";
import type { AdminNet } from "./Admin/AdminNet";
import { MSAL, Shared, type IConcurrency } from "./Database";
Expand Down Expand Up @@ -135,7 +135,7 @@ export abstract class SignalR {

if (!res) {
Shared.Sto.delete(index);
throw new TypeError("Empty Response");
throw new EmptyResponseError();
}

await Shared.Set<T & { QueryExp: number }>(index, {
Expand Down
20 changes: 13 additions & 7 deletions src/ShopNet/User/Get.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useConst } from "@fluentui/react-hooks";
import { useLiveQuery } from "dexie-react-hooks";
import { IPersona } from "~/Components/ShopCart/Persona";
import { NotLoginError } from "~/Helpers/Exceptions";
import { EmptyResponseError, NotLoginError } from "~/Helpers/Exceptions";
import type { Logger } from "~/Helpers/Logger";
import { useErrorToast } from "~/Helpers/useToast";
import { IConcurrency } from "../Database";
Expand All @@ -25,23 +25,29 @@ export abstract class UserGet extends ShopNet {
/**
* @author Aloento
* @since 1.0.0
* @version 0.4.0
* @version 0.4.2
*/
public static useMe(pLog: Logger): IUserGetMe | void {
const log = useConst(() => pLog.With("|", "Hub", "User", "Get", "Me"));
const { dispatch } = useErrorToast(log);

const res = useLiveQuery(() => this.GetVersionCache<IUserGetMe>(0, "UserGetMe")
.catch(e => {
if (e instanceof NotLoginError)
const res = useLiveQuery(async (): Promise<IUserGetMe | void> => {
try {
this.EnsureLogin();
return await this.GetVersionCache<IUserGetMe>(0, "UserGetMe");
} catch (e) {
if (e instanceof EmptyResponseError)
return;
else if (e instanceof NotLoginError)
log.info(e);
else
dispatch({
Message: "Failed to Get Your Info",
Error: e,
Error: e as Error,
Request: ""
});
}));
}
});

return res;
}
Expand Down

0 comments on commit 170165a

Please sign in to comment.