From b97cc4102de18329d5a49dbaad83d04e40cf5350 Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Sat, 10 Apr 2021 16:19:25 -0300 Subject: [PATCH] Feature / Add loading state to camera app (#200) * (feat/camera-loading): Add loading state to camera app * Fix imports + Add timeout * Use actual function :D --- .../camera/components/grid/GalleryGrid.tsx | 22 +++++++++---------- .../camera/components/modal/GalleryModal.tsx | 2 +- .../camera/components/modal/ShareModal.tsx | 2 +- phone/src/apps/camera/hooks/state.ts | 2 +- phone/src/apps/camera/hooks/useCamera.ts | 7 +++++- phone/src/apps/camera/hooks/usePhotos.ts | 11 ---------- phone/src/locale/en.json | 3 +++ resources/client/cl_photo.ts | 19 +++++++++++----- resources/server/sv_photo.ts | 9 ++++---- typings/photo.ts | 2 ++ 10 files changed, 43 insertions(+), 36 deletions(-) delete mode 100644 phone/src/apps/camera/hooks/usePhotos.ts diff --git a/phone/src/apps/camera/components/grid/GalleryGrid.tsx b/phone/src/apps/camera/components/grid/GalleryGrid.tsx index f7e682b..0023b08 100644 --- a/phone/src/apps/camera/components/grid/GalleryGrid.tsx +++ b/phone/src/apps/camera/components/grid/GalleryGrid.tsx @@ -1,28 +1,21 @@ import React from 'react'; -import { Box, Button } from '@material-ui/core'; +import { Box, Button, CircularProgress } from '@material-ui/core'; import useStyles from './grid.styles'; -import { usePhotos } from '../../hooks/usePhotos'; +import { useCamera } from '../../hooks/useCamera'; import AddIcon from '@material-ui/icons/Add'; import { useHistory } from 'react-router-dom'; import { useQueryParams } from '../../../../common/hooks/useQueryParams'; import { addQueryToLocation } from '../../../../common/utils/addQueryToLocation'; import { getLocationFromUrl } from '../../../../common/utils/getLocationFromUrl'; -import { PhotoEvents } from '../../../../../../typings/photo'; -import { useNuiRequest } from 'fivem-nui-react-lib'; export const GalleryGrid = () => { - const Nui = useNuiRequest(); const classes = useStyles(); const history = useHistory(); const query = useQueryParams(); const referal = query.referal ? decodeURIComponent(query.referal) : '/camera/image'; - const photos = usePhotos(); - - const handleCamera = () => { - Nui.send(PhotoEvents.TAKE_PHOTO, {}); - }; + const { photos, isLoading, takePhoto } = useCamera(); const handlePhotoOpen = (photo) => { history.push(addQueryToLocation(getLocationFromUrl(referal), 'image', photo.image)); @@ -43,8 +36,13 @@ export const GalleryGrid = () => {
- {photos.map((photo) => ( diff --git a/phone/src/apps/camera/components/modal/GalleryModal.tsx b/phone/src/apps/camera/components/modal/GalleryModal.tsx index e90c43e..b96b68e 100644 --- a/phone/src/apps/camera/components/modal/GalleryModal.tsx +++ b/phone/src/apps/camera/components/modal/GalleryModal.tsx @@ -6,7 +6,7 @@ import DeleteIcon from '@material-ui/icons/Delete'; import ShareIcon from '@material-ui/icons/Share'; import { useHistory } from 'react-router-dom'; import { useQueryParams } from '../../../../common/hooks/useQueryParams'; -import { ICameraPhoto } from '../../hooks/usePhotos'; +import { ICameraPhoto } from '../../hooks/useCamera'; import { ShareModal } from './ShareModal'; import { PhotoEvents } from '../../../../../../typings/photo'; import { useNuiRequest } from 'fivem-nui-react-lib'; diff --git a/phone/src/apps/camera/components/modal/ShareModal.tsx b/phone/src/apps/camera/components/modal/ShareModal.tsx index 0b4b722..100c06c 100644 --- a/phone/src/apps/camera/components/modal/ShareModal.tsx +++ b/phone/src/apps/camera/components/modal/ShareModal.tsx @@ -5,7 +5,7 @@ import MessageIcon from '@material-ui/icons/Message'; import { setClipboard } from '../../../../os/phone/hooks/useClipboard'; import useStyles from './modal.styles'; -import { ICameraPhoto } from '../../hooks/usePhotos'; +import { ICameraPhoto } from '../../hooks/useCamera'; import { useHistory } from 'react-router-dom'; interface IShareModalProps { diff --git a/phone/src/apps/camera/hooks/state.ts b/phone/src/apps/camera/hooks/state.ts index aa6495e..9c70cc8 100644 --- a/phone/src/apps/camera/hooks/state.ts +++ b/phone/src/apps/camera/hooks/state.ts @@ -3,6 +3,6 @@ import { atom } from 'recoil'; export const photoState = { photos: atom({ key: 'galleryPhotos', - default: null, + default: [], }), }; diff --git a/phone/src/apps/camera/hooks/useCamera.ts b/phone/src/apps/camera/hooks/useCamera.ts index 92fe98e..c49f4d6 100644 --- a/phone/src/apps/camera/hooks/useCamera.ts +++ b/phone/src/apps/camera/hooks/useCamera.ts @@ -43,7 +43,7 @@ export const useCamera = (): IUseCamera => { [addAlert, t], ); - const [takePhoto] = useNuiCallback( + const [_takePhoto] = useNuiCallback( 'CAMERA', PhotoEvents.TAKE_PHOTO, onPhotoSuccess, @@ -52,6 +52,11 @@ export const useCamera = (): IUseCamera => { useNuiEvent('CAMERA', PhotoEvents.UPLOAD_PHOTO, () => setUploading(true)); + const takePhoto = () => { + // Timeout at 1 minute + _takePhoto(undefined, { timeout: 60000 }); + }; + return { photos, setPhotos, diff --git a/phone/src/apps/camera/hooks/usePhotos.ts b/phone/src/apps/camera/hooks/usePhotos.ts deleted file mode 100644 index 1bee938..0000000 --- a/phone/src/apps/camera/hooks/usePhotos.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { useRecoilValue } from 'recoil'; -import { photoState } from './state'; - -export interface ICameraPhoto { - id: string; - image: string; -} - -export const usePhotos = (): ICameraPhoto[] | null => { - return useRecoilValue(photoState.photos); -}; diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index 5a5f784..b3a9418 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -62,6 +62,8 @@ "APPS_SELLOUT": "Market", "APPS_CAMERA": "Camera", + "APPS_CAMERA_FAILED_TO_TAKE_PHOTO": "Photo could not be saved :(", + "APPS_DIALER_INCOMING_CALL_TITLE": "Incoming Phone Call", "APPS_DIALER_CURRENT_CALL_TITLE": "Current Phone Call", "APPS_DIALER_CURRENT_CALL_WITH": "In call with {{ transmitter }}", @@ -221,6 +223,7 @@ "APPS_MARKETPLACE_FORM_DESCRIPTION": "Description", "GENERIC_CLICK_TO_REVEAL": "Click to reveal", + "GENERIC_LOADING": "Loading...", "GENERIC_WAIT": "Please wait...", "GENERIC_SAVE": "Save", "GENERIC_UPDATE": "Update", diff --git a/resources/client/cl_photo.ts b/resources/client/cl_photo.ts index dc9f8b9..9068d6e 100644 --- a/resources/client/cl_photo.ts +++ b/resources/client/cl_photo.ts @@ -1,4 +1,4 @@ -import { PhotoEvents } from '../../typings/photo'; +import { IPhoto, PhotoEvents } from '../../typings/photo'; import { Delay } from '../utils/fivem'; import { sendMessage, sendCameraEvent } from '../utils/messages'; const SCREENSHOT_BASIC_TOKEN = GetConvar('SCREENSHOT_BASIC_TOKEN', 'none'); @@ -65,6 +65,7 @@ const handleTakePicture = async () => { DestroyMobilePhone(); CellCamActivate(false, false); openPhoneTemp(); + sendCameraEvent(PhotoEvents.UPLOAD_PHOTO, true); inCameraMode = false; }; @@ -72,6 +73,7 @@ const handleCameraExit = () => { DestroyMobilePhone(); CellCamActivate(false, false); openPhoneTemp(); + sendCameraEvent(PhotoEvents.TAKE_PHOTO_SUCCESS, false); inCameraMode = false; }; @@ -94,14 +96,21 @@ function takePhoto() { }, }, (data: string) => { - const imageLink = JSON.parse(data).data.link; - emitNet(PhotoEvents.UPLOAD_PHOTO, imageLink); + try { + const imageLink = JSON.parse(data).data.link; + emitNet(PhotoEvents.UPLOAD_PHOTO, imageLink); + } catch (e) { + sendCameraEvent(PhotoEvents.TAKE_PHOTO_ERROR, 'APPS_CAMERA_FAILED_TO_TAKE_PHOTO'); + } }, ); } -onNet(PhotoEvents.UPLOAD_PHOTO_SUCCESS, () => { - emitNet(PhotoEvents.FETCH_PHOTOS); +onNet(PhotoEvents.UPLOAD_PHOTO_SUCCESS, (photo: IPhoto) => { + if (photo && photo.image) { + return sendCameraEvent(PhotoEvents.TAKE_PHOTO_SUCCESS, photo); + } + sendCameraEvent(PhotoEvents.TAKE_PHOTO_ERROR, 'APPS_CAMERA_FAILED_TO_TAKE_PHOTO'); }); // delete photo diff --git a/resources/server/sv_photo.ts b/resources/server/sv_photo.ts index c4fe45f..9c18270 100644 --- a/resources/server/sv_photo.ts +++ b/resources/server/sv_photo.ts @@ -5,9 +5,10 @@ import { mainLogger } from './sv_logger'; const photoLogger = mainLogger.child({ module: 'photo' }); -async function uploadPhoto(identifier: string, image: string) { +async function uploadPhoto(identifier: string, image: string): Promise { const query = 'INSERT INTO npwd_phone_gallery (identifier, image) VALUES (?, ?)'; - await pool.query(query, [identifier, image]); + const [results] = (await pool.query(query, [identifier, image])) as any; + return { id: results.insertId, image }; } async function getPhotosByIdentifier(identifier: string): Promise { @@ -25,8 +26,8 @@ onNet(PhotoEvents.UPLOAD_PHOTO, async (image: string) => { const _source = getSource(); try { const identifier = getIdentifier(_source); - await uploadPhoto(identifier, image); - emitNet(PhotoEvents.UPLOAD_PHOTO_SUCCESS, _source); + const photo = await uploadPhoto(identifier, image); + emitNet(PhotoEvents.UPLOAD_PHOTO_SUCCESS, _source, photo); } catch (e) { photoLogger.error(`Failed to upload photo, ${e.message}`, { source: _source, diff --git a/typings/photo.ts b/typings/photo.ts index 1d34f65..8b49b0a 100644 --- a/typings/photo.ts +++ b/typings/photo.ts @@ -5,6 +5,8 @@ export interface IPhoto { export enum PhotoEvents { TAKE_PHOTO = 'npwd:TakePhoto', + TAKE_PHOTO_SUCCESS = 'npwd:TakePhotoSuccess', + TAKE_PHOTO_ERROR = 'npwd:TakePhotoError', UPLOAD_PHOTO = 'npwd:UploadPhoto', UPLOAD_PHOTO_SUCCESS = 'npwd:UploadPhotoSuccess', FETCH_PHOTOS = 'npwd:FetchPhotos',