Skip to content

Commit

Permalink
102 Adds slice for RTK (WIP with console errors)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardovdheijden committed Mar 27, 2024
1 parent f67434e commit ccd10cc
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import React, { FC } from 'react';

import { EuiBadge } from '@elastic/eui';

import { useValueOverride } from '@/contexts';
import { ValueOverrideFunction, useValueOverride } from '@/contexts';
import { useWithOrchestratorTheme } from '@/hooks';
import { useAppSelector } from '@/rtk/hooks';
import { FieldValue } from '@/types';
import { camelToHuman } from '@/utils';

Expand All @@ -20,6 +21,34 @@ export const WfoProductBlockKeyValueRow: FC<
useWithOrchestratorTheme(getStyles);
const { valueOverride } = useValueOverride();

// START ------ Get data from store
const valueOverride2 = useAppSelector(
(state) =>
state.orchestratorComponentOverride?.subscriptionDetail
?.valueOverrides,
);

// Should be placed in a hook
const getRenderedValue = (
fieldValue: FieldValue,
): React.ReactNode | null => {
if (!valueOverride2) {
return null;
}

const renderFunctionForField: ValueOverrideFunction | undefined =
valueOverride2[fieldValue.field];

// This check is needed because TS does not infer the type correctly
if (renderFunctionForField) {
return renderFunctionForField(fieldValue);
}

return null;
};

/////// END

const { field, value } = fieldValue;

const WfoProductBlockValue: FC<{ value: FieldValue['value'] }> = ({
Expand All @@ -41,6 +70,12 @@ export const WfoProductBlockKeyValueRow: FC<
<WfoProductBlockValue value={value} />
)}
</td>
{/* Todo Temporary copy of previous col to compare solutions */}
<td css={rightColumnStyle}>
{getRenderedValue?.(fieldValue) ?? (
<WfoProductBlockValue value={value} />
)}
</td>
</tr>
);
};
3 changes: 2 additions & 1 deletion packages/orchestrator-ui-components/src/rtk/slices/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './customApis';
export * from './orchestratorComponentOverride';
export * from './toastMessages';
export * from './orchestratorConfig';
export * from './customApis';
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Slice, createSlice } from '@reduxjs/toolkit';

import { ValueOverrideConfiguration } from '@/contexts';

export type OrchestratorComponentOverride = {
subscriptionDetail?: {
valueOverrides?: ValueOverrideConfiguration;
};
};

type OrchestratorComponentOverrideSlice = Slice<OrchestratorComponentOverride>;

export const getOrchestratorComponentOverrideSlice = (
config: OrchestratorComponentOverride,
): OrchestratorComponentOverrideSlice => {
return createSlice({
name: 'orchestratorComponentOverride',
initialState: config,
reducers: {},
});
};
23 changes: 19 additions & 4 deletions packages/orchestrator-ui-components/src/rtk/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type { Dispatch, UnknownAction } from '@reduxjs/toolkit';
import { CombinedState } from '@reduxjs/toolkit/query';

import { CustomApiConfig, getCustomApiSlice } from '@/rtk/slices';
import {
OrchestratorComponentOverride,
getOrchestratorComponentOverrideSlice,
} from '@/rtk/slices/orchestratorComponentOverride';
import type { OrchestratorConfig } from '@/types';

import { orchestratorApi } from './api';
Expand All @@ -17,21 +21,32 @@ export type RootState = {
>;
toastMessages: ReturnType<typeof toastMessagesReducer>;
orchestratorConfig: OrchestratorConfig;
orchestratorComponentOverride?: OrchestratorComponentOverride;
customApis: CustomApiConfig[];
};

export const getOrchestratorStore = (
orchestratorConfig: OrchestratorConfig,
customApis: CustomApiConfig[],
): EnhancedStore<RootState> => {
export type InitialOrchestratorStoreConfig = Pick<
RootState,
'orchestratorConfig' | 'customApis' | 'orchestratorComponentOverride'
>;

export const getOrchestratorStore = ({
orchestratorConfig,
orchestratorComponentOverride = {},
customApis,
}: InitialOrchestratorStoreConfig): EnhancedStore<RootState> => {
const configSlice = getOrchestratorConfigSlice(orchestratorConfig);
const orchestratorComponentOverrideSlice =
getOrchestratorComponentOverrideSlice(orchestratorComponentOverride);
const customApisSlice = getCustomApiSlice(customApis);

const orchestratorStore = configureStore({
reducer: {
[orchestratorApi.reducerPath]: orchestratorApi.reducer,
toastMessages: toastMessagesReducer,
orchestratorConfig: configSlice.reducer,
orchestratorComponentOverride:
orchestratorComponentOverrideSlice.reducer,
customApis: customApisSlice?.reducer,
},
middleware: (getDefaultMiddleware) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@ import type { ReactNode } from 'react';
import { Provider } from 'react-redux';

import { CustomApiConfig } from '@/rtk/slices/customApis';
import { OrchestratorComponentOverride } from '@/rtk/slices/orchestratorComponentOverride';
import type { OrchestratorConfig } from '@/types';

import { getOrchestratorStore } from './store';

export type StoreProviderProps = {
initialOrchestratorConfig: OrchestratorConfig;
orchestratorComponentOverride?: OrchestratorComponentOverride;
customApis?: CustomApiConfig[];
children: ReactNode;
};

export const StoreProvider = ({
initialOrchestratorConfig,
orchestratorComponentOverride,
customApis = [],
children,
}: StoreProviderProps) => {
const store = getOrchestratorStore(initialOrchestratorConfig, customApis);
const store = getOrchestratorStore({
orchestratorConfig: initialOrchestratorConfig,
orchestratorComponentOverride,
customApis,
});
const [orchestratorStore] = useState(store);

return <Provider store={orchestratorStore}>{children}</Provider>;
Expand Down

0 comments on commit ccd10cc

Please sign in to comment.