state system

master
Tudor Stanciu 2020-12-19 04:22:38 +02:00
parent e49aea0c7e
commit 22ddc002dd
6 changed files with 95 additions and 52 deletions

View File

@ -1,40 +1,29 @@
import React, { useReducer } from "react"; import React, { useReducer, useMemo } from "react";
import ApplicationStepper from "./layout/stepper/ApplicationStepper"; import Main from "./layout/Main";
import Switcher from "./layout/Switcher";
import { makeStyles } from "@material-ui/core/styles";
import { initialState } from "../state/initialState"; import { initialState } from "../state/initialState";
import { reducer } from "../state/reducer"; import {
reducer,
const useStyles = makeStyles(() => ({ dispatchActions as reducerDispatchActions
app: { } from "../state/reducer";
textAlign: "center", import {
backgroundColor: "#282c34", ApplicationStateContext,
minHeight: "100vh", ApplicationDispatchContext
display: "flex", } from "../state/ApplicationContexts";
flexDirection: "column"
},
content: {
minHeight: "80vh",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
fontSize: "calc(10px + 2vmin)",
color: "white"
}
}));
const App = () => { const App = () => {
const classes = useStyles(); //il fac pt test dar e gresit. daca va fi un singur state se va redesena toata aplicatia de fiecare data.
//testeaza ca se redeseneaza de fiecare data
const [state, dispatch] = useReducer(reducer, initialState); const [state, dispatch] = useReducer(reducer, initialState);
const dispatchActions = useMemo(() => reducerDispatchActions(dispatch), [
dispatch
]);
return ( return (
<div className={classes.app}> <ApplicationStateContext.Provider value={state}>
<ApplicationStepper /> <ApplicationDispatchContext.Provider value={dispatchActions}>
<div className={classes.content}> <Main />
<Switcher /> </ApplicationDispatchContext.Provider>
</div> </ApplicationStateContext.Provider>
</div>
); );
}; };

View File

@ -0,0 +1,38 @@
import React from "react";
import ApplicationStepper from "./stepper/ApplicationStepper";
import Switcher from "./Switcher";
import { makeStyles } from "@material-ui/core/styles";
const useStyles = makeStyles(() => ({
app: {
textAlign: "center",
backgroundColor: "#282c34",
minHeight: "100vh",
display: "flex",
flexDirection: "column"
},
content: {
minHeight: "80vh",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
fontSize: "calc(10px + 2vmin)",
color: "white"
}
}));
const Main = () => {
const classes = useStyles();
return (
<div className={classes.app}>
<ApplicationStepper />
<div className={classes.content}>
<Switcher />
</div>
</div>
);
};
export default Main;

View File

@ -1,8 +1,12 @@
import React from "react"; import React, { useContext } from "react";
import { makeStyles } from "@material-ui/core/styles"; import { makeStyles } from "@material-ui/core/styles";
import { TextField, InputAdornment } from "@material-ui/core"; import { TextField, InputAdornment } from "@material-ui/core";
import { AccountCircleOutlined } from "@material-ui/icons"; import { AccountCircleOutlined } from "@material-ui/icons";
import PasswordField from "../../../components/common/inputs/PasswordField"; import PasswordField from "../../../components/common/inputs/PasswordField";
import {
ApplicationStateContext,
ApplicationDispatchContext
} from "../../../state/ApplicationContexts";
const useStyles = makeStyles(theme => ({ const useStyles = makeStyles(theme => ({
field: { field: {
@ -13,12 +17,22 @@ const useStyles = makeStyles(theme => ({
const LoginComponent = () => { const LoginComponent = () => {
const classes = useStyles(); const classes = useStyles();
const state = useContext(ApplicationStateContext);
const dispatchActions = useContext(ApplicationDispatchContext);
const handleChange = prop => event => {
dispatchActions.onCredentialsChange(prop, event.target.value);
};
return ( return (
<div> <div>
<TextField <TextField
className={classes.field} className={classes.field}
id="username" id="username"
label="Username" label="Username"
onChange={handleChange("userName")}
value={state.credentials.userName}
InputProps={{ InputProps={{
startAdornment: ( startAdornment: (
<InputAdornment position="start"> <InputAdornment position="start">
@ -28,7 +42,13 @@ const LoginComponent = () => {
}} }}
/> />
<br /> <br />
<PasswordField id="password" label="Password" className={classes.field} /> <PasswordField
id="password"
label="Password"
className={classes.field}
onChange={handleChange("password")}
value={state.credentials.password}
/>
</div> </div>
); );
}; };

View File

@ -0,0 +1,4 @@
import React from "react";
export const ApplicationStateContext = React.createContext();
export const ApplicationDispatchContext = React.createContext();

View File

@ -1,3 +1,6 @@
export const initialState = { export const initialState = {
credentials: {} credentials: {
userName: "",
password: ""
}
}; };

View File

@ -1,20 +1,15 @@
export function reducer(state, action) { export function reducer(state, action) {
switch (action.type) { switch (action.type) {
case "OnPagerChange": { case "onCredentialsChange": {
const { prop, value } = action.payload;
return { return {
...state, ...state,
pager: { credentials: {
...state.pager, ...state.credentials,
...action.payload [prop]: value
} }
}; };
} }
case "OnExportRequest": {
return {
...state,
export: action.value
};
}
default: { default: {
return state; return state;
} }
@ -22,12 +17,6 @@ export function reducer(state, action) {
} }
export const dispatchActions = dispatch => ({ export const dispatchActions = dispatch => ({
onFilterPropertyValueChange: (prop, value) => onCredentialsChange: (prop, value) =>
dispatch({ type: "OnFilterPropertyValueChange", payload: { prop, value } }), dispatch({ type: "onCredentialsChange", payload: { prop, value } })
onPagerChange: pager =>
dispatch({ type: "OnPagerChange", payload: { ...pager } }),
onSetCachedFilters: cachedFilters =>
dispatch({ type: "OnSetCachedFilters", payload: cachedFilters }),
onExportRequest: value => dispatch({ type: "OnExportRequest", value: value })
}); });