-
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.
Showing
11 changed files
with
155 additions
and
23 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
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
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
76 changes: 76 additions & 0 deletions
76
libs/data-layer/operations/mutations/useDeleteMemberMutation.ts
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,76 @@ | ||
import { gql } from '@apollo/client'; | ||
import type { MutationOptions } from '@camp/api-client'; | ||
import { useMutation } from '@camp/api-client'; | ||
import type { | ||
ApiDeleteMemberMutation, | ||
ApiDeleteMemberMutationVariables, | ||
ApiMemberListQuery, | ||
ApiMemberListQueryVariables, | ||
} from '@camp/data-layer'; | ||
import type { Member, MemberKeys } from '@camp/domain'; | ||
import { isNull } from '@fullstacksjs/toolbox'; | ||
|
||
import { getMemberKeys, MemberKeysFragment } from '../fragments'; | ||
import { MemberListDocument } from '../queries'; | ||
|
||
const Document = gql` | ||
mutation DeleteMember($id: uuid!) { | ||
delete_member_by_pk(id: $id) { | ||
...MemberKeys | ||
name | ||
} | ||
} | ||
${MemberKeysFragment} | ||
`; | ||
|
||
export interface DeleteMember { | ||
member: (MemberKeys & Pick<Member, 'name'>) | undefined; | ||
} | ||
|
||
const toClient = (data: ApiDeleteMemberMutation | null): DeleteMember => { | ||
const deleted = data?.delete_member_by_pk; | ||
|
||
return { | ||
member: !isNull(deleted) | ||
? { ...getMemberKeys(deleted), name: deleted.name } | ||
: undefined, | ||
}; | ||
}; | ||
|
||
interface Variables { | ||
id: string; | ||
} | ||
|
||
const toApiVariables = ( | ||
variables: Variables, | ||
): ApiDeleteMemberMutationVariables => ({ | ||
id: variables.id, | ||
}); | ||
|
||
export const useDeleteMemberMutation = ( | ||
options?: MutationOptions<typeof toClient, typeof toApiVariables>, | ||
) => { | ||
return useMutation<typeof toClient, typeof toApiVariables>(Document, { | ||
...options, | ||
toClient, | ||
toApiVariables, | ||
update(cache, { data }) { | ||
const { id, household_id: householdId } = data?.delete_member_by_pk ?? {}; | ||
if (!id || !householdId) return; | ||
|
||
cache.updateQuery<ApiMemberListQuery, ApiMemberListQueryVariables>( | ||
{ | ||
query: MemberListDocument, | ||
variables: { household_id: householdId }, | ||
overwrite: true, | ||
}, | ||
value => { | ||
return { | ||
...value, | ||
member: value?.member.filter(h => h.id !== id) ?? [], | ||
}; | ||
}, | ||
); | ||
}, | ||
}); | ||
}; |
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