From 8ac698fda28cb19aef47004023409ecd240cc3c9 Mon Sep 17 00:00:00 2001 From: Divyanshi Singh Date: Apr 02 2021 10:28:33 +0000 Subject: redux-toolkit: reducer splitting, actions implementation --- diff --git a/src/actions/reduxActions.js b/src/actions/reduxActions.js index 80a3461..306bca1 100644 --- a/src/actions/reduxActions.js +++ b/src/actions/reduxActions.js @@ -1,15 +1,8 @@ -import ActionTypes from '../constants'; +import { loadData, loadDataResp } from '../reducers/landingPageSlice'; +import { loadWizardDataResp, loadWizardData } from '../reducers/wizardSlice'; -export const loadDataResp = payload => ({ - type: ActionTypes.LOAD_DATA_RESP, - payload: payload -}) - -export const loadData = payload => dispatch => { - dispatch({ - type: ActionTypes.LOAD_DATA, - payload: payload - }); +export const fetchLandingPageData = (dispatch) => dispatch => { + dispatch(loadData()); fetch(window.env.ORACULUM_API_URL_v1 + "landing_page") .then(blob => blob.json()) @@ -21,17 +14,8 @@ export const loadData = payload => dispatch => { }); } -export const loadWizardDataResp = payload => ({ - type: ActionTypes.LOAD_WIZARD_DATA_RESP, - payload: payload -}) - -export const loadWizardData = payload => dispatch => { - dispatch({ - type: ActionTypes.LOAD_WIZARD_DATA, - payload: payload - }); - +export const fetchWizardData = payload => dispatch => { + dispatch(loadWizardData()) fetch(window.env.ORACULUM_API_URL_v1 + "actions/all") .then(blob => blob.json()) .then(data => { diff --git a/src/configureStore.js b/src/configureStore.js index 2252549..70c6cd6 100644 --- a/src/configureStore.js +++ b/src/configureStore.js @@ -1,7 +1,13 @@ -import { configureStore } from '@reduxjs/toolkit'; +import { configureStore, combineReducers } from '@reduxjs/toolkit'; import logger from 'redux-logger'; import thunkMiddleware from 'redux-thunk'; -import reducer from './reducers'; +import landingPageReducer from './reducers/landingPageSlice'; +import wizardReducer from './reducers/wizardSlice'; + +const reducer = combineReducers({ + landing_page: landingPageReducer, + wizard: wizardReducer +}) export default function configureAppStore(initialState) { const store = configureStore({ diff --git a/src/landingpage/LandingPage.js b/src/landingpage/LandingPage.js index b1bee82..aec079a 100644 --- a/src/landingpage/LandingPage.js +++ b/src/landingpage/LandingPage.js @@ -11,7 +11,7 @@ import Hideable from "./Hideable" import _ from "lodash" import { connect } from "react-redux" -import { loadData } from "../actions/reduxActions" +import { fetchLandingPageData } from "../actions/reduxActions" import Cookies from "universal-cookie" @@ -38,7 +38,7 @@ class LandingPage extends Component { } componentDidMount() { - this.props.dispatch(loadData()) + this.props.dispatch(fetchLandingPageData()) } toggle_config_mode(e) { diff --git a/src/reducers/landingPageSlice.js b/src/reducers/landingPageSlice.js new file mode 100644 index 0000000..d1e2cbb --- /dev/null +++ b/src/reducers/landingPageSlice.js @@ -0,0 +1,28 @@ +import { createSlice } from "@reduxjs/toolkit"; + +const landingPageReducer = createSlice({ + name: "landing_page", + initialState: { + blockerbugs: {}, + devel: 0, + meetings: [], + schedule: [], + last_qa_meeting: {}, + stable: 0, + config_mode: false, + enabled_components: ["events", "blockers", "minutes"], + + loading: false + }, + reducers: { + loadData(state, action) { + if (!state.loading) state.loading = true; + }, + loadDataResp(state, action) { + state.loading = false; + Object.assign(state, { ...action.payload }); + } + } +}); +export const { loadData, loadDataResp } = landingPageReducer.actions; +export default landingPageReducer.reducer; diff --git a/src/reducers/wizardSlice.js b/src/reducers/wizardSlice.js new file mode 100644 index 0000000..f48032b --- /dev/null +++ b/src/reducers/wizardSlice.js @@ -0,0 +1,24 @@ +import { createSlice } from "@reduxjs/toolkit"; + +const wizardReducer = createSlice({ + name: "wizard", + initialState: { + actions: [], + providers: [], + all_actions: [], + + loading: false + }, + reducers: { + loadWizardData(state, action) { + if (!state.loading) state.loading = true; + }, + loadWizardDataResp(state, action) { + state.loading = false; + state.providers = action.payload.providers; + state.all_actions = action.payload.actions; + } + } +}); +export const { loadWizardData, loadWizardDataResp } = wizardReducer.actions; +export default wizardReducer.reducer; diff --git a/src/wizard/Wizard.js b/src/wizard/Wizard.js index a9ecb3e..1b136bd 100644 --- a/src/wizard/Wizard.js +++ b/src/wizard/Wizard.js @@ -6,7 +6,7 @@ import Actions from "./Actions" import { Container, Row } from "reactstrap" import { connect } from "react-redux" -import { loadWizardData } from "../actions/reduxActions" +import { fetchWizardData } from "../actions/reduxActions" class Wizard extends Component { constructor(props) { @@ -26,7 +26,7 @@ class Wizard extends Component { }) }) - this.props.dispatch(loadWizardData()) + this.props.dispatch(fetchWizardData()) } show_actions(actions) {