0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-05 08:02:42 +00:00

use actual inputs and textarea with good attributes

This commit is contained in:
Víctor Losada Hernández
2024-08-29 00:23:22 +02:00
parent 51d3d11bff
commit 0c6c0c9fd6

View File

@@ -1,111 +1,151 @@
require('./notificationAdd.less');
const React = require('react'); const React = require('react');
const { useState } = require('react'); const { useState, useRef } = require('react');
const cx = require('classnames'); const cx = require('classnames');
const request = require('superagent'); const request = require('superagent');
const fields = ['dismissKey', 'title', 'text', 'startAt', 'stopAt'];
const NotificationAdd = () => { const NotificationAdd = () => {
const [state, setState] = useState({ const [state, setState] = useState({
query: '',
notificationResult: null, notificationResult: null,
searching: false, searching: false,
error: null, error: null,
dismissKey: '',
title: '',
text: '',
startAt: '',
stopAt: ''
}); });
const handleChange = (e, field) => { const dismissKeyRef = useRef(null);
const value = e.target.value; const titleRef = useRef(null);
setState(prevState => ({ const textRef = useRef(null);
...prevState, const [startAt, setStartAt] = useState(null);
[field]: value const [stopAt, setStopAt] = useState(null);
}));
};
const saveNotification = async () => { const saveNotification = async () => {
if (!state.dismissKey) { const dismissKey = dismissKeyRef.current.value;
const title = titleRef.current.value;
const text = textRef.current.value;
// Basic validation
if (!dismissKey || !title || !text || !startAt || !stopAt) {
setState(prevState => ({ setState(prevState => ({
...prevState, ...prevState,
error: 'No notification key!' error: 'All fields are required!',
})); }));
return; return;
} }
const data = { const data = {
dismissKey: state.dismissKey, dismissKey,
title: state.title, title,
text: state.text, text,
startAt: Date.parse(state.startAt), startAt: startAt ? startAt.toISOString() : '',
stopAt: Date.parse(state.stopAt) stopAt: stopAt ? stopAt.toISOString() : '',
}; };
try { try {
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; const notification = response.body;
let update = { let update = {
notificationResult: `Created notification: ${JSON.stringify(notification, null, 2)}` notificationResult: `Created notification: ${JSON.stringify(notification, null, 2)}`,
}; };
if (notification.err) { if (notification.err) {
update.notificationResult = JSON.stringify(notification.err); update.notificationResult = JSON.stringify(notification.err);
if (notification.err.code == 11000) { if (notification.err.code === 11000) {
update.notificationResult = `Duplicate dismissKey error! ${state.dismissKey} already exists.`; update.notificationResult = `Duplicate dismissKey error! ${dismissKey} already exists.`;
} }
} else { } else {
update = { update = {
...update, ...update,
dismissKey: '', notificationResult: `Notification successfully created.`,
title: '',
text: '',
startAt: '',
stopAt: ''
}; };
// Reset form fields
dismissKeyRef.current.value = '';
titleRef.current.value = '';
textRef.current.value = '';
setStartAt(null);
setStopAt(null);
} }
setState(prevState => ({ setState(prevState => ({
...prevState, ...prevState,
...update, ...update,
searching: false searching: false,
})); }));
} catch (err) { } catch (err) {
setState(prevState => ({ setState(prevState => ({
...prevState, ...prevState,
error: err.message, error: `Error saving notification: ${err.message}`,
searching: false searching: false,
})); }));
} }
}; };
return ( return (
<div className='notificationAdd'> <div className='notificationAdd'>
<h2>Add</h2> <h2>Add Notification</h2>
{fields.map((field, idx) => (
<div key={idx}> <label className='fieldLabel'>
<label className='fieldLabel'>{field.toUpperCase()}</label> DISMISSKEY
<input <input
className='fieldInput' className='fieldInput'
type='text' type='text'
value={state[field]} ref={dismissKeyRef}
onChange={(e) => handleChange(e, field)} placeholder='GOOGLEDRIVENOTIF'
placeholder={field} />
/> </label>
</div>
))} <label className='fieldLabel'>
TITLE
<input
className='fieldInput'
type='text'
ref={titleRef}
placeholder='Stop using Google Drive as image host'
/>
</label>
<label className='fieldLabel'>
TEXT
<textarea
className='fieldInput'
type='text'
ref={textRef}
placeholder='Google Drive is not an image hosting site, you should not use it as such.'>
</textarea>
</label>
<label className='fieldLabel'>
STARTAT
<input
type="date"
className='fieldInput'
selected={startAt}
onChange={date => setStartAt(date)}
/>
</label>
<label className='fieldLabel'>
STOPAT
<input
type="date"
className='fieldInput'
selected={stopAt}
onChange={date => setStopAt(date)}
/>
</label>
<div className='notificationResult'>{state.notificationResult}</div> <div className='notificationResult'>{state.notificationResult}</div>
<button onClick={saveNotification}>
<button className='notificationSave' onClick={saveNotification} disabled={state.searching}>
<i <i
className={cx('fas', { className={cx('fas', {
'fa-save': !state.searching, 'fa-save': !state.searching,
'fa-spin fa-spinner': state.searching 'fa-spin fa-spinner': state.searching,
})} })}
/> />
Save Notification
</button> </button>
{state.error && <div className='error'>{state.error.toString()}</div>}
{state.error && <div className='error'>{state.error}</div>}
</div> </div>
); );
}; };