Skip to content

Commit

Permalink
add hooks for member mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
magicznyleszek committed Dec 2, 2024
1 parent 5c8d255 commit 90161f9
Showing 1 changed file with 80 additions and 21 deletions.
101 changes: 80 additions & 21 deletions jsapp/js/account/organization/membersQuery.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import {keepPreviousData, useQuery} from '@tanstack/react-query';
// Libraries
import {
useQuery,
useQueryClient,
useMutation,
keepPreviousData,
} from '@tanstack/react-query';

// Stores, hooks and utilities
import {fetchGet, fetchPatch, fetchDelete} from 'js/api';
import {
useOrganizationQuery,
type OrganizationUserRole
} from './organizationQuery';

// Constants and types
import {endpoints} from 'js/api.endpoints';
import type {PaginatedResponse} from 'js/dataInterface';
import {fetchGet, fetchPatch, fetchDelete} from 'js/api';
import {QueryKeys} from 'js/query/queryKeys';
import {useOrganizationQuery, type OrganizationUserRole} from './organizationQuery';

export interface OrganizationMember {
/**
Expand Down Expand Up @@ -34,34 +47,79 @@ export interface OrganizationMember {
};
}

// -----------------------------------------------------------------------------
// Updating organization member
// -----------------------------------------------------------------------------

interface PatchOrgMemberVars {
orgId: string;
username: string;
newMemberData: Partial<OrganizationMember>;
}

/**
* For updating member within given organization. Accepts partial properties
* of `OrganizationMember`.
* Updates organization member.
* Accepts partial properties of `OrganizationMember`.
*/
export async function patchOrganizationMember(
organizationId: string,
username: string,
newMemberData: Partial<OrganizationMember>
) {
async function patchOrganizationMember(vars: PatchOrgMemberVars) {
const apiUrl = endpoints.ORGANIZATION_MEMBER_URL
.replace(':organization_id', organizationId)
.replace(':username', username);
return fetchPatch<OrganizationMember>(apiUrl, newMemberData);
.replace(':organization_id', vars.orgId)
.replace(':username', vars.username);
return fetchPatch<OrganizationMember>(apiUrl, vars.newMemberData);
}

/**
* For removing member from given organization.
* Mutation hook for updating organization member. It ensures that all related
* queries refetch data (are invalidated).
*/
export async function removeOrganizationMember(
organizationId: string,
username: string
) {
export function usePatchOrganizationMember() {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (vars: PatchOrgMemberVars) => patchOrganizationMember(vars),
onSettled: () => {
// We invalidate query, so it will refetch (instead of refetching it
// directly, see: https://github.com/TanStack/query/discussions/2468)
queryClient.invalidateQueries({queryKey: [QueryKeys.organizationMembers]});
},
});
}

// -----------------------------------------------------------------------------
// Removing organization member
// -----------------------------------------------------------------------------

interface RemoveOrgMemberVars {
orgId: string;
username: string;
}

async function removeOrganizationMember(vars: RemoveOrgMemberVars) {
const apiUrl = endpoints.ORGANIZATION_MEMBER_URL
.replace(':organization_id', organizationId)
.replace(':username', username);
.replace(':organization_id', vars.orgId)
.replace(':username', vars.username);
return fetchDelete(apiUrl);
}

/**
* Mutation hook for removing member from organiztion. It ensures that all
* related queries refetch data (are invalidated).
*/
export function useRemoveOrganizationMember() {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (vars: RemoveOrgMemberVars) => removeOrganizationMember(vars),
onSettled: () => {
queryClient.invalidateQueries({queryKey: [QueryKeys.organizationMembers]});
},
});
}

// -----------------------------------------------------------------------------
// Getting a list of organization members
// -----------------------------------------------------------------------------

/**
* Fetches paginated list of members for given organization.
* This is mainly needed for `useOrganizationMembersQuery`, so you most probably
Expand All @@ -77,7 +135,8 @@ async function getOrganizationMembers(
offset: offset.toString(),
});

const apiUrl = endpoints.ORGANIZATION_MEMBERS_URL.replace(':organization_id', orgId);
const apiUrl = endpoints.ORGANIZATION_MEMBERS_URL
.replace(':organization_id', orgId);

return fetchGet<PaginatedResponse<OrganizationMember>>(
apiUrl + '?' + params,
Expand Down

0 comments on commit 90161f9

Please sign in to comment.