add store props

This commit is contained in:
eglehelms
2022-11-28 10:44:38 +01:00
parent 2ad285faed
commit 0e0978c5f3
3 changed files with 35 additions and 11 deletions

View File

@@ -9,6 +9,7 @@ import { Scope } from "src/store/types";
/** Wrapper to pass different user scopes as enum values */ /** Wrapper to pass different user scopes as enum values */
const ScopedAccessTestWrapper = (props: Partial<ScopedAccessProps>) => { const ScopedAccessTestWrapper = (props: Partial<ScopedAccessProps>) => {
console.log(props);
return ( return (
<TestProvider> <TestProvider>
<ScopedAccess scope={props.scope!}> <ScopedAccess scope={props.scope!}>

View File

@@ -21,7 +21,7 @@ import type {
ACL, ACL,
} from "./types"; } from "./types";
const initialState: State = { export const initialState: State = {
featureFlags: { featureFlags: {
/** Placeholder since we may need feature-flags in the future... */ /** Placeholder since we may need feature-flags in the future... */
development: import.meta.env.DEV, development: import.meta.env.DEV,
@@ -33,7 +33,10 @@ const initialState: State = {
serviceProviders: [], serviceProviders: [],
}; };
const reducer: React.Reducer<State, Action<keyof State>> = (state, action) => { export const reducer: React.Reducer<State, Action<keyof State>> = (
state,
action
) => {
switch (action.type) { switch (action.type) {
case "user": case "user":
case "toast": case "toast":
@@ -51,7 +54,7 @@ let toastTimeout: number;
/** Async middlewares */ /** Async middlewares */
/** Proxies dispatch to reducer */ /** Proxies dispatch to reducer */
const middleware: MiddleWare = (dispatch) => { export const middleware: MiddleWare = (dispatch) => {
/** This generic implementation enforces global dispatch type-safety */ /** This generic implementation enforces global dispatch type-safety */
return <Type extends keyof State>(action: Action<Type>) => { return <Type extends keyof State>(action: Action<Type>) => {
switch (action.type) { switch (action.type) {

View File

@@ -1,18 +1,31 @@
import React from "react"; import React, { useReducer } from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom"; import { BrowserRouter, Route, Routes } from "react-router-dom";
import { StateProvider } from "src/store"; import {
initialState,
middleware,
reducer,
StateContext,
StateProvider,
} from "src/store";
import { AuthContext } from "src/router/auth"; import { AuthContext } from "src/router/auth";
import { MSG_SOMETHING_WRONG } from "src/constants"; import { MSG_SOMETHING_WRONG } from "src/constants";
import type { AuthStateContext } from "src/router/auth"; import { AuthStateContext, parseJwt } from "src/router/auth";
import type { UserLogin } from "src/api/types"; import type { UserLogin } from "src/api/types";
import userLogin from "../../cypress/fixtures/userLogin.json"; import userLogin from "../../cypress/fixtures/userLogin.json";
import {
Action,
AppStateContext,
GlobalDispatch,
State,
} from "src/store/types";
type TestProviderProps = Partial<AuthStateContext> & { type TestProviderProps = Partial<AppStateContext> &
children?: React.ReactNode; Partial<AuthStateContext> & {
}; children?: React.ReactNode;
};
type LayoutProviderProps = TestProviderProps & { type LayoutProviderProps = TestProviderProps & {
outlet: JSX.Element; outlet: JSX.Element;
@@ -33,8 +46,15 @@ export const authProps: AuthStateContext = {
* Use this when you simply need to wrap with state and auth * Use this when you simply need to wrap with state and auth
*/ */
export const TestProvider = ({ children, ...restProps }: TestProviderProps) => { export const TestProvider = ({ children, ...restProps }: TestProviderProps) => {
const userJWT = parseJwt(userLogin.token);
const [state, dispatch]: [State, React.Dispatch<Action<keyof State>>] =
useReducer(reducer, { ...initialState, user: userJWT });
const globalDispatch: GlobalDispatch = middleware(dispatch);
const storeProps: AppStateContext = { state, dispatch: globalDispatch };
return ( return (
<StateProvider> <StateContext.Provider value={storeProps}>
<AuthContext.Provider <AuthContext.Provider
value={{ value={{
...authProps, ...authProps,
@@ -47,7 +67,7 @@ export const TestProvider = ({ children, ...restProps }: TestProviderProps) => {
</Routes> </Routes>
</BrowserRouter> </BrowserRouter>
</AuthContext.Provider> </AuthContext.Provider>
</StateProvider> </StateContext.Provider>
); );
}; };