mirror of
https://github.com/lemeow125/StudE-Frontend.git
synced 2025-06-29 08:45:46 +08:00
Refactored error handling in API functions and improved error feedback in pages
This commit is contained in:
parent
c4c11d1afe
commit
cfd82d3c42
10 changed files with 181 additions and 116 deletions
|
@ -8,6 +8,7 @@ import {
|
|||
SubjectType,
|
||||
OptionType,
|
||||
StudentStatusType,
|
||||
PatchUserInfoType,
|
||||
} from "../../interfaces/Interfaces";
|
||||
import Button from "../../components/Button/Button";
|
||||
import { Image } from "react-native";
|
||||
|
@ -69,7 +70,13 @@ export default function SubjectsPage() {
|
|||
});
|
||||
const StudentInfo = useQuery({
|
||||
queryKey: ["user"],
|
||||
queryFn: GetUserInfo,
|
||||
queryFn: async () => {
|
||||
const data = await GetUserInfo();
|
||||
if (data[0] == false) {
|
||||
return Promise.reject(new Error(data[1]));
|
||||
}
|
||||
return data;
|
||||
},
|
||||
onSuccess: (data: UserInfoReturnType) => {
|
||||
setUser({
|
||||
...user,
|
||||
|
@ -84,8 +91,8 @@ export default function SubjectsPage() {
|
|||
});
|
||||
setSelectedSubjects(data[1].subjects);
|
||||
},
|
||||
onError: () => {
|
||||
toast.show("Server Error: Unable to query user info", {
|
||||
onError: (error: Error) => {
|
||||
toast.show(String(error), {
|
||||
type: "warning",
|
||||
placement: "top",
|
||||
duration: 2000,
|
||||
|
@ -94,7 +101,13 @@ export default function SubjectsPage() {
|
|||
},
|
||||
});
|
||||
const mutation = useMutation({
|
||||
mutationFn: PatchUserInfo,
|
||||
mutationFn: async (info: PatchUserInfoType) => {
|
||||
const data = await PatchUserInfo(info);
|
||||
if (data[0] != true) {
|
||||
return Promise.reject(new Error());
|
||||
}
|
||||
return data;
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["user"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["subjects"] });
|
||||
|
@ -110,6 +123,14 @@ export default function SubjectsPage() {
|
|||
animationType: "slide-in",
|
||||
});
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.show(String(error), {
|
||||
type: "warning",
|
||||
placement: "top",
|
||||
duration: 2000,
|
||||
animationType: "slide-in",
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
// Subjects
|
||||
|
@ -121,7 +142,13 @@ export default function SubjectsPage() {
|
|||
const Subjects = useQuery({
|
||||
enabled: StudentInfo.isFetched,
|
||||
queryKey: ["subjects"],
|
||||
queryFn: GetSubjects,
|
||||
queryFn: async () => {
|
||||
const data = await GetSubjects();
|
||||
if (data[0] == false) {
|
||||
return Promise.reject(new Error(JSON.stringify(data[1])));
|
||||
}
|
||||
return data;
|
||||
},
|
||||
onSuccess: (data: SubjectsReturnType) => {
|
||||
if (data[1]) {
|
||||
let subjects = data[1].map((subject: SubjectType) => ({
|
||||
|
@ -132,8 +159,8 @@ export default function SubjectsPage() {
|
|||
setSubjects(subjects);
|
||||
}
|
||||
},
|
||||
onError: () => {
|
||||
toast.show("Server Error: Unable to query subject info", {
|
||||
onError: (error: Error) => {
|
||||
toast.show(String(error), {
|
||||
type: "warning",
|
||||
placement: "top",
|
||||
duration: 2000,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue