This commit is contained in:
AntoninoP 2024-07-03 16:58:18 +02:00
commit 6cf8488476
8 changed files with 4750 additions and 256 deletions

View File

@ -6,11 +6,15 @@ module.exports = {
'plugin:react/recommended',
'plugin:react/jsx-runtime',
'plugin:react-hooks/recommended',
"plugin:@typescript-eslint/recommended",
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
settings: { react: { version: '18.2' } },
plugins: ['react-refresh'],
plugins: [
'react-refresh',
'@typescript-eslint',
],
rules: {
'react/jsx-no-target-blank': 'off',
'react-refresh/only-export-components': [

4693
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,20 +7,29 @@
"dev": "vite",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
"preview": "vite preview",
"test": "jest"
},
"dependencies": {
"@testing-library/jest-dom": "^6.4.6",
"@testing-library/react": "^16.0.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.9",
"firebase": "^10.12.2",
"jest": "^29.7.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.24.0",
"react-toastify": "^10.0.5",
"sass": "^1.77.6",
"toast": "^0.5.4"
"toast": "^0.5.4",
"typescript": "^5.5.3"
},
"devDependencies": {
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^7.15.0",
"@typescript-eslint/parser": "^7.15.0",
"@vitejs/plugin-react": "^4.3.1",
"eslint": "^8.57.0",
"eslint-plugin-react": "^7.34.2",

View File

@ -1,7 +1,7 @@
import React, { useState, useEffect } from 'react';
import styles from './CreateServer.module.scss';
import { getUserSubdomain } from "../../service/firebase";
import { saveSubdomain } from "../../service/serverService";
import serviiApi from "../../service/api.tsx";
const CreateServer = ({ user, onCreateServer }) => {
const [subdomain, setSubdomain] = useState('loading'); // Utiliser 'loading' au lieu de null
@ -26,7 +26,7 @@ const CreateServer = ({ user, onCreateServer }) => {
const handleSaveSubdomain = async () => {
try {
await saveSubdomain(subdomainInput);
await serviiApi.setSubdomain(subdomainInput);
setSubdomain(subdomainInput);
} catch (error) {
console.error('Error setting subdomain:', error);

View File

@ -4,7 +4,9 @@ import Navbar from '../../components/navbar/Navbar';
import styles from './DashboardPage.module.scss';
import NoServer from '../NoServer/NoServer';
import { getUserSubdomain } from "../../service/firebase";
import { fetchServers, createServer, runServer, stopServer, deleteServer } from '../../service/serverService';
import '../../service/api.tsx';
import serviiApi from "../../service/api.tsx";
const DashboardPage = ({ user }) => {
const [servers, setServers] = useState([]);
@ -12,7 +14,8 @@ const DashboardPage = ({ user }) => {
const loadServers = async () => {
try {
const data = await fetchServers();
const ApiResponse = await serviiApi.fetchServers();
const data = ApiResponse.message
setServers(data);
const userSubdomain = await getUserSubdomain(user.uid);
setSubdomain(userSubdomain);
@ -29,8 +32,8 @@ const DashboardPage = ({ user }) => {
try {
const serverName = prompt('Enter the name for the new server:');
if (!serverName) return;
await createServer(serverName);
loadServers();
await serviiApi.serverCreate(serverName, "1.20.6", "paper");
await loadServers();
} catch (error) {
console.error('Error creating server:', error);
}
@ -38,7 +41,7 @@ const DashboardPage = ({ user }) => {
const handleRunServer = async (serverName) => {
try {
await runServer(serverName);
await serviiApi.serverRun(serverName);
} catch (error) {
console.error('Error starting server:', error);
}
@ -46,7 +49,7 @@ const DashboardPage = ({ user }) => {
const handleStopServer = async (serverName) => {
try {
await stopServer(serverName);
await serviiApi.serverStop(serverName);
} catch (error) {
console.error('Error stopping server:', error);
}
@ -54,7 +57,7 @@ const DashboardPage = ({ user }) => {
const handleDeleteServer = async (serverName) => {
try {
await deleteServer(serverName);
await serviiApi.serverDelete(serverName);
loadServers();
} catch (error) {
console.error('Error deleting server:', error);

0
src/service/api.test.js Normal file
View File

127
src/service/api.tsx Normal file
View File

@ -0,0 +1,127 @@
// src/services/serverService.ts
import {getAuth} from 'firebase/auth';
const apiUrl: string = 'http://176.165.62.226:3000';
interface ApiResponse {
return_code: number;
message: string;
}
//fetchServers, accountCreate, accountDelete,
interface BaseRequest {
token: string;
}
//setSubdomain
interface SubdomainRequest {
token: string;
subdomain: string;
}
//serverDelete, serverRun, serverStop
interface ServerRequest {
token: string;
name: string;
}
//serverCreate
interface ServerCreationRequest {
token: string;
name: string;
version: string;
framework: string;
}
//updateProperty
interface UpdatePropertyRequest {
token: string;
name: string;
prop: string;
value: string;
}
//command
interface CommandRequest {
token: string;
command: string;
name: string;
}
enum serviiRequest {
setSubdomain = 'setSubdomain',
fetchServers = 'fetchServers',
accountCreate = 'accountCreate',
serverCreate = 'serverCreate',
serverDelete = 'serverDelete',
accountDelete = 'accountDelete',
serverRun = 'serverRun',
serverStop = 'serverStop',
updateProperty = 'updateProperty',
command = 'command',
}
class serviiApi {
constructor() {}
private static async call<T extends BaseRequest>(endpoint: serviiRequest, body: T):Promise<ApiResponse>{
const response = await fetch(`${apiUrl}/${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
const status = response.status
const json = await response.json();
if ('message' in json) {
return {return_code: status, message: json.message};
}
return {return_code: status, message: "Couldn't find an available API, we're sorry for the inconvenience."};
}
private static token(): string {
const currentUser = getAuth().currentUser;
if (!currentUser) {throw new Error('No user is currently logged in.');}
return currentUser.uid;
}
public static async setSubdomain(subdomain: string): Promise<ApiResponse> {
const payload : SubdomainRequest = {token: this.token(), subdomain: subdomain}
return this.call(serviiRequest.setSubdomain, payload);
}
public static async fetchServers(): Promise<ApiResponse> {
const payload : BaseRequest = {token: this.token()}
return this.call(serviiRequest.fetchServers, payload);
}
public static async accountCreate(): Promise<ApiResponse> {
const payload : BaseRequest = {token: this.token()}
return this.call(serviiRequest.accountCreate, payload);
}
public static async serverCreate(name: string, version: string, framework: string): Promise<ApiResponse> {
const payload : ServerCreationRequest = {token: this.token(), name: name, version: version, framework: framework};
return this.call(serviiRequest.serverCreate, payload);
}
public static async serverDelete(name: string): Promise<ApiResponse> {
const payload : ServerRequest = {token: this.token(), name: name}
return this.call(serviiRequest.serverDelete, payload);
}
public static async accountDelete(): Promise<ApiResponse> {
const payload : BaseRequest = {token: this.token()}
return this.call(serviiRequest.accountDelete, payload);
}
public static async serverRun(name: string): Promise<ApiResponse> {
const payload : ServerRequest = {token: this.token(), name: name}
return this.call(serviiRequest.serverRun, payload);
}
public static async serverStop(name: string): Promise<ApiResponse> {
const payload : ServerRequest = {token: this.token(), name: name}
return this.call(serviiRequest.serverStop, payload);
}
public static async updateProperty(name: string, prop: string, value: string): Promise<ApiResponse> {
const payload : UpdatePropertyRequest = {token: this.token(), name: name, prop: prop, value: value}
return this.call(serviiRequest.updateProperty, payload);
}
public static async command(command: string, name: string): Promise<ApiResponse> {
const payload : CommandRequest = {token: this.token(), command: command, name: name};
return this.call(serviiRequest.command, payload);
}
}
export default serviiApi;

View File

@ -1,146 +0,0 @@
// src/services/serverService.js
import { getAuth } from 'firebase/auth';
const apiUrl = 'http://176.165.62.226:3000';
const fetchServers = async () => {
const auth = getAuth();
const currentUser = auth.currentUser;
if (!currentUser) {
throw new Error('No current user found');
}
const userId = currentUser.uid;
const response = await fetch(`${apiUrl}/FetchServers`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userId}`,
},
body: JSON.stringify({ token: userId }),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Network response was not ok: ${errorText}`);
}
return await response.json();
};
const createServer = async (serverName) => {
const auth = getAuth();
const currentUser = auth.currentUser;
if (!currentUser) {
throw new Error('No current user found');
}
const userId = currentUser.uid;
const response = await fetch(`${apiUrl}/CreateServer`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userId}`,
},
body: JSON.stringify({ name: serverName, token: userId }),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to create server: ${errorText}`);
}
};
const runServer = async (serverName) => {
const auth = getAuth();
const currentUser = auth.currentUser;
if (!currentUser) {
throw new Error('No current user found');
}
const userId = currentUser.uid;
const response = await fetch(`${apiUrl}/ServerRun`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userId}`,
},
body: JSON.stringify({ name: serverName, token: userId }),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to start server: ${errorText}`);
}
};
const stopServer = async (serverName) => {
const auth = getAuth();
const currentUser = auth.currentUser;
if (!currentUser) {
throw new Error('No current user found');
}
const userId = currentUser.uid;
const response = await fetch(`${apiUrl}/ServerStop`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userId}`,
},
body: JSON.stringify({ name: serverName, token: userId }),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to stop server: ${errorText}`);
}
};
const deleteServer = async (serverName) => {
const auth = getAuth();
const currentUser = auth.currentUser;
if (!currentUser) {
throw new Error('No current user found');
}
const userId = currentUser.uid;
const response = await fetch(`${apiUrl}/ServerDelete`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userId}`,
},
body: JSON.stringify({ name: serverName, token: userId }),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to delete server: ${errorText}`);
}
};
const saveSubdomain = async (subdomain) => {
const auth = getAuth();
const currentUser = auth.currentUser;
if (!currentUser) {
throw new Error('No current user found');
}
const userId = currentUser.uid;
const response = await fetch(`${apiUrl}/SetSubdomain`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userId}`,
},
body: JSON.stringify({ subdomain, token: userId }),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to save subdomain: ${errorText}`);
}
};
export { fetchServers, createServer, runServer, stopServer, deleteServer, saveSubdomain };