Skip to content

Commit

Permalink
webui: split reducers acording to the data type
Browse files Browse the repository at this point in the history
  • Loading branch information
KKoukiou committed Jun 21, 2023
1 parent 6b7ea2d commit 7660594
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
8 changes: 4 additions & 4 deletions ui/webui/src/components/AnacondaWizard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {

const _ = cockpit.gettext;

export const AnacondaWizard = ({ dispatch, deviceData, diskSelection, languages, commonLocales, onAddErrorNotification, toggleContextHelp, hideContextHelp, title, conf }) => {
export const AnacondaWizard = ({ dispatch, storageData, localizationData, onAddErrorNotification, toggleContextHelp, hideContextHelp, title, conf }) => {
const [isFormValid, setIsFormValid] = useState(true);
const [stepNotification, setStepNotification] = useState();
const [isInProgress, setIsInProgress] = useState(false);
Expand All @@ -57,7 +57,7 @@ export const AnacondaWizard = ({ dispatch, deviceData, diskSelection, languages,
const stepsOrder = [
{
component: InstallationLanguage,
data: { dispatch, languages, commonLocales },
data: { dispatch, languages: localizationData.languages, commonLocales: localizationData.commonLocales },
id: "installation-language",
label: _("Welcome"),
},
Expand All @@ -67,7 +67,7 @@ export const AnacondaWizard = ({ dispatch, deviceData, diskSelection, languages,
label: _("Installation destination"),
steps: [{
component: InstallationDestination,
data: { deviceData, diskSelection, dispatch },
data: { deviceData: storageData.devices, diskSelection: storageData.diskSelection, dispatch },
id: "storage-devices",
label: _("Storage devices")
}, {
Expand All @@ -82,7 +82,7 @@ export const AnacondaWizard = ({ dispatch, deviceData, diskSelection, languages,
},
{
component: ReviewConfiguration,
data: { deviceData },
data: { deviceData: storageData.devices },
id: "installation-review",
label: _("Review and install"),
},
Expand Down
6 changes: 2 additions & 4 deletions ui/webui/src/components/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ export const Application = () => {
toggleContextHelp={toggleContextHelp}
hideContextHelp={() => setIsHelpExpanded(false)}
title={title}
deviceData={state.devices}
languages={state.languages}
commonLocales={state.commonLocales}
diskSelection={state.diskSelection}
storageData={state.storage}
localizationData={state.localization}
dispatch={dispatch}
conf={conf}
/>
Expand Down
52 changes: 37 additions & 15 deletions ui/webui/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@

import { useReducer, useCallback } from "react";

/* Initial state for the storeage store substate */
export const storageInitialState = {
devices: {},
diskSelection: {
usableDisks: [],
selectedDisks: [],
ignoredDisks: []
},
};

/* Initial state for the localization store substate */
export const localizationInitialState = {
languages: {},
commonLocales: []
};

/* Initial state for the global store */
export const initialState = {
localization: localizationInitialState,
storage: storageInitialState,
};

/* Custom hook to use the reducer with async actions */
export const useReducerWithThunk = (reducer, initialState) => {
const [state, dispatch] = useReducer(reducer, initialState);

Expand All @@ -36,29 +59,28 @@ export const useReducerWithThunk = (reducer, initialState) => {
};

export const reducer = (state, action) => {
return ({
localization: localizationReducer(state.localization, action),
storage: storageReducer(state.storage, action),
});
};

export const storageReducer = (state = storageInitialState, action) => {
if (action.type === "GET_DEVICE_DATA") {
return { ...state, devices: { ...action.payload.deviceData, ...state.devices } };
} else if (action.type === "GET_DISK_SELECTION") {
return { ...state, diskSelection: action.payload.diskSelection };
} else {
return state;
}
};

export const localizationReducer = (state = localizationInitialState, action) => {
if (action.type === "GET_LANGUAGE_DATA") {
return { ...state, languages: { ...action.payload.languageData, ...state.languages } };
}

if (action.type === "GET_COMMON_LOCALES") {
} else if (action.type === "GET_COMMON_LOCALES") {
return { ...state, commonLocales: action.payload.commonLocales };
} else {
return state;
}
};

export const initialState = {
devices: {},
diskSelection: {
usableDisks: [],
selectedDisks: [],
ignoredDisks: []
},

languages: {},
commonLocales: []
};

0 comments on commit 7660594

Please sign in to comment.