feat(camera): refactor to new promise event format

This commit is contained in:
Christopher authored and Taso committed 2021-07-27 17:04:28 -04:00
1 parent 1566bb0fdb
commit 4e8c57492b
15 files changed
+274 -141

No files matched your search

+1 -1
View File
@@ -70,7 +70,7 @@ function Phone() {
useMarketplaceService();
useBankService();
useMessagesService();
usePhotoService();
/*usePhotoService();*/
useCallService();
useDialService();
@@ -6,6 +6,7 @@ import { GalleryModal } from './modal/GalleryModal';
import InjectDebugData from '../../../os/debug/InjectDebugData';
import { Route, Switch } from 'react-router-dom';
import { PhotoEvents } from '../../../../../typings/photo';
import { LoadingSpinner } from '../../../ui/components/LoadingSpinner';
InjectDebugData([
{
@@ -29,8 +30,10 @@ const CameraApp = () => {
<AppWrapper id="camera-app">
<AppContent>
<Switch>
<Route path="/camera" exact component={GalleryGrid} />
<Route path="/camera/image" exact component={GalleryModal} />
<React.Suspense fallback={<LoadingSpinner />}>
<Route path="/camera" exact component={GalleryGrid} />
<Route path="/camera/image" exact component={GalleryModal} />
</React.Suspense>
</Switch>
</AppContent>
</AppWrapper>
@@ -7,20 +7,42 @@ 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 { fetchNui } from '../../../../utils/fetchNui';
import { ServerPromiseResp } from '../../../../../../typings/common';
import { GalleryPhoto, PhotoEvents } from '../../../../../../typings/photo';
import { usePhotoActions } from '../../hooks/usePhotoActions';
import { usePhotosValue } from '../../hooks/state';
import { useSnackbar } from '../../../../ui/hooks/useSnackbar';
import { useTranslation } from 'react-i18next';
export const GalleryGrid = () => {
const classes = useStyles();
const history = useHistory();
const query = useQueryParams();
const { addAlert } = useSnackbar();
const { t } = useTranslation();
const photos = usePhotosValue();
const { takePhoto } = usePhotoActions();
const referal = query.referal ? decodeURIComponent(query.referal) : '/camera/image';
const { photos, isLoading, takePhoto } = useCamera();
const handlePhotoOpen = (photo) => {
history.push(addQueryToLocation(getLocationFromUrl(referal), 'image', photo.image));
};
const handleTakePhoto = () => {
fetchNui<ServerPromiseResp<GalleryPhoto>>(PhotoEvents.TAKE_PHOTO).then((serverResp) => {
if (serverResp.status !== 'ok') {
return addAlert({
message: t('APPS_CAMERA_FAILED_TO_TAKE_PHOTO'),
type: 'error',
});
}
takePhoto(serverResp.data);
});
};
if (!photos)
return (
<Box display="flex" flexWrap="wrap" alignContent="flex-start" className={classes.root}>
@@ -36,13 +58,8 @@ export const GalleryGrid = () => {
<div>
<Box display="flex" flexWrap="wrap" alignContent="flex-start" className={classes.root}>
<Box>
<Button
disabled={isLoading}
onClick={takePhoto}
style={{ borderRadius: 0 }}
className={classes.photo}
>
{isLoading ? <CircularProgress /> : <AddIcon fontSize="large" />}
<Button onClick={handleTakePhoto} style={{ borderRadius: 0 }} className={classes.photo}>
<AddIcon fontSize="large" />
</Button>
</Box>
{photos.map((photo) => (
@@ -1,4 +1,4 @@
import React, { useMemo, useState, useCallback } from 'react';
import React, { useMemo, useState, useCallback, useEffect } from 'react';
import useStyles from './modal.styles';
import { Button, Paper } from '@material-ui/core';
import ArrowBackIcon from '@material-ui/icons/ArrowBack';
@@ -9,30 +9,47 @@ import { useQueryParams } from '../../../../common/hooks/useQueryParams';
import { ShareModal } from './ShareModal';
import { GalleryPhoto, PhotoEvents } from '../../../../../../typings/photo';
import { useNuiRequest } from 'fivem-nui-react-lib';
import { usePhotoActions } from '../../hooks/usePhotoActions';
import { fetchNui } from '../../../../utils/fetchNui';
import { ServerPromiseResp } from '../../../../../../typings/common';
import { useTranslation } from 'react-i18next';
import { useSnackbar } from '../../../../ui/hooks/useSnackbar';
export const GalleryModal = () => {
const Nui = useNuiRequest();
const [shareOpen, setShareOpen] = useState(null);
const classes = useStyles();
const history = useHistory();
const query = useQueryParams();
const referal = query.referal || '/camera';
const { deletePhoto } = usePhotoActions();
const { addAlert } = useSnackbar();
const { t } = useTranslation();
const [shareOpen, setShareOpen] = useState(null);
const referal = query.referal || '/camera';
const meta: GalleryPhoto = useMemo(
() => ({ id: parseInt(query.id), image: query.image as string }),
[query],
);
console.log('meta', meta);
const _handleClose = () => {
history.push(referal);
};
const handleDeletePhoto = () => {
Nui.send(PhotoEvents.DELETE_PHOTO, {
fetchNui<ServerPromiseResp<GalleryPhoto>>(PhotoEvents.DELETE_PHOTO, {
image: meta.image,
}).then((serverResp) => {
if (serverResp.status !== 'ok') {
return addAlert({ message: t('APPS_CAMERA_FAILED_TO_DELETE_PHOTO'), type: 'error' });
}
deletePhoto(meta.image);
history.push(referal);
});
history.push(referal);
};
const handleSharePhoto = useCallback(() => {
+27 -3
View File
@@ -1,9 +1,33 @@
import { atom } from 'recoil';
import { GalleryPhoto } from '../../../../../typings/photo';
import { atom, selector, useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import { ServerPromiseResp } from '../../../../../typings/common';
import { GalleryPhoto, PhotoEvents } from '../../../../../typings/photo';
import { fetchNui } from '../../../utils/fetchNui';
import LogDebugEvent from '../../../os/debug/LogDebugEvents';
import { isEnvBrowser } from '../../../utils/misc';
import { MockPhotoData } from '../utils/constants';
export const photoState = {
photos: atom<GalleryPhoto[]>({
key: 'galleryPhotos',
default: [],
default: selector({
key: 'defaultGalleryPhotos',
get: async () => {
try {
const resp = await fetchNui<ServerPromiseResp<GalleryPhoto[]>>(PhotoEvents.FETCH_PHOTOS);
LogDebugEvent({ action: 'PhotosFetched', data: resp.data });
return resp.data;
} catch (e) {
if (isEnvBrowser()) {
return MockPhotoData;
}
console.error(e);
return [];
}
},
}),
}),
};
export const useSetPhotos = () => useSetRecoilState(photoState.photos);
export const usePhotos = () => useRecoilState(photoState.photos);
export const usePhotosValue = () => useRecoilValue(photoState.photos);
+61 -45
View File
@@ -1,50 +1,66 @@
import { useTranslation } from 'react-i18next';
import { useSnackbar } from '../../../ui/hooks/useSnackbar';
import { photoState } from './state';
import { useCallback, useState } from 'react';
import { useNuiCallback } from 'fivem-nui-react-lib';
import { GalleryPhoto, PhotoEvents } from '../../../../../typings/photo';
import { usePhotoService } from './usePhotoService';
import { usePhotos } from './state';
import { useCallback } from 'react';
/*
export const usePhotoActions = () => {
const { t } = useTranslation();
const [photos, setPhotos] = usePhotos();
const takePhoto = useCallback(
(photo) => {
setPhotos((curPhotos) => [photo, ...curPhotos]);
},
[setPhotos],
);
const deletePhoto = useCallback(
(image: string) => {
setPhotos((curPhotos) => [...curPhotos].filter((meta) => meta.image !== image));
},
[setPhotos],
);
return {
takePhoto,
deletePhoto,
};
/*const { t } = useTranslation();
const { addAlert } = useSnackbar();
const [photos, setPhotos] = usePhotoService();
const [isUploading, setUploading] = useState(false);
const onPhotoSuccess = useCallback((photo) => {
setUploading(false);
if (!photo?.image) return;
setPhotos((curr) => [photo, ...curr]);
}, [setPhotos])
const onPhotoError = useCallback(
(i18nKey) => {
setUploading(false);
addAlert({ type: 'error', message: t(i18nKey) });
},
[addAlert, t],
);
const [_takePhoto] = useNuiCallback<void, GalleryPhoto>(
'CAMERA',
PhotoEvents.TAKE_PHOTO,
onPhotoSuccess,
onPhotoError,
);
const takePhoto = () => {
_takePhoto(undefined, { timeout: 60000 });
};
return {
photos,
setPhotos,
takePhoto,
isLoading: isUploading
}
}*/
const [isUploading, setUploading] = useState(false);*/
/*
const onPhotoSuccess = useCallback(
(photo) => {
setUploading(false);
if (!photo?.image) return;
setPhotos((curr) => [photo, ...curr]);
},
[setPhotos],
);
const onPhotoError = useCallback(
(i18nKey) => {
setUploading(false);
addAlert({ type: 'error', message: t(i18nKey) });
},
[addAlert, t],
);
const [_takePhoto] = useNuiCallback<void, GalleryPhoto>(
'CAMERA',
PhotoEvents.TAKE_PHOTO,
onPhotoSuccess,
onPhotoError,
);
const takePhoto = () => {
_takePhoto(undefined, { timeout: 60000 });
};
return {
photos,
setPhotos,
takePhoto,
isLoading: isUploading,
};*/
};
+10
View File
@@ -0,0 +1,10 @@
export const MockPhotoData = [
{
id: 1,
image: 'https://beta.iodine.gg/noDcb.jpeg',
},
{
id: 2,
image: 'https://beta.iodine.gg/teUcY.jpeg',
},
];
+3 -4
View File
@@ -60,15 +60,14 @@ InjectDebugData(
id: 1,
image:
'https://cdn.vox-cdn.com/thumbor/9xCALlkm2Mc0HQk8kMr1Ui3YNbc=/0x0:5464x3070/920x613/filters:focal(2295x1098:3169x1972):format(webp)/cdn.vox-cdn.com/uploads/chorus_image/image/67031748/shutterstock_1450403549.0.jpg',
name: 'Person Alpha',
bio:
'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.',
name: 'Evan You',
bio: 'Husband, father of two, independent open source developer. Creator / project lead of @vuejs, @vite_js and connoisseur of sushi.',
createdAt: 1615932985,
updatedAt: 1615932985,
lastActive: 1616004986,
job: 'Gardener',
location: 'Grapeseed',
tags: 'gardening',
tags: 'shit framework, vue sucks',
phoneNumber: '987-6543',
},
{
+1 -1
View File
@@ -7,7 +7,7 @@ interface IRouterLocation {
}
export const addQueryToLocation = (
{ pathname = '', search = '' }: Record<string, unknown> & IRouterLocation,
{ pathname = '', search = '' }: IRouterLocation,
key = '',
value = '',
) => {
+1
View File
@@ -64,6 +64,7 @@
"APPS_CAMERA": "Camera",
"APPS_CAMERA_FAILED_TO_TAKE_PHOTO": "Photo could not be saved :(",
"APPS_CAMERA_FAILED_TO_DELETE_PHOTO": "Photo could not be deleted :(",
"APPS_DIALER_INCOMING_CALL_TITLE": "Incoming Phone Call",
"APPS_DIALER_CURRENT_CALL_TITLE": "Current Phone Call",