Skip to content

Commit

Permalink
refact: rewrite config feature with RequestStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Nov 13, 2024
1 parent dfcd1ab commit fbfaeae
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/js/features/config/config.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -16,33 +17,48 @@ export const makeGetConfigRequest = createAsyncThunk<DiscoveryRules, void, { rej
.get(katsuPublicRulesUrl, scopedAuthorizedRequestConfig(getState()))
.then((res) => res.data)
.catch(printAPIError(rejectWithValue));
},
{
condition(_, { getState }) {
const state = getState();
return state.metadata.selectedScope.scopeSet && state.config.configStatus === RequestStatus.Idle;
},
}
);

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: '',
},
Expand All @@ -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<DiscoveryRules>) => {
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<ServicesResponse[]>) => {
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;
});
},
});
Expand Down

0 comments on commit fbfaeae

Please sign in to comment.