diff --git a/phone/src/apps/notes/NotesApp.tsx b/phone/src/apps/notes/NotesApp.tsx index 7186f0c..8875d82 100644 --- a/phone/src/apps/notes/NotesApp.tsx +++ b/phone/src/apps/notes/NotesApp.tsx @@ -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 = () => { - + { > - + ); }; diff --git a/phone/src/apps/notes/alert/NotesAlert.tsx b/phone/src/apps/notes/alert/NotesAlert.tsx new file mode 100644 index 0000000..c760082 --- /dev/null +++ b/phone/src/apps/notes/alert/NotesAlert.tsx @@ -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 ( + + + {message} + + + ) +} diff --git a/phone/src/apps/notes/hooks/state.ts b/phone/src/apps/notes/hooks/state.ts index 37c31d5..b5c0ea7 100644 --- a/phone/src/apps/notes/hooks/state.ts +++ b/phone/src/apps/notes/hooks/state.ts @@ -13,4 +13,8 @@ export const noteStates = { key: 'noteDetail', default: null, }), + alert: atom({ + key: 'notesAlert', + default: null + }) }; diff --git a/phone/src/apps/notes/hooks/useNoteAlert.ts b/phone/src/apps/notes/hooks/useNoteAlert.ts new file mode 100644 index 0000000..ba081b2 --- /dev/null +++ b/phone/src/apps/notes/hooks/useNoteAlert.ts @@ -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 } +} \ No newline at end of file diff --git a/phone/src/apps/notes/hooks/useNotesService.ts b/phone/src/apps/notes/hooks/useNotesService.ts index 5e504a6..c4356c4 100644 --- a/phone/src/apps/notes/hooks/useNotesService.ts +++ b/phone/src/apps/notes/hooks/useNotesService.ts @@ -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 }; }; diff --git a/phone/src/apps/notes/modal/NoteModal.tsx b/phone/src/apps/notes/modal/NoteModal.tsx index fb75c6e..7867d2a 100644 --- a/phone/src/apps/notes/modal/NoteModal.tsx +++ b/phone/src/apps/notes/modal/NoteModal.tsx @@ -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 = () => {
{ 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 && ( - )} diff --git a/phone/src/common/typings/notes.ts b/phone/src/common/typings/notes.ts index 2f70474..8d46deb 100644 --- a/phone/src/common/typings/notes.ts +++ b/phone/src/common/typings/notes.ts @@ -1,3 +1,18 @@ +export type AlertCategory = + | 'NOTES_ADD_SUCCESS' + | 'NOTES_ADD_FAILED' + | 'NOTES_UPDATE_SUCCESS' + | 'NOTES_UPDATE_FAILED' + | 'NOTES_DELETE_SUCCESS' + | 'NOTES_DELETE_FAILED' + + +export interface INotesAlert { + alert: AlertCategory; + setAlert: (type: AlertCategory) => void; +} + + export interface Note { id?: number; title: string; diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index 0f0584e..953a6d6 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -84,6 +84,13 @@ "APPS_CONTACT_MODAL_PLACEHOLDER_AVATAR": "Avatar", "APPS_CONTACT_MODAL_BUTTON_ADD": "Add Contact", + "APPS_NOTES_ADD_SUCCESS": "Successfully saved note.", + "APPS_NOTES_ADD_FAILED": "Failed to save note.", + "APPS_NOTES_UPDATE_SUCCESS": "Successfully updated note.", + "APPS_NOTES_UPDATE_FAILED": "Failed to update note.", + "APPS_NOTES_DELETE_SUCCESS": "Successfully deleted note.", + "APPS_NOTES_DELETE_FAILED": "Failed to delete note.", + "APPS_SETTINGS_PHONE_NUMBER": "Phone Number", "APPS_SETTINGS_OPTION_WALLPAPER": "Wallpaper", "APPS_SETTINGS_OPTION_FRAME": "Frame", @@ -92,6 +99,7 @@ "APPS_SETTINGS_OPTION_RINGTONE": "Ringtone", "APPS_SETTINGS_OPTION_RINGTONEVOL": "Ringtone Volume", + "GENERIC_SAVE": "Save", "GENERIC_UPDATE": "Update", "GENERIC_DELETE": "Delete" } diff --git a/resources/client/cl_notes.ts b/resources/client/cl_notes.ts index d1ec160..7547036 100644 --- a/resources/client/cl_notes.ts +++ b/resources/client/cl_notes.ts @@ -1,19 +1,13 @@ import events from '../utils/events'; +import { sendNotesEvent } from '../utils/messages'; onNet(events.NOTE_SEND_NOTE, (notes: any) => { - SendNuiMessage( - JSON.stringify({ - app: 'NOTES', - method: 'setNotes', - data: notes, - }) - ); + sendNotesEvent('setNotes', notes); }); RegisterNuiCallbackType(events.NOTE_ADD_NOTE); on(`__cfx_nui:${events.NOTE_ADD_NOTE}`, (data: any) => { - const note = data; - emitNet(events.NOTE_ADD_NOTE, note); + emitNet(events.NOTE_ADD_NOTE, data); }); onNet(events.NOTE_SEND_NOTE_SUCCESS, () => { @@ -22,18 +16,22 @@ onNet(events.NOTE_SEND_NOTE_SUCCESS, () => { RegisterNuiCallbackType(events.NOTE_DELETE_NOTE); on(`__cfx_nui:${events.NOTE_DELETE_NOTE}`, (data: any) => { - const noteId = data; - emitNet(events.NOTE_DELETE_NOTE, noteId); - + emitNet(events.NOTE_DELETE_NOTE, data); + emitNet(events.NOTE_FETCH_ALL_NOTES); }); RegisterNuiCallbackType(events.NOTE_UPDATE_NOTE); on(`__cfx_nui:${events.NOTE_UPDATE_NOTE}`, (data: any) => { - const note = data; - emitNet(events.NOTE_UPDATE_NOTE, note); + emitNet(events.NOTE_UPDATE_NOTE, data); }); onNet(events.NOTE_UPDATE_NOTE_SUCCESS, () => { emitNet(events.NOTE_FETCH_ALL_NOTES); }); + + +onNet(events.NOTE_ACTION_RESULT, (type: string) => { + console.log(type) + sendNotesEvent('setAlert', type) +}) \ No newline at end of file diff --git a/resources/server/sv_notes.ts b/resources/server/sv_notes.ts index 86a5c08..7e316fc 100644 --- a/resources/server/sv_notes.ts +++ b/resources/server/sv_notes.ts @@ -13,8 +13,7 @@ async function fetchAllNotes(identifier: string): Promise { const query = 'SELECT * FROM npwd_notes WHERE identifier = ? ORDER BY id DESC'; const [result] = await pool.query(query, [identifier]); - const notes = result; - return notes; + return result; } async function deleteNote(noteId: number, identifier: string) { @@ -30,42 +29,55 @@ async function updateNote(note: Note, identifier: string): Promise { onNet(events.NOTE_ADD_NOTE, async (note: Note) => { try { + const pSource = (global as any).source const _identifier = await useIdentifier(); - addNote(_identifier, note); - emitNet(events.NOTE_SEND_NOTE_SUCCESS, getSource()); + await addNote(_identifier, note); + emitNet(events.NOTE_SEND_NOTE_SUCCESS, pSource); + emitNet(events.NOTE_ACTION_RESULT, pSource, 'NOTES_ADD_SUCCESS') + } catch (error) { - console.log('NOTES ERROR: ', error); + const pSource = (global as any).source + emitNet(events.NOTE_ACTION_RESULT, pSource, 'NOTES_ADD_FAILED') } }); onNet(events.NOTE_FETCH_ALL_NOTES, async () => { try { - const _source = (global as any).source; + const pSource = (global as any).source const _identifier = await useIdentifier(); const notes = await fetchAllNotes(_identifier); - emitNet(events.NOTE_SEND_NOTE, _source, notes); + emitNet(events.NOTE_SEND_NOTE, pSource, notes); + } catch (error) { - console.log('NOTE ERROR: ', error); + console.dir(error) } }); onNet(events.NOTE_DELETE_NOTE, async (noteId: NoteId) => { try { + const pSource = (global as any).source; const _identifier = await useIdentifier(); - const id = noteId.id; - await deleteNote(id, _identifier); + await deleteNote(noteId.id, _identifier); + + emitNet(events.NOTE_ACTION_RESULT, pSource, 'NOTES_DELETE_SUCCESS') + } catch (error) { - console.log('NOTE ERROR:', error); + const pSource = (global as any).source; + emitNet(events.NOTE_ACTION_RESULT, pSource, 'NOTES_DELETE_FAILED') } }); onNet(events.NOTE_UPDATE_NOTE, async (note: Note) => { - const _source = (global as any).source; try { + const pSource = (global as any).source; const _identifier = await useIdentifier(); - updateNote(note, _identifier); - emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, _source); + await updateNote(note, _identifier); + emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, pSource); + emitNet(events.NOTE_ACTION_RESULT, pSource, 'NOTES_UPDATE_SUCCESS') + } catch (error) { - emitNet(events.NOTE_UPDATE_NOTE_FAILURE, _source); + const pSource = (global as any).source; + emitNet(events.NOTE_UPDATE_NOTE_FAILURE, pSource); } }); + diff --git a/resources/utils/apps.ts b/resources/utils/apps.ts index 35a0726..ad74728 100644 --- a/resources/utils/apps.ts +++ b/resources/utils/apps.ts @@ -1,4 +1,5 @@ export default { TWITTER: 'TWITTER', MESSAGES: 'MESSAGES', + NOTES: 'NOTES' }; diff --git a/resources/utils/events.ts b/resources/utils/events.ts index 5bb8631..6cdc6fa 100644 --- a/resources/utils/events.ts +++ b/resources/utils/events.ts @@ -76,6 +76,8 @@ export default { NOTE_UPDATE_NOTE: 'phone:updateNote', NOTE_UPDATE_NOTE_SUCCESS: 'phone:updateNoteSuccess', NOTE_UPDATE_NOTE_FAILURE: 'phone:updateNoteFailure', + NOTE_ACTION_RESULT: 'phone:notesActionResult', + // DO NOT CHANGE WHATS BELOW, PLEASE - CHIP OPEN_APP_CONTACTS: 'phone:app:CONTACTS', diff --git a/resources/utils/messages.ts b/resources/utils/messages.ts index 088e771..4c79720 100644 --- a/resources/utils/messages.ts +++ b/resources/utils/messages.ts @@ -17,3 +17,7 @@ export function sendTwitterMessage(method: string, data: any = {}) { export function sendMessageEvent(method: string, data: any = {}) { return sendMessage(apps.MESSAGES, method, data); } + +export function sendNotesEvent(method: string, data: any = {}) { + return sendMessage(apps.NOTES, method, data); +}