From 51d3d11bff00f5c9505fad19d18d0d6603d142ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Losada=20Hern=C3=A1ndez?= Date: Thu, 29 Aug 2024 00:01:02 +0200 Subject: [PATCH] "Refactor notification utils components to use React Hooks instead of createClass" --- .../notificationAdd/notificationAdd.jsx | 184 ++++++++++-------- .../notificationLookup/notificationLookup.jsx | 151 +++++++------- .../notificationUtils/notificationUtils.jsx | 21 +- 3 files changed, 184 insertions(+), 172 deletions(-) diff --git a/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx b/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx index a1480c0a9..f41401377 100644 --- a/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx +++ b/client/admin/notificationUtils/notificationAdd/notificationAdd.jsx @@ -1,101 +1,113 @@ require('./notificationAdd.less'); const React = require('react'); -const createClass = require('create-react-class'); -const cx = require('classnames'); - +const { useState } = require('react'); +const cx = require('classnames'); const request = require('superagent'); const fields = ['dismissKey', 'title', 'text', 'startAt', 'stopAt']; +const NotificationAdd = () => { + const [state, setState] = useState({ + query: '', + notificationResult: null, + searching: false, + error: null, + dismissKey: '', + title: '', + text: '', + startAt: '', + stopAt: '' + }); -const NotificationAdd = createClass({ - displayName : 'NotificationAdd', - getDefaultProps() { - return {}; - }, - getInitialState() { - return { - query : '', - notificationResult : null, - searching : false, - error : null, + const handleChange = (e, field) => { + const value = e.target.value; + setState(prevState => ({ + ...prevState, + [field]: value + })); + }; - dismissKey : '', - title : '', - text : '', - startAt : '', - stopAt : '' - }; - }, - handleChange(e, field){ - const data = {}; - data[field] = e.target.value; - this.setState(data); - }, - saveNotification : async function(){ - if(!this.state.dismissKey) return 'No notification key!'; - const data = { - dismissKey : this.state.dismissKey, - title : this.state.title, - text : this.state.text, - startAt : Date.parse(this.state.startAt), - stopAt : Date.parse(this.state.stopAt) - }; + const saveNotification = async () => { + if (!state.dismissKey) { + setState(prevState => ({ + ...prevState, + error: 'No notification key!' + })); + return; + } - const notification = await request.post('/admin/notification/add') - .send(data) - .then((response)=>{ - return response.body; - }); + const data = { + dismissKey: state.dismissKey, + title: state.title, + text: state.text, + startAt: Date.parse(state.startAt), + stopAt: Date.parse(state.stopAt) + }; - const 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! ${this.state.dismissKey} already exists.`; - } - }; - if(!notification.err) { - update.dismissKey = ''; - update.title = ''; - update.text = ''; - update.startAt = ''; - update.stopAt = ''; - } + try { + const response = await request.post('/admin/notification/add').send(data); + const notification = response.body; + let update = { + notificationResult: `Created notification: ${JSON.stringify(notification, null, 2)}` + }; - console.log(update); + if (notification.err) { + update.notificationResult = JSON.stringify(notification.err); + if (notification.err.code == 11000) { + update.notificationResult = `Duplicate dismissKey error! ${state.dismissKey} already exists.`; + } + } else { + update = { + ...update, + dismissKey: '', + title: '', + text: '', + startAt: '', + stopAt: '' + }; + } - this.setState(update); - }, + setState(prevState => ({ + ...prevState, + ...update, + searching: false + })); + } catch (err) { + setState(prevState => ({ + ...prevState, + error: err.message, + searching: false + })); + } + }; - render(){ - return
-

Add

- {fields.map((field, idx)=>{ - return
- - this.handleChange(e, field)} placeholder={field} /> -
; - })} -
{this.state.notificationResult}
- {/* - - - */} - - - {this.state.error - &&
{this.state.error.toString()}
- } -
; - } -}); + return ( +
+

Add

+ {fields.map((field, idx) => ( +
+ + handleChange(e, field)} + placeholder={field} + /> +
+ ))} +
{state.notificationResult}
+ + {state.error &&
{state.error.toString()}
} +
+ ); +}; module.exports = NotificationAdd; diff --git a/client/admin/notificationUtils/notificationLookup/notificationLookup.jsx b/client/admin/notificationUtils/notificationLookup/notificationLookup.jsx index 4f7c0b3d7..7fa3e980e 100644 --- a/client/admin/notificationUtils/notificationLookup/notificationLookup.jsx +++ b/client/admin/notificationUtils/notificationLookup/notificationLookup.jsx @@ -1,94 +1,97 @@ require('./notificationLookup.less'); + const React = require('react'); -const createClass = require('create-react-class'); -const cx = require('classnames'); +const { useState } = require('react'); +const cx = require('classnames'); const request = require('superagent'); const Moment = require('moment'); +const NotificationLookup = () => { + const [query, setQuery] = useState(''); + const [foundNotification, setFoundNotification] = useState(null); + const [searching, setSearching] = useState(false); + const [error, setError] = useState(null); -const NotificationLookup = createClass({ - displayName : 'NotificationLookup', - getDefaultProps() { - return {}; - }, - getInitialState() { - return { - query : '', - foundNotification : null, - searching : false, - error : null - }; - }, - handleChange(e){ - this.setState({ query: e.target.value }); - }, - lookup(){ - this.setState({ searching: true, error: null }); + const handleChange = (e) => { + setQuery(e.target.value); + }; - request.get(`/admin/notification/lookup/${this.state.query}`) - .then((res)=>this.setState({ foundNotification: res.body })) - .catch((err)=>this.setState({ error: err })) - .finally(()=>this.setState({ searching: false })); - }, + const lookup = () => { + setSearching(true); + setError(null); - deleteNotification : function(){ - console.log('DELETE'); - if(!confirm(`Really delete notification ${this.state.foundNotification.dismissKey} : ${this.state.foundNotification.title}?`)) { - console.log('CANCELLED'); - return; - } - console.log('CONFIRMED'); - return; - }, + request.get(`/admin/notification/lookup/${query}`) + .then((res) => setFoundNotification(res.body)) + .catch((err) => setError(err)) + .finally(() => setSearching(false)); + }; - renderFoundNotification(){ - const notification = this.state.foundNotification; - return
-
-
Key
-
{notification.dismissKey}
+ const deleteNotification = () => { + if (!foundNotification) return; -
Title
-
{notification.title || 'No Title'}
+ const confirmed = window.confirm(`Really delete notification ${foundNotification.dismissKey} : ${foundNotification.title}?`); + if (!confirmed) { + console.log('CANCELLED'); + return; + } + console.log('CONFIRMED'); + // Perform delete operation here + }; -
Text
-
{notification.text || 'No Text'}
+ const renderFoundNotification = () => { + if (!foundNotification) return null; -
Created
-
{Moment(notification.createdAt).toLocaleString()}
+ return ( +
+
+
Key
+
{foundNotification.dismissKey}
-
Start
-
{Moment(notification.startAt).toLocaleString() || 'No Start Time'}
+
Title
+
{foundNotification.title || 'No Title'}
-
Stop
-
{Moment(notification.stopAt).toLocaleString() || 'No End Time'}
-
- -
; - }, +
Text
+
{foundNotification.text || 'No Text'}
- render(){ - return
-

Lookup

- - +
Created
+
{Moment(foundNotification.createdAt).toLocaleString()}
- {this.state.error - &&
{this.state.error.toString()}
- } +
Start
+
{Moment(foundNotification.startAt).toLocaleString() || 'No Start Time'}
- {this.state.foundNotification - ? this.renderFoundNotification() - :
No notification found.
- } -
; - } -}); +
Stop
+
{Moment(foundNotification.stopAt).toLocaleString() || 'No End Time'}
+
+ +
+ ); + }; + + return ( +
+

Lookup

+ + + + {error &&
{error.toString()}
} + + {foundNotification + ? renderFoundNotification() + :
No notification found.
+ } +
+ ); +}; module.exports = NotificationLookup; diff --git a/client/admin/notificationUtils/notificationUtils.jsx b/client/admin/notificationUtils/notificationUtils.jsx index 3e595265b..e3cf87d37 100644 --- a/client/admin/notificationUtils/notificationUtils.jsx +++ b/client/admin/notificationUtils/notificationUtils.jsx @@ -1,19 +1,16 @@ const React = require('react'); -const createClass = require('create-react-class'); const NotificationLookup = require('./notificationLookup/notificationLookup.jsx'); const NotificationAdd = require('./notificationAdd/notificationAdd.jsx'); -const NotificationUtils = createClass({ - displayName : 'NotificationUtils', - - render : function(){ - return <> - -
- - ; - } -}); +const NotificationUtils = () => { + return ( + <> + +
+ + + ); +}; module.exports = NotificationUtils;