Skip to content

Commit

Permalink
rfac: used useQuery instead of useEffect (frontend)
Browse files Browse the repository at this point in the history
  • Loading branch information
yp969803 committed Jan 5, 2025
1 parent 4f5091e commit ecb38b1
Show file tree
Hide file tree
Showing 19 changed files with 340 additions and 284 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Admin credintials:

Change admin credentials after first login.

Admin credential is stored in the file "/var/autocd/admin-credential.json", in case you forgot.

The application is running in http://localhost:5001, Logs of autocd can be seen in output.log

- Stop running autocd
Expand Down
105 changes: 105 additions & 0 deletions src/client/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 src/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hot-toast": "^2.4.1",
"react-query": "^3.39.3",
"react-router-dom": "^6.28.0",
"react-scripts": "5.0.1",
"typescript": "^4.9.5",
Expand Down
11 changes: 7 additions & 4 deletions src/client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import Projects from './pages/project';
import Login from './pages/login';
import './index.css';
import Navbar from './components/navbar';
import { useEffect, useState } from 'react';
import { useState } from 'react';
import { getToken } from './utils/utils';
import toast from 'react-hot-toast';
import { getCurrentUser } from './api/user';
import { CurrentUser } from './models/user';
import User from './pages/user';
import Script from './pages/script';
import Logs from './pages/logs';
import { useQuery } from 'react-query';

function App() {
const navigate = useNavigate();
Expand Down Expand Up @@ -38,9 +39,11 @@ function App() {
}
}
};
useEffect(() => {
fetchCurrentUser();
}, []);

useQuery('currentUser', fetchCurrentUser, {
retry: false,
});

return (
<div>
<Navbar currUser={currUser} setCurrUser={setCurrUser} />
Expand Down
4 changes: 0 additions & 4 deletions src/client/src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ export const getUsers = async (): Promise<UserModel[]> => {
export const createUser = async (
username: string,
password: string,
roles: string[],
permissions: string[]
): Promise<UserModel> => {
const response = await axios.post<UserModel>(
url + '/api/protected/user/admin/add',
{
username: username,
password: password,
roles: roles,
permissions: permissions,
},
{
Expand All @@ -57,15 +55,13 @@ export const updateUser = async (
id: number,
username: string,
password: string,
roles: string[],
permissions: string[]
): Promise<UserModel> => {
const response = await axios.put<UserModel>(
url + '/api/protected/user/admin/update/' + id,
{
username: username,
password: password,
roles: roles,
permissions: permissions,
},
{
Expand Down
3 changes: 1 addition & 2 deletions src/client/src/components/navbar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './index.css';
import logo from '../../assets/autocd-logo.png';
import { CurrentUser } from '../../models/user';
import React, { useEffect } from 'react';
import React from 'react';
import { Link, useNavigate } from 'react-router-dom';

interface Prop {
Expand All @@ -11,7 +11,6 @@ interface Prop {

const Navbar: React.FC<Prop> = ({ currUser, setCurrUser }) => {
const navigate = useNavigate();
useEffect(() => {}, [currUser, setCurrUser]);
const logoutHandler = () => {
setCurrUser(null);
localStorage.removeItem('token');
Expand Down
2 changes: 1 addition & 1 deletion src/client/src/pages/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Login = () => {
return (
<>
<div className="log-div">
<img className="logo" src={logo} alt="autocd"/>
<img className="logo" src={logo} alt="autocd" />
<div className="input-div">
<div className="input">
<span className="input-box">Username: </span>
Expand Down
9 changes: 5 additions & 4 deletions src/client/src/pages/logs/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useParams } from 'react-router-dom';
import './index.css';
import { useEffect, useState } from 'react';
import { useState } from 'react';
import { getLogs } from '../../api/logs';
import MonacoEditor from '@monaco-editor/react';
import { useQuery } from 'react-query';
const Logs = () => {
const { id } = useParams();
const [logs, setLogs] = useState<string>('');
Expand All @@ -16,9 +17,9 @@ const Logs = () => {
} catch (e) {}
};

useEffect(() => {
Fetch();
}, []);
useQuery('logs', Fetch, {
retry: false,
});

return (
<div>
Expand Down
9 changes: 5 additions & 4 deletions src/client/src/pages/project/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useEffect, useState } from 'react';
import { useState } from 'react';
import './index.css';
import { ProjectModel } from '../../models/project';
import { addProject, getAllProjects } from '../../api/project';
import ProjectCard from './project-card';
import toast from 'react-hot-toast';
import { useQuery } from 'react-query';

const Projects = () => {
const [projects, setProjects] = useState<ProjectModel[]>([]);
Expand Down Expand Up @@ -41,9 +42,9 @@ const Projects = () => {
error: data => `${data.message}`,
});
};
useEffect(() => {
fetchProjects();
}, []);
useQuery('projects', fetchProjects, {
retry: false,
});
return (
<div className="project-page">
<button className="project-btn" onClick={() => setOpen(true)}>
Expand Down
9 changes: 5 additions & 4 deletions src/client/src/pages/script/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useNavigate, useParams } from 'react-router-dom';
import './index.css';
import { useEffect, useState } from 'react';
import { useState } from 'react';
import { ProjectModel } from '../../models/project';
import { getProject } from '../../api/project';
import { getScript, updateScript } from '../../api/script';
Expand All @@ -9,6 +9,7 @@ import toast from 'react-hot-toast';
import { Link } from 'react-router-dom';
import { getHost } from '../../utils/utils';
import { getAuthToken } from '../../api/token';
import { useQuery } from 'react-query';

const Script = () => {
const { id } = useParams();
Expand Down Expand Up @@ -72,9 +73,9 @@ const Script = () => {
setToken(res.token);
} catch (e) {}
};
useEffect(() => {
fetch();
}, []);
useQuery('script', fetch, {
retry: false,
});

return (
<div className="script-page">
Expand Down
Loading

0 comments on commit ecb38b1

Please sign in to comment.