Merge pull request #79 from project-error/fix/notes-handling

Fix/notes handling
This commit is contained in:
Christopher Gjelten authored and GitHub committed 2021-02-08 22:34:22 +01:00
commit be754a3dc6
13 files changed
+170 -43

No files matched your search

+5 -3
View File
@@ -15,6 +15,7 @@ import { useNoteDetail } from './hooks/useNoteDetail';
import useStyles from './notes.styles';
import InjectDebugData from '../../os/debug/InjectDebugData';
import { NotesAlert } from './alert/NotesAlert';
InjectDebugData([
{
@@ -32,7 +33,7 @@ InjectDebugData([
content: 'Hello, this is another shitty note',
},
],
},
}
]);
export const NotesApp = () => {
@@ -54,7 +55,8 @@ export const NotesApp = () => {
<NoteModal key={detail?.id} />
<AppContent>
<NoteList notes={notes} />
<Fab
</AppContent>
<Fab
className={classes.absolute}
onClick={handleModal}
style={{
@@ -64,7 +66,7 @@ export const NotesApp = () => {
>
<AddIcon />
</Fab>
</AppContent>
<NotesAlert />
</AppWrapper>
);
};
+62
View File
@@ -0,0 +1,62 @@
import React, { useState, useEffect, useCallback } from 'react'
import { Snackbar } from '@material-ui/core';
import { Alert } from '../../../ui/components/Alert';
import { AlertCategory } from '../../../common/typings/notes';
import { useTranslation } from 'react-i18next';
import { useNotesAlert } from '../hooks/useNoteAlert';
const LOCALE_PREFIX = 'APPS_';
export const NotesAlert = () => {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const { alert, setAlert } = useNotesAlert();
const [message, setMessage] = useState(null);
const [severity, setSeverity] = useState('');
const _handleClose = () => {
setOpen(false)
setAlert(null)
}
const setMessageAlert = useCallback((type: AlertCategory, severity: 'success' | 'error') => {
setMessage(t(LOCALE_PREFIX + type));
setSeverity(severity)
setOpen(true)
}, [t])
useEffect(() => {
switch (alert) {
case 'NOTES_ADD_SUCCESS':
setMessageAlert(alert, 'success');
break;
case 'NOTES_ADD_FAILED':
setMessageAlert(alert, 'error');
break;
case 'NOTES_UPDATE_SUCCESS':
setMessageAlert(alert, 'success');
break;
case 'NOTES_UPDATE_FAILED':
setMessageAlert(alert, 'error');
break;
case 'NOTES_DELETE_SUCCESS':
setMessageAlert(alert, 'success');
break;
case 'NOTES_DELETE_FAILED':
setMessageAlert(alert, 'error')
}
}, [alert, setMessageAlert])
return (
<Snackbar
style={{ width: '80%', position: 'absolute', bottom: '10%' }}
autoHideDuration={1500}
open={open}
onClose={_handleClose}
>
<Alert severity={severity} variant='filled'>
{message}
</Alert>
</Snackbar>
)
}
+4
View File
@@ -13,4 +13,8 @@ export const noteStates = {
key: 'noteDetail',
default: null,
}),
alert: atom({
key: 'notesAlert',
default: null
})
};
@@ -0,0 +1,8 @@
import { useRecoilState } from 'recoil';
import { INotesAlert } from '../../../common/typings/notes';
import { noteStates } from './state';
export const useNotesAlert = (): INotesAlert => {
const [alert, setAlert] = useRecoilState(noteStates.alert)
return { alert, setAlert }
}
@@ -2,9 +2,12 @@ import { useSetRecoilState } from 'recoil';
import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
import { useNotes } from './useNotes';
import { noteStates } from './state';
import { useNotesAlert } from './useNoteAlert';
export const useNotesService = () => {
const setNotes = useSetRecoilState(noteStates.noteItems);
const setAlert = useSetRecoilState(noteStates.alert);
useNuiEvent('NOTES', 'setNotes', setNotes);
return useNotes();
useNuiEvent('NOTES', 'setAlert', setAlert)
return { useNotes, useNotesAlert };
};
+18 -10
View File
@@ -1,5 +1,5 @@
import { Button, TextField, Slide } from '@material-ui/core';
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import ArrowBackIcon from '@material-ui/icons/ArrowBack';
import { useNoteModal } from '../hooks/useNoteModal';
@@ -14,33 +14,37 @@ import Nui from '../../../os/nui-events/utils/Nui';
export const NoteModal = () => {
const { noteModal, setNoteModal } = useNoteModal();
const { detail, setDetail } = useNoteDetail();
console.log(detail);
const [title, setTitle] = useState(detail ? detail.title : '');
const [content, setContent] = useState(detail ? detail.content : '');
const classes = useStyles();
const classes = useStyles()
const _handleClose = () => {
setNoteModal(false);
setDetail(null);
setNoteModal(false);
};
const handleNoteSave = () => {
setNoteModal(false);
setDetail(null);
Nui.send('phone:addNote', {
title,
content,
});
setDetail(null);
setNoteModal(false);
setTitle('')
setContent('')
};
const handleDeleteNote = () => {
const id = detail.id;
setNoteModal(false);
Nui.send('phone:deleteNote', {
id,
});
setDetail(null);
setNoteModal(false);
};
const handleUpdateNote = () => {
@@ -49,8 +53,8 @@ export const NoteModal = () => {
title,
content,
});
setNoteModal(false);
setDetail(null);
setNoteModal(false);
};
return (
@@ -63,9 +67,11 @@ export const NoteModal = () => {
<div id='notes-modal' className={classes.noteContainer}>
<TextField
className={classes.input}
rowsMax={1}
placeholder='Title'
inputProps={{
className: classes.inputPropsTitle,
maxLength: 25
}}
fullWidth
value={title}
@@ -75,6 +81,7 @@ export const NoteModal = () => {
className={classes.input}
inputProps={{
className: classes.inputPropsContent,
maxLength: 250,
}}
placeholder='Content'
multiline
@@ -84,8 +91,9 @@ export const NoteModal = () => {
value={content}
onChange={(e) => setContent(e.target.value)}
/>
{!detail && (
<Button className={classes.saveButton} onClick={handleNoteSave}>
<p style={{ color: "#fff" }}>{content.length}/250</p>
{!detail &&(
<Button disabled={title.length > 0 ? false : true} className={classes.saveButton} onClick={handleNoteSave}>
Save
</Button>
)}