-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use context api for handling state
Needed to save the user state as well as tests to the context
- Loading branch information
1 parent
a9f2005
commit eec77ce
Showing
5 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SET_PROGRESS = "SET_PROGRESS"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { useReducer } from "react"; | ||
import UserContext from "./userContext"; | ||
import UserReducer from "./userReducer"; | ||
import { SET_PROGRESS } from "./types"; | ||
|
||
const UserState = props => { | ||
const initialState = { | ||
user: {}, | ||
test: [ | ||
{ | ||
case: ["b=5", "b=5;"], | ||
hint: "b should have value of 5", | ||
isCorrect: false | ||
}, | ||
{} | ||
] | ||
}; | ||
|
||
const [state, dispatch] = useReducer(UserReducer, initialState); | ||
|
||
//set user progress - complete function | ||
|
||
return ( | ||
<UserContext.Provider | ||
value={{ | ||
user: state.user, | ||
test: state.test, | ||
setProgress | ||
}} | ||
> | ||
{props.children} | ||
</UserContext.Provider> | ||
); | ||
}; | ||
|
||
export default UserState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createContext } from "react"; | ||
|
||
const userContext = createContext(); | ||
|
||
export default userContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { SET_PROGRESS } from "..types/"; | ||
|
||
export default function ur(state, action) { | ||
switch (action.type) { | ||
case SET_PROGRESS: | ||
return { | ||
...state, | ||
user: action.payload | ||
}; | ||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import '../styles/globals.css' | ||
import { UserContext } from "../context/user/userContext"; | ||
import "../styles/globals.css"; | ||
|
||
function MyApp({ Component, pageProps }) { | ||
return <Component {...pageProps} /> | ||
return ( | ||
<UserContext> | ||
<Component {...pageProps} /> | ||
</UserContext> | ||
); | ||
} | ||
|
||
export default MyApp | ||
export default MyApp; |