diff --git a/src/js/features/config/config.store.ts b/src/js/features/config/config.store.ts index d94937c5..ee4341f6 100644 --- a/src/js/features/config/config.store.ts +++ b/src/js/features/config/config.store.ts @@ -6,6 +6,7 @@ import { katsuPublicRulesUrl } from '@/constants/configConstants'; import type { ServiceInfoStore, ServicesResponse } from '@/types/services'; import type { RootState } from '@/store'; import type { DiscoveryRules } from '@/types/configResponse'; +import { RequestStatus } from '@/types/requests'; import { printAPIError } from '@/utils/error.util'; import { scopedAuthorizedRequestConfig } from '@/utils/requests'; @@ -16,6 +17,12 @@ export const makeGetConfigRequest = createAsyncThunk res.data) .catch(printAPIError(rejectWithValue)); + }, + { + condition(_, { getState }) { + const state = getState(); + return state.metadata.selectedScope.scopeSet && state.config.configStatus === RequestStatus.Idle; + }, } ); @@ -23,26 +30,35 @@ export const makeGetServiceInfoRequest = createAsyncThunk< ServicesResponse[], void, { state: RootState; rejectValue: string } ->('config/getServiceInfo', (_, { rejectWithValue }) => - axios - .get(`${PUBLIC_URL}/api/service-registry/services`) - .then((res) => res.data) - .catch(printAPIError(rejectWithValue)) +>( + 'config/getServiceInfo', + (_, { rejectWithValue }) => + axios + .get(`${PUBLIC_URL}/api/service-registry/services`) + .then((res) => res.data) + .catch(printAPIError(rejectWithValue)), + { + condition(_, { getState }) { + return getState().config.serviceInfoStatus === RequestStatus.Idle; + }, + } ); export interface ConfigState { + configStatus: RequestStatus; maxQueryParameters: number; - isFetchingConfig: boolean; - isFetchingServiceInfo: boolean; - serviceInfo: ServiceInfoStore; maxQueryParametersRequired: boolean; + // ---------------------------------------------------- + serviceInfoStatus: RequestStatus; + serviceInfo: ServiceInfoStore; } const initialState: ConfigState = { - isFetchingConfig: false, + configStatus: RequestStatus.Idle, maxQueryParameters: 0, maxQueryParametersRequired: true, - isFetchingServiceInfo: false, + // ---------------------------------------------------- + serviceInfoStatus: RequestStatus.Idle, serviceInfo: { auth: '', }, @@ -58,24 +74,24 @@ const configStore = createSlice({ }, extraReducers: (builder) => { builder.addCase(makeGetConfigRequest.pending, (state) => { - state.isFetchingConfig = true; + state.configStatus = RequestStatus.Pending; }); builder.addCase(makeGetConfigRequest.fulfilled, (state, { payload }: PayloadAction) => { state.maxQueryParameters = payload.max_query_parameters; - state.isFetchingConfig = false; + state.configStatus = RequestStatus.Fulfilled; }); builder.addCase(makeGetConfigRequest.rejected, (state) => { - state.isFetchingConfig = false; + state.configStatus = RequestStatus.Rejected; }); builder.addCase(makeGetServiceInfoRequest.pending, (state) => { - state.isFetchingServiceInfo = true; + state.serviceInfoStatus = RequestStatus.Pending; }); builder.addCase(makeGetServiceInfoRequest.fulfilled, (state, { payload }: PayloadAction) => { state.serviceInfo.auth = payload.find((service) => service.bento.serviceKind === 'authorization')?.url || ''; - state.isFetchingServiceInfo = false; + state.serviceInfoStatus = RequestStatus.Fulfilled; }); builder.addCase(makeGetServiceInfoRequest.rejected, (state) => { - state.isFetchingServiceInfo = false; + state.serviceInfoStatus = RequestStatus.Rejected; }); }, });