From dbbfb0b62826a61df238c691414131853efbab30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Losada=20Hern=C3=A1ndez?= Date: Fri, 13 Sep 2024 23:29:36 +0200 Subject: [PATCH] suggestions added, linted --- .../notificationAdd/notificationAdd.jsx | 212 +++++++----------- 1 file changed, 87 insertions(+), 125 deletions(-) diff --git a/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx b/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx index 3d8571cf8..78862eb8e 100644 --- a/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx +++ b/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx @@ -3,155 +3,117 @@ const React = require('react'); const { useState, useRef } = require('react'); const request = require('superagent'); -const NotificationAdd = () => { - const [state, setState] = useState({ - notificationResult: null, - searching: false, - error: null, - }); +const NotificationAdd = ()=>{ + const [state, setState] = useState({ + notificationResult : null, + searching : false, + error : null, + }); - const dismissKeyRef = useRef(null); - const titleRef = useRef(null); - const textRef = useRef(null); - const startAtRef = useRef(null); - const stopAtRef = useRef(null); + const dismissKeyRef = useRef(null); + const titleRef = useRef(null); + const textRef = useRef(null); + const startAtRef = useRef(null); + const stopAtRef = useRef(null); - const saveNotification = async () => { - const dismissKey = dismissKeyRef.current.value; - const title = titleRef.current.value; - const text = textRef.current.value; - const startAt = new Date(startAtRef.current.value); - const stopAt = new Date(stopAtRef.current.value); + const saveNotification = async ()=>{ + const dismissKey = dismissKeyRef.current.value; + const title = titleRef.current.value; + const text = textRef.current.value; + const startAt = new Date(startAtRef.current.value); + const stopAt = new Date(stopAtRef.current.value); - // Basic validation - if (!dismissKey || !title || !text || !startAt || !stopAt) { - setState(prevState => ({ - ...prevState, - error: 'All fields are required!', - })); - return; - } + // Basic validation + if(!dismissKey || !title || !text || !startAt || !stopAt) { + setState((prevState)=>({ + ...prevState, + error : 'All fields are required!', + })); + return; + } - const data = { - dismissKey, - title, - text, - startAt: startAt ? startAt.toISOString() : '', - stopAt: stopAt ? stopAt.toISOString() : '', - }; + const data = { + dismissKey, + title, + text, + startAt : startAt?.toISOString() ?? '', + stopAt : stopAt?.stopAt.toISOString() ?? '', + }; - try { - setState(prevState => ({ ...prevState, searching: true, error: null })); - const response = await request.post('/admin/notification/add').send(data); - const notification = response.body; + try { + setState((prevState)=>({ ...prevState, searching: true, error: null })); + const response = await request.post('/admin/notification/add').send(data); + console.log(response.body); + const update = { notificationResult: `Notification successfully created.` }; - let update = { - notificationResult: `Created notification: ${JSON.stringify(notification, null, 2)}`, - }; + // Reset form fields + dismissKeyRef.current.value = ''; + titleRef.current.value = ''; + textRef.current.value = ''; - if (notification.err) { - update.notificationResult = JSON.stringify(notification.err); - if (notification.err.code === 11000) { - update.notificationResult = `Duplicate dismissKey error! ${dismissKey} already exists.`; - } - } else { - update = { - ...update, - notificationResult: `Notification successfully created.`, - }; - // Reset form fields - dismissKeyRef.current.value = ''; - titleRef.current.value = ''; - textRef.current.value = ''; - } + setState((prevState)=>({ + ...prevState, + ...update, + searching : false, + })); + } catch (err) { + setState((prevState)=>({ + ...prevState, + error : `Error saving notification: ${err.message}`, + searching : false, + })); + } + }; - setState(prevState => ({ - ...prevState, - ...update, - searching: false, - })); - } catch (err) { - setState(prevState => ({ - ...prevState, - error: `Error saving notification: ${err.message}`, - searching: false, - })); - } - }; + return ( +
+

Add Notification

- return ( -
-

Add Notification

- - - - - - -
{state.notificationResult}
+
{state.notificationResult}
- + - {state.error &&
{state.error}
} -
- ); + {state.error &&
{state.error}
} +
+ ); }; module.exports = NotificationAdd;