mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-05 06:28:51 +00:00
refactor(phone/marketplace): Refactor state management and fetching
This commit is contained in:
1 parent
2d1e192892
commit
4afa9c691b
12 files changed
+211
-87
No files matched your search
@@ -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';
|
||||
|
||||
+40
-7
@@ -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<MarketplaceListing> = ({ 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<MarketplaceActionResp>(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<MarketplaceActionResp>(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 (
|
||||
<Box flex justifyContent="space-between" alignItems="center">
|
||||
<Box justifyContent="space-between" alignItems="center">
|
||||
<div style={{ float: 'left' }}>
|
||||
{listing.number !== myNumber ? (
|
||||
{listing.number !== myNumber && (
|
||||
<>
|
||||
<Tooltip title={t('GENERIC_MESSAGE')}>
|
||||
<Button onClick={handleMessage}>
|
||||
@@ -61,7 +94,7 @@ export const ListingActions = ({ listing }: { listing: MarketplaceListing }) =>
|
||||
</Button>
|
||||
</Tooltip>
|
||||
</>
|
||||
) : null}
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div style={{ float: 'right' }}>
|
||||
+2
-2
@@ -41,7 +41,7 @@ const useStyles = makeStyles((theme) => ({
|
||||
},
|
||||
}));
|
||||
|
||||
export const MarketplaceItem = (listing: MarketplaceListing) => {
|
||||
export const MarketplaceItem: React.FC<MarketplaceListing> = ({ children, ...listing }) => {
|
||||
const classes = useStyles();
|
||||
|
||||
return (
|
||||
@@ -73,7 +73,7 @@ export const MarketplaceItem = (listing: MarketplaceListing) => {
|
||||
<Typography variant="body1" className={classes.listingContent}>
|
||||
{listing.description}
|
||||
</Typography>
|
||||
<ListingActions listing={listing} />
|
||||
<ListingActions {...listing} />
|
||||
</Paper>
|
||||
</div>
|
||||
</ListItem>
|
||||
+3
-3
@@ -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 (
|
||||
<List>
|
||||
@@ -0,0 +1,17 @@
|
||||
import React from 'react';
|
||||
import { MarketplaceList } from './MarketplaceList';
|
||||
import { Box, CircularProgress } from '@material-ui/core';
|
||||
|
||||
const LoadingListings: React.FC = () => (
|
||||
<Box display="flex" flexGrow={1} width="100%" justifyContent="center" alignItems="center">
|
||||
<CircularProgress />
|
||||
</Box>
|
||||
);
|
||||
|
||||
export const MarketplaceListContainer: React.FC = () => {
|
||||
return (
|
||||
<React.Suspense fallback={<LoadingListings />}>
|
||||
<MarketplaceList />
|
||||
</React.Suspense>
|
||||
);
|
||||
};
|
||||
@@ -1,6 +0,0 @@
|
||||
import React from 'react';
|
||||
import { MarketplaceList } from './MarketplaceList';
|
||||
|
||||
export const MarketplaceListContainer = () => {
|
||||
return <MarketplaceList />;
|
||||
};
|
||||
@@ -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<MarketplaceActionResp>(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'),
|
||||
|
||||
Reference in new issue
Block a user