Skip to content

Commit ea00995

Browse files
committed
feat: add form and mutation queries
1 parent 9c99625 commit ea00995

File tree

4 files changed

+329
-111
lines changed

4 files changed

+329
-111
lines changed

site/src/api/api.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,36 @@ class ApiMethods {
733733
return response.data;
734734
};
735735

736+
/**
737+
* @param data
738+
* @param organization Can be the organization's ID or name
739+
*/
740+
patchGroupIdpSyncSettings = async (
741+
data: TypesGen.GroupSyncSettings,
742+
organization: string,
743+
) => {
744+
const response = await this.axios.patch<TypesGen.Response>(
745+
`/api/v2/organizations/${organization}/settings/idpsync/groups"`,
746+
data,
747+
);
748+
return response.data;
749+
};
750+
751+
/**
752+
* @param data
753+
* @param organization Can be the organization's ID or name
754+
*/
755+
patchRoleIdpSyncSettings = async (
756+
data: TypesGen.RoleSyncSettings,
757+
organization: string,
758+
) => {
759+
const response = await this.axios.patch<TypesGen.Response>(
760+
`/api/v2/organizations/${organization}/settings/idpsync/roles"`,
761+
data,
762+
);
763+
return response.data;
764+
};
765+
736766
/**
737767
* @param organization Can be the organization's ID or name
738768
*/

site/src/api/queries/organizations.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { API } from "api/api";
22
import type {
33
AuthorizationResponse,
44
CreateOrganizationRequest,
5+
GroupSyncSettings,
6+
RoleSyncSettings,
57
UpdateOrganizationRequest,
68
} from "api/typesGenerated";
79
import type { QueryClient } from "react-query";
@@ -156,6 +158,18 @@ export const groupIdpSyncSettings = (organization: string) => {
156158
};
157159
};
158160

161+
export const patchGroupSyncSettings = (
162+
organization: string,
163+
queryClient: QueryClient,
164+
) => {
165+
return {
166+
mutationFn: (request: GroupSyncSettings) =>
167+
API.patchGroupIdpSyncSettings(request, organization),
168+
onSuccess: async () =>
169+
await queryClient.invalidateQueries(groupIdpSyncSettings(organization)),
170+
};
171+
};
172+
159173
export const getRoleIdpSyncSettingsKey = (organization: string) => [
160174
"organizations",
161175
organization,
@@ -169,6 +183,20 @@ export const roleIdpSyncSettings = (organization: string) => {
169183
};
170184
};
171185

186+
export const patchRoleSyncSettings = (
187+
organization: string,
188+
queryClient: QueryClient,
189+
) => {
190+
return {
191+
mutationFn: (request: RoleSyncSettings) =>
192+
API.patchRoleIdpSyncSettings(request, organization),
193+
onSuccess: async () =>
194+
await queryClient.invalidateQueries(
195+
getRoleIdpSyncSettingsKey(organization),
196+
),
197+
};
198+
};
199+
172200
/**
173201
* Fetch permissions for a single organization.
174202
*

site/src/pages/ManagementSettingsPage/IdpSyncPage/IdpSyncPage.tsx

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1+
import { getErrorMessage } from "api/errors";
12
import { groupsByOrganization } from "api/queries/groups";
23
import {
34
groupIdpSyncSettings,
5+
patchGroupSyncSettings,
6+
patchRoleSyncSettings,
47
roleIdpSyncSettings,
58
} from "api/queries/organizations";
69
import { organizationRoles } from "api/queries/roles";
710
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne";
811
import { EmptyState } from "components/EmptyState/EmptyState";
12+
import { displayError } from "components/GlobalSnackbar/utils";
13+
import { displaySuccess } from "components/GlobalSnackbar/utils";
914
import { Paywall } from "components/Paywall/Paywall";
1015
import { SquareArrowOutUpRight } from "lucide-react";
1116
import { useFeatureVisibility } from "modules/dashboard/useFeatureVisibility";
1217
import { useOrganizationSettings } from "modules/management/OrganizationSettingsLayout";
13-
import type { FC } from "react";
18+
import { type FC, useEffect } from "react";
1419
import { Helmet } from "react-helmet-async";
15-
import { useQueries } from "react-query";
20+
import { useMutation, useQueries, useQueryClient } from "react-query";
1621
import { useParams } from "react-router-dom";
1722
import { docs } from "utils/docs";
1823
import { pageTitle } from "utils/page";
1924
import IdpSyncPageView from "./IdpSyncPageView";
2025

2126
export const IdpSyncPage: FC = () => {
27+
const queryClient = useQueryClient();
2228
const { organization: organizationName } = useParams() as {
2329
organization: string;
2430
};
@@ -45,10 +51,41 @@ export const IdpSyncPage: FC = () => {
4551
return <EmptyState message="Organization not found" />;
4652
}
4753

54+
const patchGroupSyncSettingsMutation = useMutation(
55+
patchGroupSyncSettings(organizationName, queryClient),
56+
);
57+
const patchRoleSyncSettingsMutation = useMutation(
58+
patchRoleSyncSettings(organizationName, queryClient),
59+
);
60+
61+
useEffect(() => {
62+
if (patchGroupSyncSettingsMutation.error) {
63+
displayError(
64+
getErrorMessage(
65+
patchGroupSyncSettingsMutation.error,
66+
"Error updating IdP group sync settings.",
67+
),
68+
);
69+
}
70+
}, [patchGroupSyncSettingsMutation.error]);
71+
72+
useEffect(() => {
73+
if (patchRoleSyncSettingsMutation.error) {
74+
displayError(
75+
getErrorMessage(
76+
patchRoleSyncSettingsMutation.error,
77+
"Error updating IdP role sync settings.",
78+
),
79+
);
80+
}
81+
}, [patchRoleSyncSettingsMutation.error]);
82+
4883
const error =
4984
groupIdpSyncSettingsQuery.error ||
5085
roleIdpSyncSettingsQuery.error ||
51-
groupsQuery.error;
86+
groupsQuery.error ||
87+
patchGroupSyncSettingsMutation.error ||
88+
patchRoleSyncSettingsMutation.error;
5289

5390
const groupsMap = new Map<string, string>();
5491
if (groupsQuery.data) {
@@ -98,6 +135,32 @@ export const IdpSyncPage: FC = () => {
98135
roles={rolesQuery.data}
99136
organization={organization}
100137
error={error}
138+
onSubmitGroupSyncSettings={async (data) => {
139+
try {
140+
await patchGroupSyncSettingsMutation.mutateAsync(data);
141+
displaySuccess("IdP Group sync settings updated.");
142+
} catch (error) {
143+
displayError(
144+
getErrorMessage(
145+
error,
146+
"Failed to update IdP group sync settings",
147+
),
148+
);
149+
}
150+
}}
151+
onSubmitRoleSyncSettings={async (data) => {
152+
try {
153+
await patchRoleSyncSettingsMutation.mutateAsync(data);
154+
displaySuccess("IdP Role sync settings updated.");
155+
} catch (error) {
156+
displayError(
157+
getErrorMessage(
158+
error,
159+
"Failed to update IdP role sync settings",
160+
),
161+
);
162+
}
163+
}}
101164
/>
102165
</Cond>
103166
</ChooseOne>

0 commit comments

Comments
 (0)