Skip to content

Commit b544395

Browse files
authored
Use swagger api for IDP configuration details (#3200)
1 parent b9f0ccf commit b544395

File tree

4 files changed

+63
-50
lines changed

4 files changed

+63
-50
lines changed

api/embedded_spec.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swagger.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,8 @@ paths:
33953395
put:
33963396
summary: Update IDP Configuration
33973397
operationId: UpdateConfiguration
3398+
consumes:
3399+
- application/json
33983400
parameters:
33993401
- name: body
34003402
in: body

web-app/src/api/consoleApi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5177,6 +5177,7 @@ export class Api<
51775177
method: "PUT",
51785178
body: body,
51795179
secure: true,
5180+
type: ContentType.Json,
51805181
format: "json",
51815182
...params,
51825183
}),

web-app/src/screens/Console/IDP/IDPConfigurationDetails.tsx

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,23 @@ import {
3737
} from "mds";
3838
import { useNavigate, useParams } from "react-router-dom";
3939
import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary";
40-
import { ErrorResponseHandler } from "../../../common/types";
4140
import { useAppDispatch } from "../../../store";
4241
import {
4342
setErrorSnackMessage,
4443
setHelpName,
4544
setServerNeedsRestart,
4645
} from "../../../systemSlice";
47-
import api from "../../../common/api";
48-
import useApi from "../Common/Hooks/useApi";
4946
import DeleteIDPConfigurationModal from "./DeleteIDPConfigurationModal";
5047
import PageHeaderWrapper from "../Common/PageHeaderWrapper/PageHeaderWrapper";
5148
import HelpMenu from "../HelpMenu";
49+
import { api } from "api";
50+
import {
51+
ApiError,
52+
HttpResponse,
53+
IdpServerConfiguration,
54+
SetIDPResponse,
55+
} from "api/consoleApi";
56+
import { errorToHandler } from "api/errors";
5257

5358
type IDPConfigurationDetailsProps = {
5459
formFields: object;
@@ -75,7 +80,9 @@ const IDPConfigurationDetails = ({
7580

7681
const configurationName = params.idpName;
7782

78-
const [loading, setLoading] = useState<boolean>(true);
83+
const [loadingDetails, setLoadingDetails] = useState<boolean>(true);
84+
const [loadingSave, setLoadingSave] = useState<boolean>(false);
85+
const [loadingEnabledSave, setLoadingEnabledSave] = useState<boolean>(false);
7986
const [isEnabled, setIsEnabled] = useState<boolean>(false);
8087
const [fields, setFields] = useState<any>({});
8188
const [overrideFields, setOverrideFields] = useState<any>({});
@@ -85,29 +92,6 @@ const IDPConfigurationDetails = ({
8592
const [deleteOpen, setDeleteOpen] = useState<boolean>(false);
8693
const [envOverride, setEnvOverride] = useState<boolean>(false);
8794

88-
const onSuccess = (res: any) => {
89-
dispatch(setServerNeedsRestart(res.restart === true));
90-
};
91-
92-
const onError = (err: ErrorResponseHandler) =>
93-
dispatch(setErrorSnackMessage(err));
94-
95-
const [loadingSave, invokeApi] = useApi(onSuccess, onError);
96-
97-
const onEnabledSuccess = (res: any) => {
98-
setIsEnabled(!isEnabled);
99-
dispatch(setServerNeedsRestart(res.restart === true));
100-
};
101-
102-
const onEnabledError = (err: ErrorResponseHandler) => {
103-
dispatch(setErrorSnackMessage(err));
104-
};
105-
106-
const [loadingEnabledSave, invokeEnabledApi] = useApi(
107-
onEnabledSuccess,
108-
onEnabledError,
109-
);
110-
11195
const parseFields = useCallback(
11296
(record: any) => {
11397
let fields: any = {};
@@ -158,32 +142,27 @@ const IDPConfigurationDetails = ({
158142
setOriginalFields(fields);
159143
};
160144

161-
useEffect(() => {
162-
setLoading(true);
163-
}, []);
164-
165145
useEffect(() => {
166146
const loadRecord = () => {
167-
api
168-
.invoke("GET", `${endpoint}${configurationName}`)
169-
.then((result: any) => {
170-
if (result) {
171-
setRecord(result);
172-
parseFields(result);
173-
parseOriginalFields(result);
147+
api.idp
148+
.getConfiguration(configurationName || "", "openid")
149+
.then((res: HttpResponse<IdpServerConfiguration, ApiError>) => {
150+
if (res.data) {
151+
setRecord(res.data);
152+
parseFields(res.data);
153+
parseOriginalFields(res.data);
174154
}
175-
setLoading(false);
176155
})
177-
.catch((err: ErrorResponseHandler) => {
178-
dispatch(setErrorSnackMessage(err));
179-
setLoading(false);
180-
});
156+
.catch((res: HttpResponse<IdpServerConfiguration, ApiError>) => {
157+
dispatch(setErrorSnackMessage(errorToHandler(res.error)));
158+
})
159+
.finally(() => setLoadingDetails(false));
181160
};
182161

183-
if (loading) {
162+
if (loadingDetails) {
184163
loadRecord();
185164
}
186-
}, [dispatch, loading, configurationName, endpoint, parseFields]);
165+
}, [dispatch, loadingDetails, configurationName, endpoint, parseFields]);
187166

188167
const validSave = () => {
189168
for (const [key, value] of Object.entries(formFields)) {
@@ -206,15 +185,27 @@ const IDPConfigurationDetails = ({
206185
};
207186

208187
const saveRecord = (event: React.FormEvent) => {
188+
setLoadingSave(true);
209189
event.preventDefault();
210190
let input = "";
211191
for (const key of Object.keys(formFields)) {
212192
if (fields[key] || fields[key] !== originalFields[key]) {
213193
input += `${key}=${fields[key]} `;
214194
}
215195
}
216-
invokeApi("PUT", `${endpoint}${configurationName}`, { input });
217-
setEditMode(false);
196+
197+
api.idp
198+
.updateConfiguration(configurationName || "", "openid", { input })
199+
.then((res: HttpResponse<SetIDPResponse, ApiError>) => {
200+
if (res.data) {
201+
dispatch(setServerNeedsRestart(res.data.restart === true));
202+
setEditMode(false);
203+
}
204+
})
205+
.catch(async (res: HttpResponse<SetIDPResponse, ApiError>) => {
206+
dispatch(setErrorSnackMessage(errorToHandler(res.error)));
207+
})
208+
.finally(() => setLoadingSave(false));
218209
};
219210

220211
const closeDeleteModalAndRefresh = async (refresh: boolean) => {
@@ -226,8 +217,21 @@ const IDPConfigurationDetails = ({
226217
};
227218

228219
const toggleConfiguration = (value: boolean) => {
220+
setLoadingEnabledSave(true);
229221
const input = `enable=${value ? "on" : "off"}`;
230-
invokeEnabledApi("PUT", `${endpoint}${configurationName}`, { input });
222+
223+
api.idp
224+
.updateConfiguration(configurationName || "", "openid", { input: input })
225+
.then((res: HttpResponse<SetIDPResponse, ApiError>) => {
226+
if (res.data) {
227+
setIsEnabled(!isEnabled);
228+
dispatch(setServerNeedsRestart(res.data.restart === true));
229+
}
230+
})
231+
.catch((res: HttpResponse<SetIDPResponse, ApiError>) => {
232+
dispatch(setErrorSnackMessage(errorToHandler(res.error)));
233+
})
234+
.finally(() => setLoadingEnabledSave(false));
231235
};
232236

233237
const renderFormField = (key: string, value: any) => {
@@ -331,7 +335,7 @@ const IDPConfigurationDetails = ({
331335
type="submit"
332336
variant="callAction"
333337
color="primary"
334-
disabled={loading || loadingSave || !validSave()}
338+
disabled={loadingDetails || loadingSave || !validSave()}
335339
label={"Save"}
336340
/>
337341
)}
@@ -498,7 +502,7 @@ const IDPConfigurationDetails = ({
498502
</Tooltip>
499503
<Button
500504
id={"refresh-idp-config"}
501-
onClick={() => setLoading(true)}
505+
onClick={() => setLoadingDetails(true)}
502506
label={"Refresh"}
503507
icon={<RefreshIcon />}
504508
/>

0 commit comments

Comments
 (0)