-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow featuring/unfeaturing listing from list view
- Loading branch information
1 parent
abb2bb4
commit aa7a9eb
Showing
7 changed files
with
134 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import prisma from '@prisma-rw' | ||
|
||
export async function PATCH(_request, { params }) { | ||
try { | ||
const listingId = Number(params.id) | ||
|
||
const listing = await prisma.listing.update({ | ||
where: { | ||
id: listingId, | ||
}, | ||
data: { | ||
featured: true, | ||
}, | ||
}) | ||
|
||
return Response.json({ | ||
listing, | ||
}) | ||
} catch (e) { | ||
console.error(`[RW] Unable to feature listing - ${e}`) | ||
return new Response(`Unable to feature listing - ${e}`, { | ||
status: 500, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import prisma from '@prisma-rw' | ||
|
||
export async function PATCH(_request, { params }) { | ||
try { | ||
const listingId = Number(params.id) | ||
|
||
const listing = await prisma.listing.update({ | ||
where: { | ||
id: listingId, | ||
}, | ||
data: { | ||
featured: false, | ||
}, | ||
}) | ||
|
||
return Response.json({ | ||
listing, | ||
}) | ||
} catch (e) { | ||
console.error(`[RW] Unable to unfeature listing - ${e}`) | ||
return new Response(`Unable to unfeature listing - ${e}`, { | ||
status: 500, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { useAppContext } from '@store/hooks' | ||
|
||
const featureListingRequest = async (id) => { | ||
const response = await fetch(`api/listing/${id}/feature`, { | ||
method: 'PATCH', | ||
}) | ||
|
||
const data = await response.json() | ||
const { listing } = data | ||
return listing | ||
} | ||
|
||
const unfeatureListingRequest = async (id) => { | ||
const response = await fetch(`api/listing/${id}/unfeature`, { | ||
method: 'PATCH', | ||
}) | ||
|
||
const data = await response.json() | ||
const { listing } = data | ||
return listing | ||
} | ||
|
||
export default function useFeatureListing() { | ||
const queryClient = useQueryClient() | ||
const { selectedWebSlug: webSlug } = useAppContext() | ||
|
||
const { mutate: featureListing } = useMutation({ | ||
mutationFn: featureListingRequest, | ||
onSettled: () => { | ||
queryClient.invalidateQueries({ | ||
queryKey: ['listings', 'list', { webSlug }], | ||
}) | ||
}, | ||
}) | ||
|
||
const { mutate: unfeatureListing } = useMutation({ | ||
mutationFn: unfeatureListingRequest, | ||
onSettled: () => { | ||
queryClient.invalidateQueries({ | ||
queryKey: ['listings', 'list', { webSlug }], | ||
}) | ||
}, | ||
}) | ||
|
||
return { | ||
featureListing, | ||
unfeatureListing, | ||
} | ||
} |