Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Refactor: all admin page #23

Merged
merged 24 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e6cc7e8
refactor: routes refactor and feat questionCheck admin routes
swgvenghy Aug 5, 2024
ca5b1d7
Refactor: tailwind CSS refactor
swgvenghy Aug 5, 2024
3919640
feat: api admin-retrieve-file logic
swgvenghy Aug 5, 2024
ff432d1
refactor: add list current file list
swgvenghy Aug 5, 2024
e6aca88
feat: adminQuestionCheck api logic
swgvenghy Aug 5, 2024
061f79f
Feat: table question check page
swgvenghy Aug 5, 2024
b8c1354
Chore: Feat react-cookie
swgvenghy Aug 6, 2024
eb76e93
Refactor: set cookie provider
swgvenghy Aug 6, 2024
fa58b68
Feat: cookies util setting
swgvenghy Aug 6, 2024
83a5f5e
Refactor: axios util withCredentails true
swgvenghy Aug 6, 2024
ff8b0e4
Refactor: admin-login api not use swr and set Cookie
swgvenghy Aug 6, 2024
d26b38f
Refactor: token postion set refactor sessionStorage to cookie
swgvenghy Aug 6, 2024
52df81d
Refactor: admin login tsx refactor
swgvenghy Aug 6, 2024
e7ae47d
Feat: edit modal ant.design
swgvenghy Aug 6, 2024
987ee8b
Refactor: add edit modal
swgvenghy Aug 6, 2024
7de60fe
Feat: admin api with bearer token
swgvenghy Aug 6, 2024
e7fc052
Refactor: Admin logout api add
swgvenghy Aug 6, 2024
7e8b590
Refactor: add timeout function
swgvenghy Aug 6, 2024
cb153ec
Refactor: Add logout button
swgvenghy Aug 6, 2024
dfad722
Refactor: admin api header set
swgvenghy Aug 6, 2024
6ab68ef
Refactor: page navigate error refactor
swgvenghy Aug 6, 2024
b2e18a5
Refactor: edit-modal textarea content
swgvenghy Aug 6, 2024
e9cef46
Fix: Conflict error
swgvenghy Aug 6, 2024
3dc2593
Merge branch 'main' into admin-questions-answer-table
swgvenghy Aug 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"prettier": "^3.3.2",
"prettier-plugin-tailwindcss": "^0.6.5",
"react": "^18.3.1",
"react-cookie": "^7.2.0",
"react-dom": "^18.3.1",
"react-icons": "^5.2.1",
"react-markdown": "^9.0.1",
Expand Down
24 changes: 24 additions & 0 deletions src/api/admin-check-question-answer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { server_axiosInstance } from '../utils/axios';
import { getCookie } from '../utils/cookies';

interface AdminCheckQuestionAnswerProps {
questionId: number;
check: boolean;
}
export async function AdminCheckQuestionAnswer({ questionId, check }: AdminCheckQuestionAnswerProps) {
const data = {
questionId,
check,
};
try {
const response = await server_axiosInstance.post('/api/admin/questions/check', JSON.stringify(data), {
headers: {
Authorization: `Bearer ${getCookie('accessToken')}`,
'Content-Type': 'application/json',
},
});
return response.data;
} catch (error: any) {
throw new Error('checked failed');
}
}
22 changes: 22 additions & 0 deletions src/api/admin-edit-answer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { server_axiosInstance } from '../utils/axios';
import { getCookie } from '../utils/cookies';

export async function AdminEditAnswer(id: number, content: string): Promise<any> {
const data = {
id,
content,
};

try {
const response = await server_axiosInstance.post('/api/admin/answers', JSON.stringify(data), {
headers: {
Authorization: `Bearer ${getCookie('accessToken')}`,
'Content-Type': 'application/json',
},
});
console.log(response.data);
return response.data;
} catch (error: any) {
throw new Error('Post Failed');
}
}
48 changes: 22 additions & 26 deletions src/api/admin-login.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
import axios from 'axios';
import useSWR, { mutate } from 'swr';
import { server_axiosInstance } from '../utils/axios';
import { getCookie, removeCookie, setCookie } from '../utils/cookies';

interface LoginResponse {
token: string;
user: {
email: string;
export async function AdminLogin(email: string, password: string) {
const data = {
email,
password,
};
}

const fetcher = (url: string) => axios.get(url).then((res) => res.data);
try {
if (getCookie('accessToken')) {
removeCookie('accessToken');
removeCookie('refreshToken');
}

export async function login(email: string, password: string): Promise<LoginResponse> {
const response = await axios.post<LoginResponse>('경로변경넣어야됌', { email, password }, { withCredentials: true });
return response.data;
}
const response = await server_axiosInstance.post('/api/auth/sign-in', data);
const accessToken: string = response.headers['authorization'];
const refreshToken: string = response.headers['authorization-refresh'];

export function useUser() {
const { data, error } = useSWR<LoginResponse>('경로넣어야행', fetcher);
return {
user: data,
isLoading: !error && !data,
isError: error,
};
setCookie('accessToken', accessToken.split(' ')[1], { path: '/admin', secure: true, sameSite: 'lax' });
setCookie('refreshToken', refreshToken.split(' ')[1], { path: '/admin', secure: true, sameSite: 'lax' });
} catch (error: any) {
throw new Error('Login Failed - Server network failed');
}
}

export async function handleLogin(email: string, password: string): Promise<void> {
try {
const userData = await login(email, password);
mutate('경로헤헿콩', userData, false);
} catch (err: any) {
throw new Error(err.message);
}
export function AdminLogout() {
removeCookie('accessToken', { path: '/admin', secure: true, sameSite: 'lax' });
removeCookie('refreshToken', { path: '/admin', secure: true, sameSite: 'lax' });
}
20 changes: 20 additions & 0 deletions src/api/admin-question-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { server_axiosInstance } from '../utils/axios';

interface AdminQuestionCheckProps {
type: string;
category?: string;
}

export async function adminQuestionCheck({ type, category }: AdminQuestionCheckProps) {
try {
const response = await server_axiosInstance.get('/api/questions', {
params: {
type,
category,
},
});
return response.data;
} catch (error: any) {
throw new Error(`Upload failed: ${error.message}`);
}
}
20 changes: 20 additions & 0 deletions src/api/admin-retrieve-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { llm_axiosInstance } from '../utils/axios';

interface AdminRetrieveFileProps {
type?: string;
category?: string;
}

export async function adminRetrieveFile({ type, category }: AdminRetrieveFileProps) {
try {
const response = await llm_axiosInstance.get('/retrieve_documents_by_type_and_category', {
params: {
type,
category,
},
});
return response.data;
} catch (error: any) {
throw new Error(`Upload failed: ${error.message}`);
}
}
7 changes: 6 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import App from './App';
import ReactDOM from 'react-dom/client';
import reportWebVitals from './reportWebVitals';
import './index.css';
import { CookiesProvider } from 'react-cookie';

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(<App />);
root.render(
<CookiesProvider>
<App />
</CookiesProvider>,
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
Expand Down
7 changes: 4 additions & 3 deletions src/routes/admin-routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { Route, Routes } from 'react-router-dom';
import PrivateRoute from './private-route';
import FileList from '../ui/pages/admin/file-list';
import Login from '../ui/pages/admin/login';
import QuestionCheck from '../ui/pages/admin/question-check';
import AdminLayout from '../ui/components/admin/Layout/admin-layout';

import { getCookie } from '../utils/cookies';
import QuestionCheck from '../ui/pages/admin/question-check';
const AdminRoutes: React.FC = () => {
const token = sessionStorage.getItem('Authorization');
const token = getCookie('accessToken');
return (
<Routes>
<Route path="/login" element={<Login />} />
<Route path="/" element={<PrivateRoute component={<AdminLayout />} authenticated={token} />}>
<Route path="setting/file" element={<FileList />} />
<Route path="question/list" element={<QuestionCheck />} />
</Route>
</Routes>
);
Expand Down
2 changes: 0 additions & 2 deletions src/routes/app-routes.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React from 'react';
import { Route, Routes } from 'react-router-dom';
import AdminRoutes from './admin-routes';
import PageTest from '../ui/pages/page-test';
import MaruEgg from '../ui/pages/maru-egg';

const AppRoutes: React.FC = () => {
return (
<Routes>
<Route path="/" element={<MaruEgg />} />
<Route path="/admin/*" element={<AdminRoutes />} />
<Route path="/test/*" element={<PageTest />} />
</Routes>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/routes/private-route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface PrivateRouteProps {
}

const PrivateRoute = ({ authenticated, component }: PrivateRouteProps) => {
return authenticated ? component : <Navigate to="/login"></Navigate>;
return authenticated ? component : <Navigate to="/admin/login"></Navigate>;
};

export default PrivateRoute;
17 changes: 14 additions & 3 deletions src/ui/components/admin/Layout/admin-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import React, { useEffect, useState } from 'react';
import { Breadcrumb, Layout, Menu } from 'antd';
import { Outlet, useLocation } from 'react-router-dom';
import { Breadcrumb, Button, Layout, Menu } from 'antd';
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
import SlideMenu from '../slide-menu/slide-menu';
import { Header } from 'antd/es/layout/layout';
import { AdminLogout } from '../../../../api/admin-login';
import { removeCookie } from '../../../../utils/cookies';

const { Sider, Content, Footer } = Layout;

const AdminLayout: React.FC = () => {
const { pathname } = useLocation();
const navigate = useNavigate();
useEffect(() => {
pathname.split('/').map((a, i) => {
console.log(pathname.split('/')[i]);
});
});

const handleLogout = () => {
AdminLogout();
navigate('/admin/login');
};

const breadcrumpItem1 = pathname.split('/')[1];
const breadcrumpItem2 = pathname.split('/')[2];

Expand All @@ -24,8 +32,11 @@ const AdminLayout: React.FC = () => {
<SlideMenu />
</Sider>
<Layout className="site-layout">
<Header className="flex items-center bg-[#DFF4FF]">
<Header className="flex items-center justify-between bg-white">
<Breadcrumb separator=">" items={breadcrumpItems} className="pl-2 text-lg text-black" />
<Button type="primary" onClick={handleLogout}>
로그아웃
</Button>
</Header>
<Content style={{ margin: '16px 0' }}>
<div className="site-layout-background" style={{ padding: 24, minHeight: 360 }}>
Expand Down
Loading
Loading