Skip to content

Commit 560fd4f

Browse files
committed
query refetching is cool
1 parent 40faf01 commit 560fd4f

File tree

6 files changed

+38
-33
lines changed

6 files changed

+38
-33
lines changed

site/src/api/api.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,19 +515,19 @@ class ApiMethods {
515515
};
516516

517517
updateOrganization = async (
518-
orgId: string,
518+
organizationId: string,
519519
params: TypesGen.UpdateOrganizationRequest,
520520
) => {
521521
const response = await this.axios.patch<TypesGen.Organization>(
522-
`/api/v2/organizations/${orgId}`,
522+
`/api/v2/organizations/${organizationId}`,
523523
params,
524524
);
525525
return response.data;
526526
};
527527

528-
deleteOrganization = async (orgId: string) => {
528+
deleteOrganization = async (organizationId: string) => {
529529
await this.axios.delete<TypesGen.Organization>(
530-
`/api/v2/organizations/${orgId}`,
530+
`/api/v2/organizations/${organizationId}`,
531531
);
532532
};
533533

@@ -1486,11 +1486,11 @@ class ApiMethods {
14861486
};
14871487

14881488
getGroup = async (
1489-
orgId: string,
1489+
organizationId: string,
14901490
groupName: string,
14911491
): Promise<TypesGen.Group> => {
14921492
const response = await this.axios.get(
1493-
`/api/v2/organizations/${orgId}/groups/${groupName}`,
1493+
`/api/v2/organizations/${organizationId}/groups/${groupName}`,
14941494
);
14951495
return response.data;
14961496
};

site/src/api/queries/groups.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ export const removeMember = (queryClient: QueryClient) => {
142142

143143
export const invalidateGroup = (
144144
queryClient: QueryClient,
145-
orgId: string,
145+
organizationId: string,
146146
groupId: string,
147147
) =>
148148
Promise.all([
149149
queryClient.invalidateQueries(GROUPS_QUERY_KEY),
150-
queryClient.invalidateQueries(getGroupQueryKey(orgId, groupId)),
150+
queryClient.invalidateQueries(getGroupQueryKey(organizationId, groupId)),
151151
]);
152152

153153
export function sortGroupsByName(

site/src/api/queries/organizations.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export const createOrganization = (queryClient: QueryClient) => {
1919
};
2020

2121
interface UpdateOrganizationVariables {
22-
orgId: string;
22+
organizationId: string;
2323
req: UpdateOrganizationRequest;
2424
}
2525

2626
export const updateOrganization = (queryClient: QueryClient) => {
2727
return {
2828
mutationFn: (variables: UpdateOrganizationVariables) =>
29-
API.updateOrganization(variables.orgId, variables.req),
29+
API.updateOrganization(variables.organizationId, variables.req),
3030

3131
onSuccess: async () => {
3232
await queryClient.invalidateQueries(organizationsKey);
@@ -36,7 +36,8 @@ export const updateOrganization = (queryClient: QueryClient) => {
3636

3737
export const deleteOrganization = (queryClient: QueryClient) => {
3838
return {
39-
mutationFn: (orgId: string) => API.deleteOrganization(orgId),
39+
mutationFn: (organizationId: string) =>
40+
API.deleteOrganization(organizationId),
4041

4142
onSuccess: async () => {
4243
await queryClient.invalidateQueries(meKey);
@@ -79,7 +80,7 @@ export const removeOrganizationMember = (
7980
};
8081
};
8182

82-
export const organizationsKey = ["organizations", "me"] as const;
83+
export const organizationsKey = ["organizations"] as const;
8384

8485
export const organizations = () => {
8586
return {

site/src/pages/ManagementSettingsPage/GroupsPage/GroupPage.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const GroupPage: FC = () => {
6868
: { enabled: false },
6969
);
7070
const addMemberMutation = useMutation(addMember(queryClient));
71+
const removeMemberMutation = useMutation(removeMember(queryClient));
7172
const deleteGroupMutation = useMutation(deleteGroup(queryClient));
7273
const [isDeletingGroup, setIsDeletingGroup] = useState(false);
7374
const isLoading = groupQuery.isLoading || !groupData || !permissions;
@@ -193,6 +194,20 @@ export const GroupPage: FC = () => {
193194
group={groupData}
194195
key={member.id}
195196
canUpdate={canUpdateGroup}
197+
onRemove={async () => {
198+
try {
199+
await removeMemberMutation.mutateAsync({
200+
groupId: groupData.id,
201+
userId: member.id,
202+
});
203+
await groupQuery.refetch();
204+
displaySuccess("Member removed successfully.");
205+
} catch (error) {
206+
displayError(
207+
getErrorMessage(error, "Failed to remove member."),
208+
);
209+
}
210+
}}
196211
/>
197212
))
198213
)}
@@ -275,16 +290,15 @@ interface GroupMemberRowProps {
275290
member: ReducedUser;
276291
group: Group;
277292
canUpdate: boolean;
293+
onRemove: () => void;
278294
}
279295

280296
const GroupMemberRow: FC<GroupMemberRowProps> = ({
281297
member,
282298
group,
283299
canUpdate,
300+
onRemove,
284301
}) => {
285-
const queryClient = useQueryClient();
286-
const removeMemberMutation = useMutation(removeMember(queryClient));
287-
288302
return (
289303
<TableRow key={member.id}>
290304
<TableCell width="59%">
@@ -315,19 +329,7 @@ const GroupMemberRow: FC<GroupMemberRowProps> = ({
315329
<MoreMenuContent>
316330
<MoreMenuItem
317331
danger
318-
onClick={async () => {
319-
try {
320-
await removeMemberMutation.mutateAsync({
321-
groupId: group.id,
322-
userId: member.id,
323-
});
324-
displaySuccess("Member removed successfully.");
325-
} catch (error) {
326-
displayError(
327-
getErrorMessage(error, "Failed to remove member."),
328-
);
329-
}
330-
}}
332+
onClick={onRemove}
331333
disabled={group.id === group.organization_id}
332334
>
333335
Remove

site/src/pages/ManagementSettingsPage/ManagementSettingsLayout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const ManagementSettingsLayout: FC = () => {
6161
currentOrganizationId: !inOrganizationSettings
6262
? undefined
6363
: !organization
64-
? organizationsQuery.data[0]?.id
64+
? "00000000-0000-0000-0000-000000000000"
6565
: organizationsQuery.data.find(
6666
(org) => org.name === organization,
6767
)?.id,

site/src/pages/ManagementSettingsPage/OrganizationSettingsPage.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ const OrganizationSettingsPage: FC = () => {
3737
organization={org}
3838
error={error}
3939
onSubmit={async (values) => {
40-
await updateOrganizationMutation.mutateAsync({
41-
orgId: org.id,
42-
req: values,
43-
});
40+
const updatedOrganization =
41+
await updateOrganizationMutation.mutateAsync({
42+
organizationId: org.id,
43+
req: values,
44+
});
45+
navigate(`/organizations/${updatedOrganization.name}`);
4446
displaySuccess("Organization settings updated.");
4547
}}
4648
onDeleteOrganization={() => {

0 commit comments

Comments
 (0)