11import { ApiError , ApiResponse } from '@ovh-ux/manager-core-api' ;
22import { useMutation , useQueryClient } from '@tanstack/react-query' ;
33import { 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' ;
86import { okmsQueryKeys } from '../api/okms' ;
97import {
108 updateOkmsNameQueryKey ,
119 getOkmsServiceIdQueryKey ,
1210 getOkmsServiceId ,
1311 updateOkmsName ,
1412} from '../api/okmsService' ;
13+ import { OKMS } from '@/types/okms.type' ;
1514
1615export type UpdateOkmsParams = {
17- okmsId : string ;
16+ okms : OKMS ;
1817 onSuccess : ( ) => void ;
1918 onError ?: ( result : ApiError ) => void ;
2019} ;
2120export 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 */
3128export 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