add party share mutations and crew shared parties query
This commit is contained in:
parent
272c1dfa4e
commit
7ede296826
2 changed files with 93 additions and 1 deletions
|
|
@ -15,6 +15,7 @@ import {
|
||||||
} from '$lib/api/adapters/party.adapter'
|
} from '$lib/api/adapters/party.adapter'
|
||||||
import { partyKeys } from '$lib/api/queries/party.queries'
|
import { partyKeys } from '$lib/api/queries/party.queries'
|
||||||
import { userKeys } from '$lib/api/queries/user.queries'
|
import { userKeys } from '$lib/api/queries/user.queries'
|
||||||
|
import { crewKeys } from '$lib/api/queries/crew.queries'
|
||||||
import type { Party } from '$lib/types/api/party'
|
import type { Party } from '$lib/types/api/party'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -312,3 +313,69 @@ export function useRegeneratePreview() {
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Share party with crew mutation
|
||||||
|
*
|
||||||
|
* Shares a party with the current user's crew.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```svelte
|
||||||
|
* <script lang="ts">
|
||||||
|
* import { useSharePartyWithCrew } from '$lib/api/mutations/party.mutations'
|
||||||
|
*
|
||||||
|
* const shareParty = useSharePartyWithCrew()
|
||||||
|
*
|
||||||
|
* function handleShare(partyId: string, shortcode: string) {
|
||||||
|
* shareParty.mutate({ partyId, shortcode })
|
||||||
|
* }
|
||||||
|
* </script>
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export function useSharePartyWithCrew() {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
|
return createMutation(() => ({
|
||||||
|
mutationFn: ({ partyId }: { partyId: string; shortcode: string }) =>
|
||||||
|
partyAdapter.shareWithCrew(partyId),
|
||||||
|
onSuccess: (_share, { shortcode }) => {
|
||||||
|
// Invalidate the party to refresh its shares
|
||||||
|
queryClient.invalidateQueries({ queryKey: partyKeys.detail(shortcode) })
|
||||||
|
// Invalidate crew's shared parties list
|
||||||
|
queryClient.invalidateQueries({ queryKey: crewKeys.sharedParties() })
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove party share mutation
|
||||||
|
*
|
||||||
|
* Removes a share from a party.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```svelte
|
||||||
|
* <script lang="ts">
|
||||||
|
* import { useRemovePartyShare } from '$lib/api/mutations/party.mutations'
|
||||||
|
*
|
||||||
|
* const removeShare = useRemovePartyShare()
|
||||||
|
*
|
||||||
|
* function handleRemoveShare(partyId: string, shareId: string, shortcode: string) {
|
||||||
|
* removeShare.mutate({ partyId, shareId, shortcode })
|
||||||
|
* }
|
||||||
|
* </script>
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export function useRemovePartyShare() {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
|
return createMutation(() => ({
|
||||||
|
mutationFn: ({ partyId, shareId }: { partyId: string; shareId: string; shortcode: string }) =>
|
||||||
|
partyAdapter.removeShare(partyId, shareId),
|
||||||
|
onSuccess: (_data, { shortcode }) => {
|
||||||
|
// Invalidate the party to refresh its shares
|
||||||
|
queryClient.invalidateQueries({ queryKey: partyKeys.detail(shortcode) })
|
||||||
|
// Invalidate crew's shared parties list
|
||||||
|
queryClient.invalidateQueries({ queryKey: crewKeys.sharedParties() })
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
* @module api/queries/crew
|
* @module api/queries/crew
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { queryOptions } from '@tanstack/svelte-query'
|
import { queryOptions, infiniteQueryOptions } from '@tanstack/svelte-query'
|
||||||
import { crewAdapter } from '$lib/api/adapters/crew.adapter'
|
import { crewAdapter } from '$lib/api/adapters/crew.adapter'
|
||||||
import type { MemberFilter } from '$lib/types/api/crew'
|
import type { MemberFilter } from '$lib/types/api/crew'
|
||||||
|
|
||||||
|
|
@ -96,6 +96,30 @@ export const crewQueries = {
|
||||||
queryFn: () => crewAdapter.getPendingPhantomClaims(),
|
queryFn: () => crewAdapter.getPendingPhantomClaims(),
|
||||||
staleTime: 1000 * 60 * 2, // 2 minutes
|
staleTime: 1000 * 60 * 2, // 2 minutes
|
||||||
gcTime: 1000 * 60 * 15 // 15 minutes
|
gcTime: 1000 * 60 * 15 // 15 minutes
|
||||||
|
}),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parties shared with the crew query options
|
||||||
|
* Uses infinite query for pagination
|
||||||
|
*/
|
||||||
|
sharedParties: () =>
|
||||||
|
infiniteQueryOptions({
|
||||||
|
queryKey: ['crew', 'shared_parties'] as const,
|
||||||
|
queryFn: async ({ pageParam }) => {
|
||||||
|
const response = await crewAdapter.getSharedParties(pageParam)
|
||||||
|
return {
|
||||||
|
parties: response.parties,
|
||||||
|
page: response.meta.page,
|
||||||
|
totalPages: response.meta.totalPages,
|
||||||
|
total: response.meta.count,
|
||||||
|
perPage: response.meta.perPage
|
||||||
|
}
|
||||||
|
},
|
||||||
|
initialPageParam: 1,
|
||||||
|
getNextPageParam: (lastPage) =>
|
||||||
|
lastPage.page < lastPage.totalPages ? lastPage.page + 1 : undefined,
|
||||||
|
staleTime: 1000 * 60 * 2, // 2 minutes
|
||||||
|
gcTime: 1000 * 60 * 15 // 15 minutes
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -122,6 +146,7 @@ export const crewKeys = {
|
||||||
membersAll: () => [...crewKeys.all, 'members'] as const,
|
membersAll: () => [...crewKeys.all, 'members'] as const,
|
||||||
members: (filter: MemberFilter) => [...crewKeys.all, 'members', filter] as const,
|
members: (filter: MemberFilter) => [...crewKeys.all, 'members', filter] as const,
|
||||||
crewInvitations: (crewId: string) => [...crewKeys.all, crewId, 'invitations'] as const,
|
crewInvitations: (crewId: string) => [...crewKeys.all, crewId, 'invitations'] as const,
|
||||||
|
sharedParties: () => [...crewKeys.all, 'shared_parties'] as const,
|
||||||
invitations: {
|
invitations: {
|
||||||
all: ['invitations'] as const,
|
all: ['invitations'] as const,
|
||||||
pending: () => ['invitations', 'pending'] as const
|
pending: () => ['invitations', 'pending'] as const
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue