Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(organizations): add functions for managing members TASK-985 #5281

Open
wants to merge 55 commits into
base: main
Choose a base branch
from

Conversation

magicznyleszek
Copy link
Member

@magicznyleszek magicznyleszek commented Nov 20, 2024

🗒️ Checklist

  1. run linter locally
  2. update all related docs (API, README, inline, etc.), if any
  3. draft PR with a title <type>(<scope>)<!>: <title> TASK-1234
  4. tag PR: at least frontend or backend unless it's global
  5. fill in the template below and delete template comments
  6. review thyself: read the diff and repro the preview as written
  7. open PR & confirm that CI passes
  8. request reviewers, if needed
  9. delete this section before merging

💭 Notes

Not testable per se until further PR is built atop this one and uses it :)

rajpatel24 and others added 30 commits November 7, 2024 01:42
…hook more customizable

and then use it in `MembersRoute`
…hook more customizable

and then use it in `MembersRoute`
@magicznyleszek magicznyleszek changed the base branch from main to leszek/task-980-members-table November 20, 2024 13:58
magicznyleszek and others added 4 commits November 20, 2024 23:03
…eszek/task-980-members-table

# Conflicts:
#	jsapp/js/account/organization/MembersRoute.tsx
#	jsapp/js/account/organization/membersQuery.ts
#	jsapp/js/account/organization/membersRoute.module.scss
#	jsapp/js/account/routes.constants.ts
#	jsapp/js/account/routes.tsx
#	jsapp/js/api.endpoints.ts
…ber-mutation-api

# Conflicts:
#	jsapp/js/account/organization/membersQuery.ts
@magicznyleszek magicznyleszek marked this pull request as ready for review November 21, 2024 14:37
@magicznyleszek magicznyleszek requested review from p2edwards and removed request for p2edwards November 21, 2024 14:47
Base automatically changed from leszek/task-980-members-table to main November 25, 2024 16:37
# Conflicts:
#	jsapp/js/account/organization/MembersRoute.tsx
#	jsapp/js/account/organization/membersQuery.ts
#	jsapp/js/api.endpoints.ts
#	kobo/apps/organizations/serializers.py
#	kobo/apps/organizations/views.py
.replace(':username', username);
return fetchDelete(apiUrl);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will probably need to add to these file the mutation hooks for the functions and do not export the actual functions above.

/**
 * Mutation hook to patch and organization member.
 * This receives the current members query result to refetch the list after the
 * mutation is successful instead of using query keys duo to pagination parameters.
 */
export const usePatchOrganizationMember = (
  membersQuery: UseQueryResult<PaginatedResponse<OrganizationMember>>
) => {
  const orgQuery = useOrganizationQuery();
  const orgId = orgQuery.data?.id;

  return useMutation({
    mutationFn: ({
      username,
      data,
    }: {
      username: string;
      data: Partial<OrganizationMember>;
    }) => {
      if (!orgId) {
        throw new Error('Organization not found');
      }
      return patchOrganizationMember(orgId, username, data);
    },
    onSuccess: () => {
      // This will refetch the members list
      membersQuery.refetch();
    },
  });
};

/**
 * Mutation hook to remove an organization member.
 * This receives the current members query result to refetch the list after the
 * mutation is successful instead of using query keys duo to pagination parameters.
 */
export const useRemoveOrganizationMember = (
  membersQuery: UseQueryResult<PaginatedResponse<OrganizationMember>>
) => {
  const orgQuery = useOrganizationQuery();
  const orgId = orgQuery.data?.id;

  return useMutation({
    mutationFn: (username: string) => {
      if (!orgId) {
        throw new Error('Organization not found');
      }
      return removeOrganizationMember(orgId, username);
    },
    onSuccess: () => {
      // This will refetch the members list
      membersQuery.refetch();
    },
  });
};

Usage would be something like:

const membersQuery = useOrganizationMembersQuery()
const membersPatch = usePatchOrganizationMember(membersQuery)

const onPatch = (username: string, data:Partial<OrganizationMember>) => {
    membersPatch.mutateAsync({username, data})
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants