mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-06 18:42:40 +00:00
suggestions added, linted
This commit is contained in:
@@ -3,155 +3,117 @@ 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);
|
||||||
const titleRef = useRef(null);
|
const titleRef = useRef(null);
|
||||||
const textRef = useRef(null);
|
const textRef = useRef(null);
|
||||||
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;
|
||||||
const startAt = new Date(startAtRef.current.value);
|
const startAt = new Date(startAtRef.current.value);
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
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 = {
|
// Reset form fields
|
||||||
notificationResult: `Created notification: ${JSON.stringify(notification, null, 2)}`,
|
dismissKeyRef.current.value = '';
|
||||||
};
|
titleRef.current.value = '';
|
||||||
|
textRef.current.value = '';
|
||||||
|
|
||||||
if (notification.err) {
|
setState((prevState)=>({
|
||||||
update.notificationResult = JSON.stringify(notification.err);
|
...prevState,
|
||||||
if (notification.err.code === 11000) {
|
...update,
|
||||||
update.notificationResult = `Duplicate dismissKey error! ${dismissKey} already exists.`;
|
searching : false,
|
||||||
}
|
}));
|
||||||
} else {
|
} catch (err) {
|
||||||
update = {
|
setState((prevState)=>({
|
||||||
...update,
|
...prevState,
|
||||||
notificationResult: `Notification successfully created.`,
|
error : `Error saving notification: ${err.message}`,
|
||||||
};
|
searching : false,
|
||||||
// Reset form fields
|
}));
|
||||||
dismissKeyRef.current.value = '';
|
}
|
||||||
titleRef.current.value = '';
|
};
|
||||||
textRef.current.value = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
setState(prevState => ({
|
return (
|
||||||
...prevState,
|
<div className='notificationAdd'>
|
||||||
...update,
|
<h2>Add Notification</h2>
|
||||||
searching: false,
|
|
||||||
}));
|
|
||||||
} catch (err) {
|
|
||||||
setState(prevState => ({
|
|
||||||
...prevState,
|
|
||||||
error: `Error saving notification: ${err.message}`,
|
|
||||||
searching: false,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
<label className='field'>
|
||||||
<div className='notificationAdd'>
|
|
||||||
<h2>Add Notification</h2>
|
|
||||||
|
|
||||||
<label className='field'>
|
|
||||||
Dismiss Key:
|
Dismiss Key:
|
||||||
<input
|
<input className='fieldInput' type='text' ref={dismissKeyRef} required
|
||||||
className='fieldInput'
|
placeholder='GOOGLEDRIVENOTIF'
|
||||||
type='text'
|
|
||||||
ref={dismissKeyRef}
|
/>
|
||||||
placeholder='GOOGLEDRIVENOTIF'
|
</label>
|
||||||
required
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label className='field'>
|
<label className='field'>
|
||||||
Title:
|
Title:
|
||||||
<input
|
<input className='fieldInput' type='text' ref={titleRef} required
|
||||||
className='fieldInput'
|
placeholder='Stop using Google Drive as image host'
|
||||||
type='text'
|
/>
|
||||||
ref={titleRef}
|
</label>
|
||||||
placeholder='Stop using Google Drive as image host'
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</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"
|
</label>
|
||||||
className='fieldInput'
|
|
||||||
ref={startAtRef}
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label className='field'>
|
<label className='field'>
|
||||||
End Date:
|
End Date:
|
||||||
<input
|
<input type='date' className='fieldInput' ref={stopAtRef} required
|
||||||
type="date"
|
/>
|
||||||
className='fieldInput'
|
</label>
|
||||||
ref={stopAtRef}
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</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>
|
||||||
|
|
||||||
{state.error && <div className='error'>{state.error}</div>}
|
{state.error && <div className='error'>{state.error}</div>}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = NotificationAdd;
|
module.exports = NotificationAdd;
|
||||||
|
|||||||
Reference in New Issue
Block a user