From 4afa9c691bd56dd03c18cf7a8581fa4c1197c35e Mon Sep 17 00:00:00 2001 From: Taso Date: Fri, 4 Jun 2021 10:48:02 -0400 Subject: [PATCH] refactor(phone/marketplace): Refactor state management and fetching --- .../marketplace/components/MarketplaceApp.tsx | 2 +- .../ListingActions.tsx | 47 +++++++++++-- .../MarketplaceItem.tsx | 4 +- .../MarketplaceList.tsx | 6 +- .../MarketplaceListContainer.tsx | 17 +++++ .../SelloutList/MarketplaceListContainer.tsx | 6 -- .../components/form/ListingForm.tsx | 23 +++++-- phone/src/apps/marketplace/hooks/state.ts | 49 +++++++++++-- .../src/apps/marketplace/hooks/useListing.ts | 7 -- .../hooks/useMarketplaceService.ts | 44 +++++++----- resources/client/cl_marketplace.ts | 68 +++++++++++-------- typings/marketplace.ts | 25 ++++++- 12 files changed, 211 insertions(+), 87 deletions(-) rename phone/src/apps/marketplace/components/{SelloutList => MarketplaceList}/ListingActions.tsx (64%) rename phone/src/apps/marketplace/components/{SelloutList => MarketplaceList}/MarketplaceItem.tsx (94%) rename phone/src/apps/marketplace/components/{SelloutList => MarketplaceList}/MarketplaceList.tsx (66%) create mode 100644 phone/src/apps/marketplace/components/MarketplaceList/MarketplaceListContainer.tsx delete mode 100644 phone/src/apps/marketplace/components/SelloutList/MarketplaceListContainer.tsx delete mode 100644 phone/src/apps/marketplace/hooks/useListing.ts diff --git a/phone/src/apps/marketplace/components/MarketplaceApp.tsx b/phone/src/apps/marketplace/components/MarketplaceApp.tsx index cff692e..d2aca04 100644 --- a/phone/src/apps/marketplace/components/MarketplaceApp.tsx +++ b/phone/src/apps/marketplace/components/MarketplaceApp.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { AppWrapper } from '../../../ui/components'; import { AppContent } from '../../../ui/components/AppContent'; -import { MarketplaceListContainer } from './SelloutList/MarketplaceListContainer'; +import { MarketplaceListContainer } from './MarketplaceList/MarketplaceListContainer'; import { NavigationBar } from './navigation/NavigationBar'; import { Switch, Route } from 'react-router-dom'; import { ListingFormContainer } from './form/ListingFormContainer'; diff --git a/phone/src/apps/marketplace/components/SelloutList/ListingActions.tsx b/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx similarity index 64% rename from phone/src/apps/marketplace/components/SelloutList/ListingActions.tsx rename to phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx index ee3af55..baccfb4 100644 --- a/phone/src/apps/marketplace/components/SelloutList/ListingActions.tsx +++ b/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx @@ -1,7 +1,11 @@ import { Box, Tooltip, Button } from '@material-ui/core'; import { makeStyles, Theme } from '@material-ui/core/styles'; import React from 'react'; -import { MarketplaceEvents, MarketplaceListing } from '../../../../../../typings/marketplace'; +import { + MarketplaceActionResp, + MarketplaceEvents, + MarketplaceListing, +} from '../../../../../../typings/marketplace'; import { useSimcard } from '../../../../os/simcard/hooks/useSimcard'; import DeleteIcon from '@material-ui/icons/Delete'; import ReportIcon from '@material-ui/icons/Report'; @@ -11,6 +15,8 @@ import { useNuiRequest } from 'fivem-nui-react-lib'; import { useTranslation } from 'react-i18next'; import { useHistory } from 'react-router'; import { CallEvents } from '../../../../../../typings/call'; +import { fetchNui } from '../../../../utils/fetchNui'; +import { useSnackbar } from '../../../../ui/hooks/useSnackbar'; const useStyles = makeStyles((theme: Theme) => ({ icon: { @@ -18,21 +24,48 @@ const useStyles = makeStyles((theme: Theme) => ({ }, })); -export const ListingActions = ({ listing }: { listing: MarketplaceListing }) => { +export const ListingActions: React.FC = ({ children, ...listing }) => { const Nui = useNuiRequest(); const classes = useStyles(); const { number: myNumber } = useSimcard(); const { t } = useTranslation(); const history = useHistory(); + const { addAlert } = useSnackbar(); const handleDeleteListing = () => { - Nui.send(MarketplaceEvents.DELETE_LISTING, { + fetchNui(MarketplaceEvents.DELETE_LISTING, { id: listing.id, + }).then(({ err, errMsg }) => { + if (err) { + return addAlert({ + message: t(errMsg), + type: 'error', + }); + } + + addAlert({ + message: t('APPS_MARKETPLACE_DELETE_LISTING_SUCCESS'), + type: 'success', + }); }); }; const handleReportListing = () => { - Nui.send(MarketplaceEvents.REPORT_LISTING, listing); + fetchNui(MarketplaceEvents.DELETE_LISTING, listing).then( + ({ err, errMsg }) => { + if (err) { + return addAlert({ + message: t(errMsg), + type: 'error', + }); + } + + addAlert({ + message: t('APPS_MARKETPLACE_REPORT_LISTING_SUCCESS'), + type: 'success', + }); + }, + ); }; const handleCall = () => { @@ -46,9 +79,9 @@ export const ListingActions = ({ listing }: { listing: MarketplaceListing }) => }; return ( - +
- {listing.number !== myNumber ? ( + {listing.number !== myNumber && ( <> - ) : null} + )}
diff --git a/phone/src/apps/marketplace/components/SelloutList/MarketplaceItem.tsx b/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceItem.tsx similarity index 94% rename from phone/src/apps/marketplace/components/SelloutList/MarketplaceItem.tsx rename to phone/src/apps/marketplace/components/MarketplaceList/MarketplaceItem.tsx index 39ec38d..0951e26 100644 --- a/phone/src/apps/marketplace/components/SelloutList/MarketplaceItem.tsx +++ b/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceItem.tsx @@ -41,7 +41,7 @@ const useStyles = makeStyles((theme) => ({ }, })); -export const MarketplaceItem = (listing: MarketplaceListing) => { +export const MarketplaceItem: React.FC = ({ children, ...listing }) => { const classes = useStyles(); return ( @@ -73,7 +73,7 @@ export const MarketplaceItem = (listing: MarketplaceListing) => { {listing.description} - +
diff --git a/phone/src/apps/marketplace/components/SelloutList/MarketplaceList.tsx b/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceList.tsx similarity index 66% rename from phone/src/apps/marketplace/components/SelloutList/MarketplaceList.tsx rename to phone/src/apps/marketplace/components/MarketplaceList/MarketplaceList.tsx index 00e5ea2..5af07de 100644 --- a/phone/src/apps/marketplace/components/SelloutList/MarketplaceList.tsx +++ b/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceList.tsx @@ -1,10 +1,10 @@ import React from 'react'; import { List } from '../../../../ui/components/List'; -import { useListing } from '../../hooks/useListing'; +import { useListingValue } from '../../hooks/state'; import { MarketplaceItem } from './MarketplaceItem'; -export const MarketplaceList = () => { - const listings = useListing(); +export const MarketplaceList: React.FC = () => { + const listings = useListingValue(); return ( diff --git a/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceListContainer.tsx b/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceListContainer.tsx new file mode 100644 index 0000000..a64af09 --- /dev/null +++ b/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceListContainer.tsx @@ -0,0 +1,17 @@ +import React from 'react'; +import { MarketplaceList } from './MarketplaceList'; +import { Box, CircularProgress } from '@material-ui/core'; + +const LoadingListings: React.FC = () => ( + + + +); + +export const MarketplaceListContainer: React.FC = () => { + return ( + }> + + + ); +}; diff --git a/phone/src/apps/marketplace/components/SelloutList/MarketplaceListContainer.tsx b/phone/src/apps/marketplace/components/SelloutList/MarketplaceListContainer.tsx deleted file mode 100644 index 12e889d..0000000 --- a/phone/src/apps/marketplace/components/SelloutList/MarketplaceListContainer.tsx +++ /dev/null @@ -1,6 +0,0 @@ -import React from 'react'; -import { MarketplaceList } from './MarketplaceList'; - -export const MarketplaceListContainer = () => { - return ; -}; diff --git a/phone/src/apps/marketplace/components/form/ListingForm.tsx b/phone/src/apps/marketplace/components/form/ListingForm.tsx index c128b1b..5997802 100644 --- a/phone/src/apps/marketplace/components/form/ListingForm.tsx +++ b/phone/src/apps/marketplace/components/form/ListingForm.tsx @@ -1,7 +1,6 @@ import React, { useEffect, useState, useCallback } from 'react'; import { makeStyles, Button, Box } from '@material-ui/core'; -import { useNuiRequest } from 'fivem-nui-react-lib'; -import { MarketplaceEvents } from '../../../../../../typings/marketplace'; +import { MarketplaceActionResp, MarketplaceEvents } from '../../../../../../typings/marketplace'; import { useSnackbar } from '../../../../ui/hooks/useSnackbar'; import { useTranslation } from 'react-i18next'; import ImageIcon from '@material-ui/icons/Image'; @@ -10,6 +9,7 @@ import qs from 'qs'; import { useQueryParams } from '../../../../common/hooks/useQueryParams'; import { deleteQueryFromLocation } from '../../../../common/utils/deleteQueryFromLocation'; import { TextField } from '../../../../ui/components/Input'; +import { fetchNui } from '../../../../utils/fetchNui'; const useStyles = makeStyles((theme) => ({ root: { @@ -36,7 +36,6 @@ const useStyles = makeStyles((theme) => ({ })); export const ListingForm = () => { - const Nui = useNuiRequest(); const classes = useStyles(); const { t } = useTranslation(); const { addAlert } = useSnackbar(); @@ -50,12 +49,24 @@ export const ListingForm = () => { const addListing = () => { if (title !== '' && description !== '') { - Nui.send(MarketplaceEvents.ADD_LISTING, { + fetchNui(MarketplaceEvents.ADD_LISTING, { title, - url, description, + url, + }).then(({ err, errMsg }) => { + if (err) { + return addAlert({ + message: t(errMsg), + type: 'error', + }); + } + + addAlert({ + message: t('APPS_MARKETPLACE_CREATE_LISTING_SUCCESS'), + type: 'success', + }); + history.push('/marketplace'); }); - history.push('/marketplace'); } else { addAlert({ message: t('APPS_MARKETPLACE_REQUIRED_FIELDS'), diff --git a/phone/src/apps/marketplace/hooks/state.ts b/phone/src/apps/marketplace/hooks/state.ts index 77481ed..2e336ab 100644 --- a/phone/src/apps/marketplace/hooks/state.ts +++ b/phone/src/apps/marketplace/hooks/state.ts @@ -1,9 +1,44 @@ -import { atom } from 'recoil'; -import { MarketplaceListing } from '../../../../../typings/marketplace'; +import { atom, selector, useRecoilValue, useSetRecoilState } from 'recoil'; +import { MarketplaceEvents, MarketplaceListing } from '../../../../../typings/marketplace'; +import { fetchNui } from '../../../utils/fetchNui'; -export const selloutState = { - listing: atom({ - key: 'listing', - default: [], +const defaultData: MarketplaceListing[] = [ + { + id: 1, + name: 'Some guy', + number: '111-1134', + username: 'Taso', + title: 'eeeeeeeeeeeeeeeeeeeeeeeee', + description: + 'skldfsdEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE', + url: 'https://beta.iodine.gg/706Y3.jpeg', + }, + { + id: 2, + name: 'Some other dude', + number: '666-6666', + username: 'Taso', + title: 'Material', + description: 'Selling my wife', + url: '', + }, +]; + +const listingState = atom({ + key: 'listings', + default: selector({ + key: 'defaultListings', + get: async () => { + try { + return await fetchNui(MarketplaceEvents.FETCH_LISTING); + } catch (e) { + if (process.env.NODE_ENV === 'development' && !process.env.REACT_APP_IN_GAME) { + return defaultData; + } + } + }, }), -}; +}); + +export const useListingValue = () => useRecoilValue(listingState); +export const useSetListings = () => useSetRecoilState(listingState); diff --git a/phone/src/apps/marketplace/hooks/useListing.ts b/phone/src/apps/marketplace/hooks/useListing.ts deleted file mode 100644 index 0e0f1d2..0000000 --- a/phone/src/apps/marketplace/hooks/useListing.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { useRecoilValue } from 'recoil'; -import { MarketplaceListing } from '../../../../../typings/marketplace'; -import { selloutState } from './state'; - -export const useListing = () => { - return useRecoilValue(selloutState.listing); -}; diff --git a/phone/src/apps/marketplace/hooks/useMarketplaceService.ts b/phone/src/apps/marketplace/hooks/useMarketplaceService.ts index b644f25..ba59fa0 100644 --- a/phone/src/apps/marketplace/hooks/useMarketplaceService.ts +++ b/phone/src/apps/marketplace/hooks/useMarketplaceService.ts @@ -1,24 +1,32 @@ import { useNuiEvent } from 'fivem-nui-react-lib'; -import { selloutState } from './state'; -import { useListing } from './useListing'; -import { useSetRecoilState } from 'recoil'; -import { IAlert, useSnackbar } from '../../../ui/hooks/useSnackbar'; -import { useTranslation } from 'react-i18next'; -import { MarketplaceEvents } from '../../../../../typings/marketplace'; +import { + MarketPlaceBroadcastData, + MarketplaceDeleteDTO, + MarketplaceEvents, + MarketplaceListing, +} from '../../../../../typings/marketplace'; +import { useSetListings } from './state'; export const useMarketplaceService = () => { - const setSellout = useSetRecoilState(selloutState.listing); - const { addAlert } = useSnackbar(); - const { t } = useTranslation(); + const setListings = useSetListings(); - const handleAddAlert = ({ message, type }: IAlert) => { - addAlert({ - message: t(`APPS_${message}`), - type, - }); - }; + useNuiEvent( + 'SELLOUT', + MarketplaceEvents.BROADCAST_EVENT, + ({ type, listing }) => { + if (type === 'ADD') { + const typeCastListing = listing as MarketplaceListing; + return setListings((curState) => [...curState, typeCastListing]); + } - useNuiEvent('SELLOUT', MarketplaceEvents.SEND_ALERT, handleAddAlert); - useNuiEvent('SELLOUT', MarketplaceEvents.SEND_LISTING, setSellout); - return useListing(); + if (type === 'DELETE') { + const deleteDTO = listing as MarketplaceDeleteDTO; + return setListings((currVal) => { + const arrayPos = currVal.map((item) => item.id).indexOf(deleteDTO.id); + + return [...currVal].splice(arrayPos); + }); + } + }, + ); }; diff --git a/resources/client/cl_marketplace.ts b/resources/client/cl_marketplace.ts index 964485d..72fe64a 100644 --- a/resources/client/cl_marketplace.ts +++ b/resources/client/cl_marketplace.ts @@ -1,37 +1,47 @@ -import { MarketplaceEvents, MarketplaceListing } from '../../typings/marketplace'; -import { IAlertProps } from '../../typings/alerts'; +import { MarketplaceEvents, MarketplaceListingBroadcast } from '../../typings/marketplace'; +import { RegisterNuiProxy } from './cl_utils'; import { sendMarketplaceEvent } from '../utils/messages'; -onNet(MarketplaceEvents.SEND_LISTING, (listings: MarketplaceListing[]) => { - sendMarketplaceEvent(MarketplaceEvents.SEND_LISTING, listings); +// onNet(MarketplaceEvents.SEND_LISTING, (listings: MarketplaceListing[]) => { +// sendMarketplaceEvent(MarketplaceEvents.SEND_LISTING, listings); +// }); + +RegisterNuiProxy(MarketplaceEvents.FETCH_LISTING); +RegisterNuiProxy(MarketplaceEvents.ADD_LISTING); + +// RegisterNuiCallbackType(MarketplaceEvents.ADD_LISTING); +// on(`__cfx_nui:${MarketplaceEvents.ADD_LISTING}`, (data: MarketplaceListing, cb: Function) => { +// emitNet(MarketplaceEvents.ADD_LISTING, data); +// cb(); +// }); + +RegisterNuiProxy(MarketplaceEvents.DELETE_LISTING); +RegisterNuiProxy(MarketplaceEvents.REPORT_LISTING); + +onNet(MarketplaceEvents.BROADCAST_EVENT, (broadcastEvent: MarketplaceListingBroadcast) => { + sendMarketplaceEvent(MarketplaceEvents.BROADCAST_EVENT, broadcastEvent); }); -RegisterNuiCallbackType(MarketplaceEvents.ADD_LISTING); -on(`__cfx_nui:${MarketplaceEvents.ADD_LISTING}`, (data: MarketplaceListing, cb: Function) => { - emitNet(MarketplaceEvents.ADD_LISTING, data); - cb(); -}); +// RegisterNuiCallbackType(MarketplaceEvents.DELETE_LISTING); +// on(`__cfx_nui:${MarketplaceEvents.DELETE_LISTING}`, (data: MarketplaceListing, cb: Function) => { +// emitNet(MarketplaceEvents.DELETE_LISTING, data.id); +// cb(); +// }); -RegisterNuiCallbackType(MarketplaceEvents.DELETE_LISTING); -on(`__cfx_nui:${MarketplaceEvents.DELETE_LISTING}`, (data: MarketplaceListing, cb: Function) => { - emitNet(MarketplaceEvents.DELETE_LISTING, data.id); - cb(); -}); +// RegisterNuiCallbackType(MarketplaceEvents.REPORT_LISTING); +// on(`__cfx_nui:${MarketplaceEvents.REPORT_LISTING}`, (data: MarketplaceListing, cb: Function) => { +// emitNet(MarketplaceEvents.REPORT_LISTING, data); +// cb(); +// }); -RegisterNuiCallbackType(MarketplaceEvents.REPORT_LISTING); -on(`__cfx_nui:${MarketplaceEvents.REPORT_LISTING}`, (data: MarketplaceListing, cb: Function) => { - emitNet(MarketplaceEvents.REPORT_LISTING, data); - cb(); -}); +// onNet(MarketplaceEvents.ADD_LISTING_SUCCESS, () => { +// emitNet(MarketplaceEvents.FETCH_LISTING); +// }); -onNet(MarketplaceEvents.ADD_LISTING_SUCCESS, () => { - emitNet(MarketplaceEvents.FETCH_LISTING); -}); +// onNet(MarketplaceEvents.DELETE_LISTING_SUCCESS, () => { +// emitNet(MarketplaceEvents.FETCH_LISTING); +// }); -onNet(MarketplaceEvents.DELETE_LISTING_SUCCESS, () => { - emitNet(MarketplaceEvents.FETCH_LISTING); -}); - -onNet(MarketplaceEvents.ACTION_RESULT, (alert: IAlertProps) => { - sendMarketplaceEvent(MarketplaceEvents.SEND_ALERT, alert); -}); +// onNet(MarketplaceEvents.ACTION_RESULT, (alert: IAlertProps) => { +// sendMarketplaceEvent(MarketplaceEvents.SEND_ALERT, alert); +// }); diff --git a/typings/marketplace.ts b/typings/marketplace.ts index 94099a4..ea6805a 100644 --- a/typings/marketplace.ts +++ b/typings/marketplace.ts @@ -1,14 +1,22 @@ -export interface MarketplaceListing { +export interface MarketplaceListing extends MarketplaceListingBase { id: number; identifier?: string; username: string; name: string; number: string; +} + +export interface MarketplaceListingBase { title: string; url: string; description: string; } +export interface MarketplaceActionResp { + err: boolean; + errMsg?: string; +} + export enum MarketplaceEvents { ADD_LISTING = 'npwd:addListing', ADD_LISTING_SUCCESS = 'npwd:addListingSuccess', @@ -21,4 +29,19 @@ export enum MarketplaceEvents { REPORT_LISTING_SUCCESS = 'npwd:reportListingSuccess', REPORT_LISTING_SUCESS = 'npwd:reportListingFailed', SEND_ALERT = 'nwpd:sendListingAlert', + BROADCAST_EVENT = 'npwd:sendMarketplaceBroadcast', } + +export interface MarketplaceDeleteDTO { + id: number; +} + +export type MarketPlaceBroadcastData = + | { + type: 'ADD'; + listing: MarketplaceListing; + } + | { + type: 'DELETE'; + listing: MarketplaceDeleteDTO; + };