Skip to content

Commit

Permalink
feat(phone/marketplace): broadcast deletion of listings when players …
Browse files Browse the repository at this point in the history
…drop
  • Loading branch information
itschip committed Dec 8, 2021
1 parent 1717b2e commit 43a749b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
8 changes: 5 additions & 3 deletions phone/src/apps/marketplace/hooks/useMarketplaceActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MarketplaceListing } from '@typings/marketplace';
import { useRecoilCallback } from 'recoil';

interface MarketplaceActionValues {
deleteListing: (id: number) => void;
deleteListing: (ids: number[]) => void;
addListing: (listing: MarketplaceListing) => void;
}

Expand All @@ -12,15 +12,17 @@ export const useMarketplaceActions = (): MarketplaceActionValues => {

const deleteListing = useRecoilCallback(
({ snapshot }) =>
(id: number) => {
(ids: number[]) => {
const { state, contents } = snapshot.getLoadable(listingState);
// Make sure our atom is actually loaded before we attempt a dispatch
if (state !== 'hasValue') return;
// Tts possible for the init fetch to fail and empty array is passed. In that case
// block dispatch
if (!contents.length) return;

setListings((curListings) => [...curListings].filter((listing) => listing.id !== id));
setListings((curListings) =>
[...curListings].filter((listing) => !ids.includes(listing.id)),
);
},
[],
);
Expand Down
16 changes: 4 additions & 12 deletions phone/src/apps/marketplace/hooks/useMarketplaceService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { useNuiEvent } from 'fivem-nui-react-lib';
import {
MarketplaceBroadcastAddDTO,
MarketplaceDeleteDTO,
MarketplaceEvents,
} from '@typings/marketplace';
import { MarketplaceBroadcastAddDTO, MarketplaceEvents } from '@typings/marketplace';
import { useMarketplaceActions } from './useMarketplaceActions';
import { useCallback } from 'react';

Expand All @@ -18,8 +14,8 @@ export const useMarketplaceService = () => {
);

const deleteListingHandler = useCallback(
(listing: MarketplaceDeleteDTO) => {
deleteListing(listing.id);
(listingIds: number[]) => {
deleteListing(listingIds);
},
[deleteListing],
);
Expand All @@ -30,9 +26,5 @@ export const useMarketplaceService = () => {
addListingHandler,
);

useNuiEvent<MarketplaceDeleteDTO>(
'MARKETPLACE',
MarketplaceEvents.BROADCAST_DELETE,
deleteListingHandler,
);
useNuiEvent<number[]>('MARKETPLACE', MarketplaceEvents.BROADCAST_DELETE, deleteListingHandler);
};
8 changes: 8 additions & 0 deletions resources/server/marketplace/marketplace.db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
MarketplaceDeleteDTO,
MarketplaceListing,
MarketplaceListingBase,
ReportListingDTO,
Expand Down Expand Up @@ -57,6 +58,13 @@ export class _MarketplaceDB {
return listings[0];
}

async getListingIdsByIdentifier(identifier: string): Promise<MarketplaceDeleteDTO[]> {
const query = `SELECT id FROM npwd_marketplace_listings WHERE identifier = ?`;
const [results] = await DbInterface._rawExec(query, [identifier]);

return <MarketplaceDeleteDTO[]>results;
}

async reportListing(listing: ReportListingDTO, profile: string): Promise<void> {
const query = `INSERT INTO npwd_marketplace_reports (listing_id, profile, title, description, url) VALUES (?, ?, ?, ?, ?)`;
await DbInterface._rawExec(query, [
Expand Down
10 changes: 6 additions & 4 deletions resources/server/marketplace/marketplace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ class _MarketplaceService {

resp({ status: 'ok' });

const returnObj: MarketplaceDeleteDTO = {
id: reqObj.data.id,
};
const returnObj = reqObj.data.id;

emitNet(MarketplaceEvents.BROADCAST_DELETE, -1, returnObj);
emitNet(MarketplaceEvents.BROADCAST_DELETE, -1, [returnObj]);
} catch (e) {
marketplaceLogger.error(`Error in handleDeleteListing, ${e.message}`);

Expand All @@ -105,6 +103,10 @@ class _MarketplaceService {

async handleDeleteListingsOnDrop(identifier: string) {
try {
const listingIds = await this.marketplaceDB.getListingIdsByIdentifier(identifier);

emitNet(MarketplaceEvents.BROADCAST_DELETE, -1, listingIds);

await this.marketplaceDB.deleteListingsOnDrop(identifier);
} catch (e) {
marketplaceLogger.error(`Error when deleting listings on player drop, ${e.message}`);
Expand Down

0 comments on commit 43a749b

Please sign in to comment.