mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-14 02:02:51 +00:00
Split state into separate states.
This commit is contained in:
@@ -4,11 +4,9 @@ const { useState, useRef } = require('react');
|
|||||||
const request = require('superagent');
|
const request = require('superagent');
|
||||||
|
|
||||||
const NotificationAdd = ()=>{
|
const NotificationAdd = ()=>{
|
||||||
const [state, setState] = useState({
|
const [notificationResult, setNotificationResult] = useState(null);
|
||||||
notificationResult : null,
|
const [searching, setSearching] = useState(false);
|
||||||
searching : false,
|
const [error, setError] = useState(null);
|
||||||
error : null,
|
|
||||||
});
|
|
||||||
|
|
||||||
const dismissKeyRef = useRef(null);
|
const dismissKeyRef = useRef(null);
|
||||||
const titleRef = useRef(null);
|
const titleRef = useRef(null);
|
||||||
@@ -25,17 +23,11 @@ const NotificationAdd = ()=>{
|
|||||||
|
|
||||||
// Basic validation
|
// Basic validation
|
||||||
if(!dismissKey || !title || !text || isNaN(startAt.getTime()) || isNaN(stopAt.getTime())) {
|
if(!dismissKey || !title || !text || isNaN(startAt.getTime()) || isNaN(stopAt.getTime())) {
|
||||||
setState((prevState)=>({
|
setError('All fields are required');
|
||||||
...prevState,
|
|
||||||
error : 'All fields are required',
|
|
||||||
}));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(startAt >= stopAt) {
|
if (startAt >= stopAt) {
|
||||||
setState((prevState)=>({
|
setError('End date must be after the start date!');
|
||||||
...prevState,
|
|
||||||
error : 'End date must be after the start date!',
|
|
||||||
}));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +40,8 @@ const NotificationAdd = ()=>{
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setState((prevState)=>({ ...prevState, searching: true, error: null }));
|
setSearching(true);
|
||||||
|
setError(null);
|
||||||
const response = await request.post('/admin/notification/add').send(data);
|
const response = await request.post('/admin/notification/add').send(data);
|
||||||
console.log(response.body);
|
console.log(response.body);
|
||||||
|
|
||||||
@@ -57,18 +50,12 @@ const NotificationAdd = ()=>{
|
|||||||
titleRef.current.value = '';
|
titleRef.current.value = '';
|
||||||
textRef.current.value = '';
|
textRef.current.value = '';
|
||||||
|
|
||||||
setState((prevState)=>({
|
setNotificationResult('Notification successfully created.');
|
||||||
...prevState,
|
setSearching(false);
|
||||||
notificationResult : `Notification successfully created.`,
|
} catch (err) {
|
||||||
searching : false,
|
|
||||||
}));
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error.response.body.message);
|
console.log(error.response.body.message);
|
||||||
setState((prevState)=>({
|
setError(`Error saving notification: ${err.response.body.message}`);
|
||||||
...prevState,
|
setSearching(false);
|
||||||
error : `Error saving notification: ${error.response.body.message}`,
|
|
||||||
searching : false,
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -108,13 +95,13 @@ const NotificationAdd = ()=>{
|
|||||||
<input type='date' className='fieldInput' ref={stopAtRef} required/>
|
<input type='date' className='fieldInput' ref={stopAtRef} required/>
|
||||||
</label>
|
</label>
|
||||||
|
|
||||||
<div className='notificationResult'>{state.notificationResult}</div>
|
<div className='notificationResult'>{notificationResult}</div>
|
||||||
|
|
||||||
<button className='notificationSave' onClick={saveNotification} disabled={state.searching}>
|
<button className='notificationSave' onClick={saveNotification} disabled={searching}>
|
||||||
<i className={`fas ${state.searching ? 'fa-spin fa-spinner' : 'fa-save'}`}/>
|
<i className={`fas ${searching ? 'fa-spin fa-spinner' : 'fa-save'}`}/>
|
||||||
Save Notification
|
Save Notification
|
||||||
</button>
|
</button>
|
||||||
{state.error && <div className='error'>{state.error}</div>}
|
{error && <div className='error'>{error}</div>}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user