0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-16 14:42:45 +00:00

suggestions added, linted

This commit is contained in:
Víctor Losada Hernández
2024-09-13 23:29:36 +02:00
parent 4f2c2916d6
commit dbbfb0b628

View File

@@ -3,11 +3,11 @@ const React = require('react');
const { useState, useRef } = require('react'); const { useState, useRef } = require('react');
const request = require('superagent'); const request = require('superagent');
const NotificationAdd = () => { const NotificationAdd = ()=>{
const [state, setState] = useState({ const [state, setState] = useState({
notificationResult: null, notificationResult : null,
searching: false, searching : false,
error: null, error : null,
}); });
const dismissKeyRef = useRef(null); const dismissKeyRef = useRef(null);
@@ -16,7 +16,7 @@ const NotificationAdd = () => {
const startAtRef = useRef(null); const startAtRef = useRef(null);
const stopAtRef = useRef(null); const stopAtRef = useRef(null);
const saveNotification = async () => { const saveNotification = async ()=>{
const dismissKey = dismissKeyRef.current.value; const dismissKey = dismissKeyRef.current.value;
const title = titleRef.current.value; const title = titleRef.current.value;
const text = textRef.current.value; const text = textRef.current.value;
@@ -24,10 +24,10 @@ const NotificationAdd = () => {
const stopAt = new Date(stopAtRef.current.value); const stopAt = new Date(stopAtRef.current.value);
// Basic validation // Basic validation
if (!dismissKey || !title || !text || !startAt || !stopAt) { if(!dismissKey || !title || !text || !startAt || !stopAt) {
setState(prevState => ({ setState((prevState)=>({
...prevState, ...prevState,
error: 'All fields are required!', error : 'All fields are required!',
})); }));
return; return;
} }
@@ -36,45 +36,31 @@ const NotificationAdd = () => {
dismissKey, dismissKey,
title, title,
text, text,
startAt: startAt ? startAt.toISOString() : '', startAt : startAt?.toISOString() ?? '',
stopAt: stopAt ? stopAt.toISOString() : '', stopAt : stopAt?.stopAt.toISOString() ?? '',
}; };
try { try {
setState(prevState => ({ ...prevState, searching: true, error: null })); setState((prevState)=>({ ...prevState, searching: true, error: null }));
const response = await request.post('/admin/notification/add').send(data); const response = await request.post('/admin/notification/add').send(data);
const notification = response.body; console.log(response.body);
const update = { notificationResult: `Notification successfully created.` };
let update = {
notificationResult: `Created notification: ${JSON.stringify(notification, null, 2)}`,
};
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 // Reset form fields
dismissKeyRef.current.value = ''; dismissKeyRef.current.value = '';
titleRef.current.value = ''; titleRef.current.value = '';
textRef.current.value = ''; textRef.current.value = '';
}
setState(prevState => ({ setState((prevState)=>({
...prevState, ...prevState,
...update, ...update,
searching: false, searching : false,
})); }));
} catch (err) { } catch (err) {
setState(prevState => ({ setState((prevState)=>({
...prevState, ...prevState,
error: `Error saving notification: ${err.message}`, error : `Error saving notification: ${err.message}`,
searching: false, searching : false,
})); }));
} }
}; };
@@ -85,67 +71,43 @@ const NotificationAdd = () => {
<label className='field'> <label className='field'>
Dismiss Key: Dismiss Key:
<input <input className='fieldInput' type='text' ref={dismissKeyRef} required
className='fieldInput'
type='text'
ref={dismissKeyRef}
placeholder='GOOGLEDRIVENOTIF' placeholder='GOOGLEDRIVENOTIF'
required
/> />
</label> </label>
<label className='field'> <label className='field'>
Title: Title:
<input <input className='fieldInput' type='text' ref={titleRef} required
className='fieldInput'
type='text'
ref={titleRef}
placeholder='Stop using Google Drive as image host' placeholder='Stop using Google Drive as image host'
required
/> />
</label> </label>
<label className='field'> <label className='field'>
Text: Text:
<textarea <textarea className='fieldInput' type='text' ref={textRef} required
className='fieldInput'
type='text'
ref={textRef}
placeholder='Google Drive is not an image hosting site, you should not use it as such.' placeholder='Google Drive is not an image hosting site, you should not use it as such.'
required> >
</textarea> </textarea>
</label> </label>
<label className='field'> <label className='field'>
Start Date: Start Date:
<input <input type='date' className='fieldInput' ref={startAtRef} required/>
type="date"
className='fieldInput'
ref={startAtRef}
required
/>
</label> </label>
<label className='field'> <label className='field'>
End Date: End Date:
<input <input type='date' className='fieldInput' ref={stopAtRef} required
type="date"
className='fieldInput'
ref={stopAtRef}
required
/> />
</label> </label>
<div className='notificationResult'>{state.notificationResult}</div> <div className='notificationResult'>{state.notificationResult}</div>
<button className='notificationSave' onClick={saveNotification} disabled={state.searching}> <button className='notificationSave' onClick={saveNotification} disabled={state.searching}>
<i <i className={`fas ${state.searching ? 'fa-spin fa-spinner' : 'fa-save'}`} />
className={'fas', {
'fa-save': !state.searching,
'fa-spin fa-spinner': state.searching,
}}
/>
Save Notification Save Notification
</button> </button>