From 5af16b593c020280c4c4417ace04798413dfc9ac Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Sat, 17 May 2025 02:27:56 +0200 Subject: [PATCH] Implemented a delete button for mods and fixed an issue in the backend --- backend/config/config.json | 2 +- backend/src/controllers/mods.js | 2 +- frontend/src/pages/mod_creation.jsx | 146 +++++++++++++++++--- frontend/src/pages/mod_page.jsx | 78 ++++++++--- frontend/src/services/mods.js | 35 ++++- frontend/src/styles/content_page.module.css | 20 +++ frontend/src/styles/settings.module.css | 2 + 7 files changed, 240 insertions(+), 45 deletions(-) diff --git a/backend/config/config.json b/backend/config/config.json index a060d02..c5332f0 100644 --- a/backend/config/config.json +++ b/backend/config/config.json @@ -14,6 +14,6 @@ "auth": { "JWT_secret": "HGF7654EGBNKJNBJH6754356788GJHGY", - "tokenExpiry": "1h" + "tokenExpiry": "30d" } } \ No newline at end of file diff --git a/backend/src/controllers/mods.js b/backend/src/controllers/mods.js index 840ce08..17cfe66 100644 --- a/backend/src/controllers/mods.js +++ b/backend/src/controllers/mods.js @@ -53,7 +53,7 @@ async function getModByName(req, res) { async function deleteMod(req, res) { try { // Authorize - authorizeModModification(req); + await authorizeModModification(req); // Query const name = req.params.name const query_result = await mod_service.deleteMod(name); diff --git a/frontend/src/pages/mod_creation.jsx b/frontend/src/pages/mod_creation.jsx index 2d08fe7..3ee449a 100644 --- a/frontend/src/pages/mod_creation.jsx +++ b/frontend/src/pages/mod_creation.jsx @@ -1,6 +1,8 @@ // Preact import { h } from 'preact'; import { useState, useEffect } from 'preact/hooks'; +import Cookies from 'js-cookie' +import { jwtDecode } from 'jwt-decode' // Functions import { createMod } from '../services/mods'; @@ -18,13 +20,113 @@ import styles from '../styles/content_creation.module.css' import Button from '../components/Buttons/button'; function ModCreationPage() { + //TODO add missing fields - const user = "guill" //TODO - - const mod_infos = { - name: "" + const null_fields = { + name: null, + display_name: null, + description: null, + mod_infos: { + full_description: null + } } + //TODO use a service + let user; + const token = Cookies.get('authToken'); + if (token) { + const decoded_token = jwtDecode(token); + if (decoded_token) { + user = decoded_token.username; + } else { + location.replace('/login') + } + } else { + location.replace('/login') + } + + const [name, setName] = useState(''); + const [display_name, setDisplayName] = useState(''); + const [description, setDescription] = useState(''); + const [full_description, setFullDescription] = useState(''); + + const [publish_status, setPublishStatus] = useState(null); + const [field_errors, setFieldErrors] = useState(null_fields); + + + // Handle fields changes + + const handleNameChange = (event) => { + setName(event.target.value); + // Reset error + setFieldErrors(prevErrors => ({ ...prevErrors, name: null })); + }; + const handleDisplayNameChange = (event) => { + setDisplayName(event.target.value); + setFieldErrors(prevErrors => ({ ...prevErrors, display_name: null })); + }; + const handleDescriptionChange = (event) => { + setDescription(event.target.value); + setFieldErrors(prevErrors => ({ ...prevErrors, description: null })); + }; + const handleFullDescriptionChange = (event) => { + setFullDescription(event.target.value); + console.debug(event.target.value); + setFieldErrors(prevErrors => ({ ...prevErrors, full_description: null })); + }; + + // Submission + + const handleSubmit = async (event) => { + event.preventDefault(); + setFieldErrors(null_fields); + + setPublishStatus('publishing'); + + try { + // Gather data + const mod_data = { + name: name, + display_name: display_name, + description: description, + mod_infos: { + full_description: full_description, + //TODO handle all possible fields + "license": { + "type": "none" + }, + "links": [] + } + } + + // Query + const response = await createMod(mod_data); + + // On success + console.debug('Published successfully:', response); + setPublishStatus('success'); + window.location.replace("/mods/" + name); //TODO no page reloading + + } catch (error) { + + console.error('Creation failed:', error); + setPublishStatus('error'); + + //TODO handle different codes differently + setFieldErrors({ + name: ' ', + display_name: ' ', + description: ' ', + mod_infos: { + full_description: ' ' + } + }); + } + finally { + setPublishStatus(null); + } + }; + return ( <> @@ -33,7 +135,7 @@ function ModCreationPage() {
-
+
@@ -41,9 +143,9 @@ function ModCreationPage() { @@ -67,9 +169,9 @@ function ModCreationPage() {