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
This commit is contained in:
J Francisco Rader authored and GitHub committed 2021-04-10 16:19:25 -03:00
1 parent e1e6e8440d
commit b97cc4102d
10 files changed
+43 -36

No files matched your search

@@ -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 = () => {
<div>
<Box display="flex" flexWrap="wrap" alignContent="flex-start" className={classes.root}>
<Box>
<Button onClick={handleCamera} style={{ borderRadius: 0 }} className={classes.photo}>
<AddIcon fontSize="large" />
<Button
disabled={isLoading}
onClick={takePhoto}
style={{ borderRadius: 0 }}
className={classes.photo}
>
{isLoading ? <CircularProgress /> : <AddIcon fontSize="large" />}
</Button>
</Box>
{photos.map((photo) => (
@@ -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';
@@ -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 {
+1 -1
View File
@@ -3,6 +3,6 @@ import { atom } from 'recoil';
export const photoState = {
photos: atom({
key: 'galleryPhotos',
default: null,
default: [],
}),
};
+6 -1
View File
@@ -43,7 +43,7 @@ export const useCamera = (): IUseCamera => {
[addAlert, t],
);
const [takePhoto] = useNuiCallback<void, ICameraPhoto>(
const [_takePhoto] = useNuiCallback<void, ICameraPhoto>(
'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,
-11
View File
@@ -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<ICameraPhoto[] | null>(photoState.photos);
};
+3
View File
@@ -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",
+14 -5
View File
@@ -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
+5 -4
View File
@@ -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<IPhoto> {
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<IPhoto[]> {
@@ -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,
+2
View File
@@ -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',