Skip to content

Commit

Permalink
feat: redux state management setup
Browse files Browse the repository at this point in the history
  • Loading branch information
TheHappyKoala committed Oct 22, 2023
1 parent 96b4d66 commit ea3fd5f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gatsby-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import wrapWithProvider from "./wrap-with-provider";

export const wrapRootElement = wrapWithProvider;
3 changes: 3 additions & 0 deletions gatsby-ssr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import wrapWithProvider from "./wrap-with-provider";

export const wrapRootElement = wrapWithProvider;
10 changes: 10 additions & 0 deletions src/state/creators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SET_SCENARIO } from "./types";
import { ScenarioType } from "../types/scenario";
import { SetScenarioActionType } from "../types/actions";

export const setScenario = (scenario: ScenarioType): SetScenarioActionType => {
return {
type: SET_SCENARIO,
payload: scenario,
};
};
20 changes: 20 additions & 0 deletions src/state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { legacy_createStore as createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import { SET_SCENARIO } from "./types";
import { ScenarioType } from "../types/scenario";
import { ScenarioActionTypes } from "../types/actions";

const scenarioReducer = (
state = <ScenarioType>{},
action: ScenarioActionTypes,
): ScenarioType => {
switch (action.type) {
case SET_SCENARIO:
return { ...state, ...action.payload };

default:
return state;
}
};

export default createStore(scenarioReducer, applyMiddleware(thunk));
5 changes: 5 additions & 0 deletions src/state/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const SET_SCENARIO = "SET_SCENARIO";
export const MODIFY_SCENARIO_PROPERTY = "MODIFY_SCENARIO_PROPERTY";
export const MODIFY_MASS_PROPERTY = "MODIFY_MASS_PROPERTY";
export const ADD_MASS = "ADD_MASS";
export const DELETE_MASS = "DELETE_MASS";
8 changes: 8 additions & 0 deletions src/types/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ScenarioType } from "./scenario";

export type SetScenarioActionType = {
type: string;
payload: ScenarioType;
};

export type ScenarioActionTypes = SetScenarioActionType;
11 changes: 11 additions & 0 deletions wrap-with-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { ReactElement, ReactNode } from "react";
import { Provider } from "react-redux";
import store from "./src/state";

type Props = {
element: ReactNode;
};

export default ({ element }: Props): ReactElement => (
<Provider store={store}>{element}</Provider>
);

0 comments on commit ea3fd5f

Please sign in to comment.