Skip to content

Commit

Permalink
fix(frontend): too many notifications when 401
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Jun 5, 2024
1 parent f029237 commit 95f5456
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 64 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ build: swag
@go build -ldflags "-linkmode external -w -s $(LDFLAGS)" -o ./build/$(BINARY) $(PACKAGE)/cmd/cloudsdale
@echo Build finished.

run: export DEBUG = true
run: swag
@echo Running $(PACKAGE)...
go run -ldflags "$(LDFLAGS)" $(PACKAGE)/cmd/cloudsdale
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package Cloudsdale
2 changes: 1 addition & 1 deletion web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import routes from "~react-pages";
import { Box, LoadingOverlay, MantineProvider } from "@mantine/core";
import { emotionTransform, MantineEmotionProvider } from "@mantine/emotion";
import { Suspense, useEffect } from "react";
import useTheme from "@/composables/useTheme";
import { useTheme } from "@/utils/theme";
import { useCategoryApi } from "@/api/category";
import { useCategoryStore } from "@/stores/category";
import { useConfigApi } from "@/api/config";
Expand Down
6 changes: 4 additions & 2 deletions web/src/pages/admin/challenges/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ function Page() {
}

function saveAttachment() {
const notificationID = showLoadingNotification({
showLoadingNotification({
id: "upload-attachment",
message: "正在上传附件",
});
const config: AxiosRequestConfig<FormData> = {};
challengeApi
.saveChallengeAttachment(Number(id), attachment!, config)
.then((_) => {
showSuccessNotification({
id: notificationID,
id: "upload-attachment",
message: "附件上传成功",
update: true,
});
setRefresh((prev) => prev + 1);
});
Expand Down
1 change: 1 addition & 0 deletions web/src/utils/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const useAuth = () => {
authStore.setUser(undefined);
navigate("/login");
showInfoNotification({
id: "auth-expired",
message: "登录凭据已过期,请重新登录",
});
}
Expand Down
126 changes: 66 additions & 60 deletions web/src/utils/notification.tsx
Original file line number Diff line number Diff line change
@@ -1,140 +1,146 @@
import { notifications, showNotification } from "@mantine/notifications";
import {
NotificationData,
showNotification,
updateNotification,
} from "@mantine/notifications";
import MDIcon from "@/components/ui/MDIcon";

export function showErrNotification({
id,
title,
message,
update,
}: {
id?: string;
title?: string;
message?: string;
update?: boolean;
}) {
if (id) {
notifications.update({
id: id,
title: title || "发生了错误",
message: message,
color: "red",
icon: <MDIcon c={"white"}>close</MDIcon>,
const notificationData: NotificationData = {
id: id,
title: title || "错误",
message: message,
color: "red",
icon: <MDIcon c={"white"}>exclamation</MDIcon>,
};
if (update) {
updateNotification({
...notificationData,
autoClose: 2000,
withCloseButton: true,
loading: false,
});
return;
} else {
showNotification(notificationData);
}
showNotification({
title: title || "发生了错误",
message: message,
color: "red",
icon: <MDIcon c={"white"}>close</MDIcon>,
});
}

export function showSuccessNotification({
id,
title,
message,
update,
}: {
id?: string;
title?: string;
message?: string;
update?: boolean;
}) {
if (id) {
notifications.update({
id: id,
title: title || "成功",
message: message,
color: "green",
icon: <MDIcon c={"white"}>check</MDIcon>,
const notificationData: NotificationData = {
id: id,
title: title || "成功",
message: message,
color: "green",
icon: <MDIcon c={"white"}>check</MDIcon>,
};
if (update) {
updateNotification({
...notificationData,
autoClose: 2000,
withCloseButton: true,
loading: false,
});
return;
} else {
showNotification(notificationData);
}
showNotification({
title: title || "成功",
message: message,
color: "green",
icon: <MDIcon c={"white"}>check</MDIcon>,
});
}

export function showInfoNotification({
id,
title,
message,
update,
}: {
id?: string;
title?: string;
message?: string;
update?: boolean;
}) {
if (id) {
notifications.update({
id: id,
title: title || "信息",
message: message,
color: "blue",
icon: <MDIcon c={"white"}>info_i</MDIcon>,
const notificationData: NotificationData = {
id: id,
title: title || "信息",
message: message,
color: "blue",
icon: <MDIcon c={"white"}>info_i</MDIcon>,
};
if (update) {
updateNotification({
...notificationData,
autoClose: 2000,
withCloseButton: true,
loading: false,
});
return;
} else {
showNotification(notificationData);
}
showNotification({
title: title || "信息",
message: message,
color: "blue",
icon: <MDIcon c={"white"}>info_i</MDIcon>,
});
}

export function showWarnNotification({
id,
title,
message,
update,
}: {
id?: string;
title?: string;
message?: string;
update?: boolean;
}) {
if (id) {
notifications.update({
id: id,
title: title || "警告",
message: message,
color: "orange",
icon: <MDIcon c={"white"}>exclamation</MDIcon>,
const notificationData: NotificationData = {
id: id,
title: title || "警告",
message: message,
color: "orange",
icon: <MDIcon c={"white"}>exclamation</MDIcon>,
};
if (update) {
updateNotification({
...notificationData,
autoClose: 2000,
withCloseButton: true,
loading: false,
});
return;
} else {
showNotification(notificationData);
}
showNotification({
title: title || "警告",
message: message,
color: "orange",
icon: <MDIcon c={"white"}>exclamation</MDIcon>,
});
}

export function showLoadingNotification({
id,
title,
message,
}: {
id?: string;
title?: string;
message?: string;
}): string {
const id = notifications.show({
}) {
showNotification({
id: id,
title: title || "请稍后",
loading: true,
message: message,
color: "blue",
autoClose: false,
withCloseButton: false,
});
return id;
}
2 changes: 1 addition & 1 deletion web/src/composables/useTheme.ts → web/src/utils/theme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createTheme } from "@mantine/core";

export default function useTheme() {
export function useTheme() {
const theme = createTheme({
colors: {
brand: [
Expand Down

0 comments on commit 95f5456

Please sign in to comment.