Skip to content

Commit

Permalink
added ApiManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Nov 22, 2024
1 parent 63a5257 commit 9397c03
Show file tree
Hide file tree
Showing 16 changed files with 475 additions and 306 deletions.
49 changes: 13 additions & 36 deletions src/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,16 @@ import useCookie from "@/app/modules/utils/useCookie.module";
import { useEffect, useRef, useState } from "react";
import style_root from '@/app/styles/admin/page.module.css';
import { redirect, useRouter } from "next/navigation";
import Header, { Query } from "@/app/modules/components/header.module";
import { authApi } from "@/app/modules/utils/api.module";
import Header from "@/app/modules/components/header.module";
import AdaptiveGrid from "../modules/components/adaptiveGrid.module";
import { Fira_Code } from "next/font/google";
import Link from "next/link";
import SlideButton from "../modules/components/slideButton.module";
import { httpStatusCodes } from "../modules/utils/statusCodes.module";
import ApiManager from "../modules/utils/apiManager";
import { UserAdmins } from "../interfaces";

const fira = Fira_Code({ subsets: ["latin"] });

interface UserAdmins {
id: number,
username: string,
name: string,
joined_at: Date,
discord_id: number,
banned: boolean,
permissions: string[]
}

const Admin = () => {
const logged = useCookie('sessionId');
const cookies = useRef<Cookies>(useCookies());
Expand All @@ -42,36 +32,23 @@ const Admin = () => {
}, [logged]);

useEffect(() => {
authApi.get('user/me').then((response) => {
if (response.status === 200) {
const data = response.data as Query;
if (data.permissions.every(perm => perm === 'default')) {
router.replace('/');
return;
}
ApiManager.getMe().then(data => {
if (data.permissions.every(perm => perm === 'default')) {
router.replace('/');
return;
}

if (data.permissions.includes('updateusers') || data.permissions.includes('superadmin')) {
authApi.get('/users').then((response) => {
response.status === 200 && setUsers(response.data as UserAdmins[]);
})
}
if (data.permissions.includes('updateusers') || data.permissions.includes('superadmin')) {
ApiManager.getUsers().then(setUsers);
}
})
}, [])

const changeBan = (user: UserAdmins, banned: boolean): Promise<void> => {
return new Promise((resolve, reject) => {
try {
authApi.put(`/users/${user.username}`, { banned }).then(response => {
if (response.status !== 200) {
reject();
alert(`${response?.data?.message || httpStatusCodes[response?.status] || 'Unknown Error'} ${response?.status}`);
}
resolve();
})
} catch {
reject();
}
ApiManager.updateUser(user.username, { banned })
.then(resolve)
.catch(reject);
});
}

Expand Down
33 changes: 32 additions & 1 deletion src/app/interfaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,35 @@ export interface Role {
id: number,
title: string,
color: number
}
}

export interface UserAdmins {
id: number,
username: string,
name: string,
joined_at: Date,
discord_id: number,
banned: boolean,
permissions: string[]
}


export interface NotificationsInterface {
data: {
id: number,
content: string,
author: string,
type: number,
creation_date: Date
}[],
total_count: number
}

export interface Session {
id: number,
last_accessed: Date,
is_self: boolean,
is_mobile: boolean,
browser: string,
browser_version: string
}
19 changes: 3 additions & 16 deletions src/app/me/notifications/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,14 @@ import Style from "@/app/styles/me/notifications.module.css";
import Header from "@/app/modules/components/header.module";
import useCookie from '@/app/modules/utils/useCookie.module';
import { Me } from '@/app/modules/components/me.module';
import { authApi } from '@/app/modules/utils/api.module';
import { formatDate } from '@/app/modules/components/card.module';
import { Paginator } from '@/app/modules/components/paginator.module';
import style_sidebar from "@/app/styles/me/sidebar.module.css";
import Image from "next/image";
import { Cookies, useCookies } from 'next-client-cookies';
import ApiManager from '@/app/modules/utils/apiManager';
import { NotificationsInterface } from '@/app/interfaces';

interface NotificationsInterface {
data: {
id: number,
content: string,
author: string,
type: number,
creation_date: Date
}[],
total_count: number
}

const Notifications = () => {
const cookies = useRef<Cookies>(useCookies());
Expand All @@ -42,11 +33,7 @@ const Notifications = () => {

useEffect(() => {
if (page < 0) return;
authApi.get('user/me/notifications', { params: { page: page } }).then((response) => {
if (response.status === 200) {
setNotifications(response.data);
}
});
ApiManager.getMeNotifications({ page }).then(setNotifications);
}, [page]);

const notifications_el = notifications?.data.map((notification) => {
Expand Down
16 changes: 7 additions & 9 deletions src/app/me/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import IconSvgCropped from '@/app/resources/icon-cropped.svg';
import IconSvg from '@/app/resources/icon.svg';
import { httpStatusCodes } from '../modules/utils/statusCodes.module';
import { renderSkin } from '../modules/utils/skinCardRender.module';
import ApiManager from '../modules/utils/apiManager';

const Main = () => {
const router = useRouter();
Expand Down Expand Up @@ -48,7 +49,10 @@ const Main = () => {
if (code) {
authApi.post(`auth/discord/${code}`).then(response => {
if (response.status !== 200) {
setLoadingStatus(`${response.status}: ${response.data.message_ru || response.data.message || httpStatusCodes[response.status]}`);
setLoadingStatus(`${response.status}: ${response.data.message_ru ||
response.data.message ||
httpStatusCodes[response.status]}`
);
return;
}
setIsLogged(true);
Expand All @@ -59,14 +63,8 @@ const Main = () => {
}, []);

useEffect(() => {
if (isLogged) {
authApi.get('user/me/works').then(response => {
if (response.status === 200) {
setData(response.data);
}
});
}
}, [isLogged])
isLogged && ApiManager.getMeWorks().then(setData);
}, [isLogged]);

return (
<body>
Expand Down
Loading

0 comments on commit 9397c03

Please sign in to comment.