diff --git a/amplify/backend/function/createDeadline/src/index.js b/amplify/backend/function/createDeadline/src/index.js index 6284229..7f5a2c2 100644 --- a/amplify/backend/function/createDeadline/src/index.js +++ b/amplify/backend/function/createDeadline/src/index.js @@ -1,7 +1,15 @@ const { createDbConnection } = require("/opt/nodejs/dbConnection"); -const { validateDeadline } = require("/opt/nodejs/helper"); +const { + validateDeadline, + createCorsResponse, + handleOptionsRequest, +} = require("/opt/nodejs/helper"); exports.handler = async (event) => { + if (event.httpMethod === "OPTIONS") { + return handleOptionsRequest(); + } + let connection; try { @@ -19,15 +27,7 @@ exports.handler = async (event) => { type, }); if (error) { - return { - statusCode: 400, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ message: error.details[0].message }), - }; + return createCorsResponse(400, { message: error.details[0].message }); } connection = await createDbConnection({ @@ -42,15 +42,7 @@ exports.handler = async (event) => { user_id, ]); if (user.length === 0) { - return { - statusCode: 400, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ message: "Utente non trovato." }), - }; + return createCorsResponse(400, { message: "Utente non trovato." }); } // Inserisci la deadline @@ -59,35 +51,16 @@ exports.handler = async (event) => { [title, description, due_date, notifications_on, user_id, type] ); - return { - statusCode: 201, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": - "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", - "Access-Control-Allow-Methods": - "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Scadenza creata con successo", - id: result.insertId, - }), - }; + return createCorsResponse(201, { + message: "Scadenza creata con successo", + id: result.insertId, + }); } catch (err) { console.error("❌ Errore in createDeadline:", err); - return { - statusCode: 500, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Errore durante la creazione della deadline.", - error: err, - }), - }; + return createCorsResponse(500, { + message: "Errore durante la creazione della deadline.", + error: err.message, + }); } finally { if (connection) await connection.end(); } diff --git a/amplify/backend/function/createUsers/src/index.js b/amplify/backend/function/createUsers/src/index.js index 9aed8b5..04c8b6b 100644 --- a/amplify/backend/function/createUsers/src/index.js +++ b/amplify/backend/function/createUsers/src/index.js @@ -1,7 +1,15 @@ const { createDbConnection } = require("/opt/nodejs/dbConnection"); -const { getS3AvatarConfig } = require("/opt/nodejs/helper"); +const { + getS3AvatarConfig, + createCorsResponse, + handleOptionsRequest, +} = require("/opt/nodejs/helper"); exports.handler = async (event) => { + if (event.httpMethod === "OPTIONS") { + return handleOptionsRequest(); + } + let connection; try { @@ -28,18 +36,10 @@ exports.handler = async (event) => { ); if (existingUsers.length > 0) { - return { - statusCode: 409, // Conflict - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Un utente con questa email esiste già.", - code: "USER_ALREADY_EXISTS", - }), - }; + return createCorsResponse(409, { + message: "Un utente con questa email esiste già.", + code: "USER_ALREADY_EXISTS", + }); } // Inserisci il nuovo utente @@ -56,49 +56,22 @@ exports.handler = async (event) => { profileImageUrl: `https://${s3Config.bucketName}.s3.${s3Config.region}.amazonaws.com/${s3Config.folderName}/${profileImageId}.jpg`, }; - return { - statusCode: 201, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": - "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", - "Access-Control-Allow-Methods": - "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", - "Content-Type": "application/json", - }, - body: JSON.stringify(newUser), - }; + return createCorsResponse(201, newUser); } catch (err) { console.error("❌ Errore in createUsers:", err); // Gestisci errori specifici del database if (err.code === "ER_DUP_ENTRY") { - return { - statusCode: 409, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Un utente con questa email esiste già.", - code: "USER_ALREADY_EXISTS", - }), - }; + return createCorsResponse(409, { + message: "Un utente con questa email esiste già.", + code: "USER_ALREADY_EXISTS", + }); } - return { - statusCode: 500, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Errore del server durante la creazione dell'utente.", - error: err.message, - }), - }; + return createCorsResponse(500, { + message: "Errore del server durante la creazione dell'utente.", + error: err.message, + }); } finally { if (connection) { await connection.end(); diff --git a/amplify/backend/function/deleteDeadlines/src/index.js b/amplify/backend/function/deleteDeadlines/src/index.js index 548b765..d2d4489 100644 --- a/amplify/backend/function/deleteDeadlines/src/index.js +++ b/amplify/backend/function/deleteDeadlines/src/index.js @@ -1,6 +1,14 @@ const { createDbConnection } = require("/opt/nodejs/dbConnection"); +const { + createCorsResponse, + handleOptionsRequest, +} = require("/opt/nodejs/helper"); exports.handler = async (event) => { + if (event.httpMethod === "OPTIONS") { + return handleOptionsRequest(); + } + let connection; try { @@ -16,15 +24,7 @@ exports.handler = async (event) => { console.log("ID ricevuto per delete:", id, "Body:", body); if (!id) { - return { - statusCode: 400, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ message: "ID obbligatorio." }), - }; + return createCorsResponse(400, { message: "ID obbligatorio." }); } connection = await createDbConnection({ @@ -40,43 +40,18 @@ exports.handler = async (event) => { ); if (result.affectedRows === 0) { - return { - statusCode: 404, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ message: "Scadenza non trovata" }), - }; + return createCorsResponse(404, { message: "Scadenza non trovata" }); } - return { - statusCode: 200, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": - "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", - "Access-Control-Allow-Methods": - "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", - "Content-Type": "application/json", - }, - body: JSON.stringify({ message: "Scadenza eliminata con successo" }), - }; + return createCorsResponse(200, { + message: "Scadenza eliminata con successo", + }); } catch (err) { console.error("❌ Errore in deleteDeadlines:", err); - return { - statusCode: 500, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Errore del server. Impossibile eliminare la scadenza.", - error: err.message, - }), - }; + return createCorsResponse(500, { + message: "Errore del server. Impossibile eliminare la scadenza.", + error: err.message, + }); } finally { if (connection) await connection.end(); } diff --git a/amplify/backend/function/deleteUser/src/index.js b/amplify/backend/function/deleteUser/src/index.js index 912f49e..ce086f8 100644 --- a/amplify/backend/function/deleteUser/src/index.js +++ b/amplify/backend/function/deleteUser/src/index.js @@ -1,6 +1,14 @@ const { createDbConnection } = require("/opt/nodejs/dbConnection"); +const { + createCorsResponse, + handleOptionsRequest, +} = require("/opt/nodejs/helper"); exports.handler = async (event) => { + if (event.httpMethod === "OPTIONS") { + return handleOptionsRequest(); + } + let connection; try { @@ -19,34 +27,15 @@ exports.handler = async (event) => { // Elimina l'utente await connection.execute("DELETE FROM users WHERE id = ?", [id]); - return { - statusCode: 200, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": - "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", - "Access-Control-Allow-Methods": - "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: `Utente con id ${id} eliminato con successo.`, - }), - }; + return createCorsResponse(200, { + message: `Utente con id ${id} eliminato con successo.`, + }); } catch (err) { console.error("❌ Errore in deleteUser:", err); - return { - statusCode: 500, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Errore del server durante l'eliminazione dell'utente.", - error: err, - }), - }; + return createCorsResponse(500, { + message: "Errore del server durante l'eliminazione dell'utente.", + error: err.message, + }); } finally { if (connection) { await connection.end(); diff --git a/amplify/backend/function/getAppConfig/src/index.js b/amplify/backend/function/getAppConfig/src/index.js index b060992..7b59bde 100644 --- a/amplify/backend/function/getAppConfig/src/index.js +++ b/amplify/backend/function/getAppConfig/src/index.js @@ -1,7 +1,15 @@ -const { getS3LogoConfig } = require("/opt/nodejs/helper"); +const { + getS3LogoConfig, + createCorsResponse, + handleOptionsRequest, +} = require("/opt/nodejs/helper"); const AWS = require("aws-sdk"); exports.handler = async (event) => { + if (event.httpMethod === "OPTIONS") { + return handleOptionsRequest(); + } + console.log("=== getAppConfig START ==="); console.log("getAppConfig - Event:", JSON.stringify(event, null, 2)); @@ -102,18 +110,7 @@ exports.handler = async (event) => { console.log("Final app config:", appConfig); console.log("=== getAppConfig SUCCESS ==="); - return { - statusCode: 200, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": - "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", - "Access-Control-Allow-Methods": - "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", - "Content-Type": "application/json", - }, - body: JSON.stringify(appConfig), - }; + return createCorsResponse(200, appConfig); } catch (err) { console.error("=== getAppConfig ERROR ==="); console.error("Error details:", { @@ -123,19 +120,11 @@ exports.handler = async (event) => { statusCode: err.statusCode, }); - return { - statusCode: 500, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Errore del server durante il recupero della configurazione.", - error: err.message, - logos: {}, - logoUrl: null, - }), - }; + return createCorsResponse(500, { + message: "Errore del server durante il recupero della configurazione.", + error: err.message, + logos: {}, + logoUrl: null, + }); } }; diff --git a/amplify/backend/function/homecloudSharedUtils/homecloudSharedUtils-awscloudformation-template.json b/amplify/backend/function/homecloudSharedUtils/homecloudSharedUtils-awscloudformation-template.json index 431239c..3b498c7 100644 --- a/amplify/backend/function/homecloudSharedUtils/homecloudSharedUtils-awscloudformation-template.json +++ b/amplify/backend/function/homecloudSharedUtils/homecloudSharedUtils-awscloudformation-template.json @@ -20,7 +20,7 @@ } }, "Resources": { - "LambdaLayerVersiona6cb83be": { + "LambdaLayerVersionbfdad44b": { "Type": "AWS::Lambda::LayerVersion", "Properties": { "CompatibleRuntimes": { @@ -51,12 +51,12 @@ "DeletionPolicy": "Delete", "UpdateReplacePolicy": "Retain" }, - "LambdaLayerPermissionPrivatea6cb83be": { + "LambdaLayerPermissionPrivatebfdad44b": { "Type": "AWS::Lambda::LayerVersionPermission", "Properties": { "Action": "lambda:GetLayerVersion", "LayerVersionArn": { - "Ref": "LambdaLayerVersiona6cb83be" + "Ref": "LambdaLayerVersionbfdad44b" }, "Principal": { "Ref": "AWS::AccountId" @@ -67,7 +67,7 @@ "Outputs": { "Arn": { "Value": { - "Ref": "LambdaLayerVersiona6cb83be" + "Ref": "LambdaLayerVersionbfdad44b" } } } diff --git a/amplify/backend/function/homecloudSharedUtils/lib/nodejs/helper.js b/amplify/backend/function/homecloudSharedUtils/lib/nodejs/helper.js index cc84330..84d7ecc 100644 --- a/amplify/backend/function/homecloudSharedUtils/lib/nodejs/helper.js +++ b/amplify/backend/function/homecloudSharedUtils/lib/nodejs/helper.js @@ -58,8 +58,31 @@ function validateDeadline(data) { return deadlineSchema.validate(data); } +// Funzione helper per le risposte CORS +const createCorsResponse = (statusCode, body) => { + const corsHeaders = { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Headers": + "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", + "Access-Control-Allow-Methods": "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", + "Content-Type": "application/json", + }; + + return { + statusCode, + headers: corsHeaders, + body: JSON.stringify(body), + }; +}; + +const handleOptionsRequest = () => { + return createCorsResponse(200, ""); +}; + module.exports = { getS3AvatarConfig, getS3LogoConfig, validateDeadline, + createCorsResponse, + handleOptionsRequest, }; diff --git a/amplify/backend/function/homecloudSharedUtils/parameters.json b/amplify/backend/function/homecloudSharedUtils/parameters.json index 4f1be15..60247c5 100644 --- a/amplify/backend/function/homecloudSharedUtils/parameters.json +++ b/amplify/backend/function/homecloudSharedUtils/parameters.json @@ -2,5 +2,5 @@ "runtimes": [ "nodejs22.x" ], - "description": "Updated layer version 2025-07-24T17:43:32.969Z" + "description": "Updated layer version 2025-07-25T19:09:59.330Z" } \ No newline at end of file diff --git a/amplify/backend/function/readDeadlines/src/index.js b/amplify/backend/function/readDeadlines/src/index.js index fffdd0a..f5401c3 100644 --- a/amplify/backend/function/readDeadlines/src/index.js +++ b/amplify/backend/function/readDeadlines/src/index.js @@ -1,6 +1,14 @@ const { createDbConnection } = require("/opt/nodejs/dbConnection"); +const { + createCorsResponse, + handleOptionsRequest, +} = require("/opt/nodejs/helper"); exports.handler = async (event) => { + if (event.httpMethod === "OPTIONS") { + return handleOptionsRequest(); + } + let connection; try { @@ -13,32 +21,13 @@ exports.handler = async (event) => { const [results] = await connection.query("SELECT * FROM deadlines"); - return { - statusCode: 200, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": - "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", - "Access-Control-Allow-Methods": - "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", - "Content-Type": "application/json", - }, - body: JSON.stringify(results), - }; + return createCorsResponse(200, results); } catch (err) { console.error("❌ Errore in readDeadlines:", err); - return { - statusCode: 500, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Errore del server. Impossibile ottenere le scadenze.", - error: err.message, - }), - }; + return createCorsResponse(500, { + message: "Errore del server. Impossibile ottenere le scadenze.", + error: err.message, + }); } finally { if (connection) await connection.end(); } diff --git a/amplify/backend/function/readUsers/src/index.js b/amplify/backend/function/readUsers/src/index.js index 5e9465d..636f6b0 100644 --- a/amplify/backend/function/readUsers/src/index.js +++ b/amplify/backend/function/readUsers/src/index.js @@ -1,7 +1,15 @@ const { createDbConnection } = require("/opt/nodejs/dbConnection"); -const { getS3AvatarConfig } = require("/opt/nodejs/helper"); +const { + getS3AvatarConfig, + createCorsResponse, + handleOptionsRequest, +} = require("/opt/nodejs/helper"); exports.handler = async (event) => { + if (event.httpMethod === "OPTIONS") { + return handleOptionsRequest(); + } + let connection; try { @@ -28,30 +36,13 @@ exports.handler = async (event) => { profileImageUrl: `https://${s3Config.bucketName}.s3.${s3Config.region}.amazonaws.com/${s3Config.folderName}/${user.profileImageId}.jpg`, })); - return { - statusCode: 200, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": - "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", - "Access-Control-Allow-Methods": - "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", - }, - body: JSON.stringify(usersWithProfileImage), - }; + return createCorsResponse(200, usersWithProfileImage); } catch (err) { console.error("❌ Errore in readUsers:", err); - return { - statusCode: 500, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - }, - body: JSON.stringify({ - message: "Errore del server durante la lettura degli utenti.", - error: err, - }), - }; + return createCorsResponse(500, { + message: "Errore del server durante la lettura degli utenti.", + error: err.message, + }); } finally { if (connection) { await connection.end(); diff --git a/amplify/backend/function/updateDeadlines/src/index.js b/amplify/backend/function/updateDeadlines/src/index.js index c1256da..6ce7620 100644 --- a/amplify/backend/function/updateDeadlines/src/index.js +++ b/amplify/backend/function/updateDeadlines/src/index.js @@ -1,20 +1,20 @@ const { createDbConnection } = require("/opt/nodejs/dbConnection"); -const { validateDeadline } = require("/opt/nodejs/helper"); +const { + validateDeadline, + createCorsResponse, + handleOptionsRequest, +} = require("/opt/nodejs/helper"); exports.handler = async (event) => { + if (event.httpMethod === "OPTIONS") { + return handleOptionsRequest(); + } + let connection; try { if (!event.body) { - return { - statusCode: 400, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ message: "Richiesta senza body." }), - }; + return createCorsResponse(400, { message: "Richiesta senza body." }); } const body = JSON.parse(event.body); @@ -38,15 +38,9 @@ exports.handler = async (event) => { !user_id || !type ) { - return { - statusCode: 400, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ message: "Tutti i campi sono obbligatori." }), - }; + return createCorsResponse(400, { + message: "Tutti i campi sono obbligatori.", + }); } const { error } = validateDeadline({ @@ -58,15 +52,7 @@ exports.handler = async (event) => { type, }); if (error) { - return { - statusCode: 400, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ message: error.details[0].message }), - }; + return createCorsResponse(400, { message: error.details[0].message }); } connection = await createDbConnection({ @@ -83,45 +69,18 @@ exports.handler = async (event) => { ); if (result.affectedRows === 0) { - return { - statusCode: 404, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ message: "Scadenza non trovata" }), - }; + return createCorsResponse(404, { message: "Scadenza non trovata" }); } - return { - statusCode: 200, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": - "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", - "Access-Control-Allow-Methods": - "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Scadenza aggiornata con successo", - }), - }; + return createCorsResponse(200, { + message: "Scadenza aggiornata con successo", + }); } catch (err) { console.error("❌ Errore in updateDeadlines:", err); - return { - statusCode: 500, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Errore del server. Impossibile aggiornare la scadenza.", - error: err.message, - }), - }; + return createCorsResponse(500, { + message: "Errore del server. Impossibile aggiornare la scadenza.", + error: err.message, + }); } finally { if (connection) await connection.end(); } diff --git a/amplify/backend/function/updateUser/src/index.js b/amplify/backend/function/updateUser/src/index.js index b18e53a..1595b28 100644 --- a/amplify/backend/function/updateUser/src/index.js +++ b/amplify/backend/function/updateUser/src/index.js @@ -1,7 +1,15 @@ const { createDbConnection } = require("/opt/nodejs/dbConnection"); -const { getS3AvatarConfig } = require("/opt/nodejs/helper"); +const { + getS3AvatarConfig, + createCorsResponse, + handleOptionsRequest, +} = require("/opt/nodejs/helper"); exports.handler = async (event) => { + if (event.httpMethod === "OPTIONS") { + return handleOptionsRequest(); + } + let connection; try { @@ -41,32 +49,13 @@ exports.handler = async (event) => { profileImageUrl: `https://${s3Config.bucketName}.s3.${s3Config.region}.amazonaws.com/${s3Config.folderName}/${profileImageId}.jpg`, }; - return { - statusCode: 200, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": - "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", - "Access-Control-Allow-Methods": - "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", - "Content-Type": "application/json", - }, - body: JSON.stringify(updatedUser), - }; + return createCorsResponse(200, updatedUser); } catch (err) { console.error("❌ Errore in updateUser:", err); - return { - statusCode: 500, - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Headers": "*", - "Content-Type": "application/json", - }, - body: JSON.stringify({ - message: "Errore del server durante l'aggiornamento dell'utente.", - error: err, - }), - }; + return createCorsResponse(500, { + message: "Errore del server durante l'aggiornamento dell'utente.", + error: err.message, + }); } finally { if (connection) { await connection.end(); diff --git a/src/pages/AddDeadline.js b/src/pages/AddDeadline.js index 5b9e929..9f43c27 100644 --- a/src/pages/AddDeadline.js +++ b/src/pages/AddDeadline.js @@ -8,6 +8,7 @@ import { faCalendarDay, faChevronDown, faCheck, + faBars, } from "@fortawesome/free-solid-svg-icons"; import { useNavigate } from "react-router-dom"; import axios from "axios"; @@ -72,6 +73,7 @@ const AddDeadline = () => { const [typeDropdownOpen, setTypeDropdownOpen] = useState(false); const [selectedType, setSelectedType] = useState(""); const [isNotificationOn, setIsNotificationOn] = useState(false); + const [isSidebarOpen, setIsSidebarOpen] = useState(false); useEffect(() => { const fetchUser = async () => { @@ -118,6 +120,24 @@ const AddDeadline = () => { setTypeDropdownOpen(false); }; + // Funzione per toggle della sidebar mobile + const toggleSidebar = () => { + setIsSidebarOpen(!isSidebarOpen); + }; + + // Chiude la sidebar quando si clicca su un link (mobile) + const closeSidebar = () => { + setIsSidebarOpen(false); + }; + + // Aggiorna il form quando selectedType cambia + useEffect(() => { + setForm((prev) => ({ + ...prev, + type: selectedType, + })); + }, [selectedType]); + const handleSubmit = async (e) => { e.preventDefault(); setMessage(null); @@ -162,11 +182,28 @@ const AddDeadline = () => { user_id: user.id, }; - await axios.post( + // Debug logs + console.log( + "Request URL:", + `${process.env.REACT_APP_BACKEND_URL}/deadlines` + ); + console.log("Request data:", deadlineData); + console.log("Request headers:", { + "Content-Type": "application/json", + }); + + const response = await axios.post( `${process.env.REACT_APP_BACKEND_URL}/deadlines`, - deadlineData + deadlineData, + { + headers: { + "Content-Type": "application/json", + }, + } ); + console.log("Response:", response); + setMessage({ type: "success", text: "Scadenza aggiunta con successo!", @@ -188,6 +225,11 @@ const AddDeadline = () => { navigate("/dashboard"); }, 1000); } catch (error) { + console.error("Full error:", error); + console.error("Error response:", error.response); + console.error("Error status:", error.response?.status); + console.error("Error data:", error.response?.data); + setMessage({ type: "error", text: @@ -201,11 +243,21 @@ const AddDeadline = () => { return (