Skip to content

Commit 0f083b5

Browse files
Romain Jametrjamet-ovh
authored andcommitted
feat(okms): add <OkmsUpdateNameModal /> common component
ref: #MANAGER-19793 Signed-off-by: Romain Jamet <[email protected]>
1 parent 7d71395 commit 0f083b5

File tree

3 files changed

+91
-40
lines changed

3 files changed

+91
-40
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react';
2+
import { UpdateNameModal } from '@ovh-ux/manager-react-components';
3+
import { useTranslation } from 'react-i18next';
4+
import { useNavigate } from 'react-router-dom';
5+
import { NAMESPACES } from '@ovh-ux/manager-common-translations';
6+
import { useUpdateOkmsName } from '@/data/hooks/useUpdateOkmsName';
7+
import { OKMS } from '@/types/okms.type';
8+
9+
type OkmsUpdateNameModalProps = {
10+
okms: OKMS;
11+
};
12+
13+
const OkmsUpdateNameModal = ({ okms }: OkmsUpdateNameModalProps) => {
14+
const { t } = useTranslation([NAMESPACES.DASHBOARD, NAMESPACES.ACTIONS]);
15+
const navigate = useNavigate();
16+
17+
const { updateOkmsName, isPending, error: updateError } = useUpdateOkmsName({
18+
okms,
19+
onSuccess: () => {
20+
navigate('..');
21+
},
22+
});
23+
24+
const handleUpdateDisplayName = (newDisplayName: string) => {
25+
updateOkmsName({ displayName: newDisplayName });
26+
};
27+
28+
return (
29+
<UpdateNameModal
30+
isOpen
31+
headline={t('modify_name', { ns: NAMESPACES.ACTIONS })}
32+
inputLabel={t('display_name', { ns: NAMESPACES.DASHBOARD })}
33+
defaultValue={okms.iam.displayName}
34+
isLoading={isPending}
35+
error={updateError?.message}
36+
closeModal={() => {
37+
navigate('..');
38+
}}
39+
updateDisplayName={handleUpdateDisplayName}
40+
/>
41+
);
42+
};
43+
44+
export default OkmsUpdateNameModal;

packages/manager/apps/okms/src/data/api/okmsService.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { apiClient } from '@ovh-ux/manager-core-api';
2-
import { ServiceDetails } from '@ovh-ux/manager-react-components';
2+
import { ServiceDetails } from '@ovh-ux/manager-module-common-api';
33

44
export type UpdateOkmsNameParams = {
55
serviceId: number;
@@ -12,14 +12,15 @@ export const updateOkmsName = async ({
1212
serviceId,
1313
displayName,
1414
}: UpdateOkmsNameParams) =>
15-
apiClient.v6.put(`/services/${serviceId}`, {
15+
apiClient.v6.put<ServiceDetails>(`/services/${serviceId}`, {
1616
displayName,
1717
});
1818

1919
export const getOkmsServiceIdQueryKey = (okmsId: string) => [
2020
`get/okms/services`,
2121
okmsId,
2222
];
23+
2324
/**
2425
* allowedServices operations : List all services allowed in this kms
2526
*/

packages/manager/apps/okms/src/data/hooks/useUpdateOkmsName.ts

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
import { ApiError, ApiResponse } from '@ovh-ux/manager-core-api';
22
import { useMutation, useQueryClient } from '@tanstack/react-query';
33
import { useTranslation } from 'react-i18next';
4-
import {
5-
getServiceDetailsQueryKey,
6-
useNotifications,
7-
} from '@ovh-ux/manager-react-components';
4+
import { useNotifications } from '@ovh-ux/manager-react-components';
5+
import { NAMESPACES } from '@ovh-ux/manager-common-translations';
86
import { okmsQueryKeys } from '../api/okms';
97
import {
108
updateOkmsNameQueryKey,
119
getOkmsServiceIdQueryKey,
1210
getOkmsServiceId,
1311
updateOkmsName,
1412
} from '../api/okmsService';
13+
import { OKMS } from '@/types/okms.type';
1514

1615
export type UpdateOkmsParams = {
17-
okmsId: string;
16+
okms: OKMS;
1817
onSuccess: () => void;
1918
onError?: (result: ApiError) => void;
2019
};
2120
export type UpdateOkmsNameMutationParams = {
22-
/** Okms service id */
23-
okms: string;
2421
/** Okms service new display name */
2522
displayName: string;
2623
};
@@ -29,60 +26,69 @@ export type UpdateOkmsNameMutationParams = {
2926
* Get the function to mutate a okms Services
3027
*/
3128
export const useUpdateOkmsName = ({
32-
okmsId,
29+
okms,
3330
onSuccess,
3431
onError,
3532
}: UpdateOkmsParams) => {
3633
const queryClient = useQueryClient();
37-
const { t } = useTranslation('key-management-service/serviceKeys');
38-
const { addError, addSuccess, clearNotifications } = useNotifications();
34+
const { t } = useTranslation([NAMESPACES.ACTIONS, NAMESPACES.ERROR]);
35+
const { addSuccess, clearNotifications } = useNotifications();
3936

40-
const {
41-
mutate: updateKmsName,
42-
isPending,
43-
error: updateNameError,
44-
} = useMutation({
37+
const { mutate, isPending, error: updateNameError } = useMutation({
4538
mutationKey: updateOkmsNameQueryKey(),
46-
mutationFn: async ({ okms, displayName }: UpdateOkmsNameMutationParams) => {
39+
mutationFn: async ({ displayName }: UpdateOkmsNameMutationParams) => {
4740
const { data: servicesId } = await queryClient.fetchQuery<
4841
ApiResponse<number[]>
4942
>({
50-
queryKey: getOkmsServiceIdQueryKey(okms),
51-
queryFn: () => getOkmsServiceId(okms),
43+
queryKey: getOkmsServiceIdQueryKey(okms.id),
44+
queryFn: () => getOkmsServiceId(okms.id),
5245
});
5346
return updateOkmsName({ serviceId: servicesId[0], displayName });
5447
},
55-
onSuccess: async () => {
56-
await queryClient.invalidateQueries({
57-
queryKey: okmsQueryKeys.listDatagrid,
58-
});
59-
await queryClient.invalidateQueries({
60-
queryKey: getServiceDetailsQueryKey(okmsId),
48+
onSuccess: (_, { displayName }) => {
49+
const previousData = queryClient.getQueryData<{ data: OKMS }>(
50+
okmsQueryKeys.detail(okms.id),
51+
);
52+
53+
// To handle the delay in which the new name is propagated to the OKMS databases, we need to:
54+
// 1. Optimistically update the OKMS domain cache so that the user sees the new name on the OKMS dashboard immediately.
55+
queryClient.setQueryData(okmsQueryKeys.detail(okms.id), {
56+
data: {
57+
...previousData.data,
58+
iam: {
59+
...previousData.data.iam,
60+
displayName,
61+
},
62+
},
6163
});
62-
await queryClient.invalidateQueries({
63-
queryKey: okmsQueryKeys.detail(okmsId),
64+
65+
// 2. Invalidate the OKMS list query so that the list is refetched when the user returns to it.
66+
queryClient.invalidateQueries({
67+
queryKey: okmsQueryKeys.listDatagrid,
6468
});
69+
70+
// 3. Invalidate the OKMS list and detail queries after a delay to synchronize the front-end cache with the back-end.
71+
setTimeout(async () => {
72+
queryClient.invalidateQueries({
73+
queryKey: okmsQueryKeys.detail(okms.id),
74+
});
75+
// Invalidate the list query again to handle the case where the user returns to the list before the data is propagated in the back-end.
76+
queryClient.invalidateQueries({
77+
queryKey: okmsQueryKeys.listDatagrid,
78+
});
79+
}, 3000);
80+
6581
clearNotifications();
66-
addSuccess(
67-
t('key_management_service_service-keys_update_name_success'),
68-
true,
69-
);
82+
addSuccess(t('modify_name_success', { ns: NAMESPACES.ACTIONS }), true);
7083
onSuccess?.();
7184
},
7285
onError: (result: ApiError) => {
73-
clearNotifications();
74-
addError(
75-
t('key_management_service_service-keys_update_error', {
76-
error: result.message,
77-
}),
78-
true,
79-
);
8086
onError?.(result);
8187
},
8288
});
8389

8490
return {
85-
updateKmsName,
91+
updateOkmsName: mutate,
8692
isPending,
8793
error: updateNameError,
8894
};

0 commit comments

Comments
 (0)