Skip to content

Commit

Permalink
fix: admin detach and parts of page error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowballXueQiu committed Aug 26, 2024
1 parent 178c3e3 commit cfaf19c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 15 deletions.
71 changes: 71 additions & 0 deletions api/AdminApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,81 @@ export default class AdminApi {

return res.json();
}

public static async getAdminTokenInfo(): Promise<TokenQueryResponse<AdminInfo>> {
const myHeaders = new Headers();
myHeaders.append('token', Cookie.getCookie('token'));

const requestOptions: RequestInit = {
method: 'GET',
headers: myHeaders,
redirect: 'follow',
};

const result: Response = await fetch(`${SERVER_URL}/api/token/admin`, requestOptions);

if (!result.ok) {
return {
ok: false,
result: {
id: 0,
username: 'unknown',
disabled: true,
},
};
}

const info: AdminInfo = await result.json();

return {
ok: true,
result: info,
};
}

public static async getTokenInfo(): Promise<TokenQueryResponse<UserInfo>> {
const myHeaders = new Headers();
myHeaders.append('token', Cookie.getCookie('token'));

const requestOptions: RequestInit = {
method: 'GET',
headers: myHeaders,
redirect: 'follow',
};

const result: Response = await fetch(`${SERVER_URL}/api/token`, requestOptions);

if (!result.ok) {
return {
ok: false,
result: {
uid: '0',
username: 'unknown',
},
};
}

const info: UserInfo = await result.json();

return {
ok: true,
result: info,
};
}
}

export interface TokenQueryResponse<T> {
ok: boolean;
result: T
}

export interface AdminInfo {
id: number;
username: string;
disabled: boolean;
}

export interface UserInfo {
uid: string;
username: string;
}
3 changes: 2 additions & 1 deletion app/(root)/backstage/components/Tools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default function Tools() {

function logOut() {
Cookie.clearAllCookies();
goHome();
sessionStorage.setItem('logOutAndRedirect', 'true');
window.location.reload();
}

return (
Expand Down
21 changes: 10 additions & 11 deletions app/(root)/backstage/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ export default function BackStageLayout({ children }: { children: React.ReactNod
setUserName(decodeURI(userNameFromCookie));
setUserId(userIdFromCookie);

if (userIdFromCookie) {
AdminApi.getAdminInfo(Number(userIdFromCookie))
.then(() => {
setLoading(false);
})
.catch(() => {
AdminApi.getAdminTokenInfo()
.then((res) => {
if (!res.ok) {
sessionStorage.setItem('adminAccessDenied', 'true');
router.push('/');
});
} else {
router.push('/');
}
// router.push('/');
}

setUserId(res.result.id.toString());
setUserName(res.result.username);
setLoading(false);
});
}
}, [router]);

Expand Down
4 changes: 2 additions & 2 deletions app/(root)/oauth/components/LoginBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function LoginBanner() {
<Container w="100%">
<div className={classes.wrapper}>
<SimpleGrid cols={{ base: 1, sm: 2 }}>
<Container w={256}>
<Image src={image.src} className={classes.loginBannerImage} w="100%" />
<Container w={256} className={classes.loginBannerImage}>
<Image src={image.src} w="100%" />
</Container>
<div className={classes.body}>
<Title className={classes.title}>
Expand Down
1 change: 0 additions & 1 deletion app/(root)/survey/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export default function SurveyPage({ params }: { params: { id: number } }) {
const newAnswers = new Map(answers);
newAnswers.set(id, value);
setAnswers(newAnswers);
// console.log(newAnswers);
};

const getAnswerGetter = (id: string) => answers.get(id) || undefined;
Expand Down

0 comments on commit cfaf19c

Please sign in to comment.