Skip to content

Commit

Permalink
feat: use context api for handling state
Browse files Browse the repository at this point in the history
Needed to save the user state as well as tests to the context
  • Loading branch information
akhilalekha committed Apr 27, 2021
1 parent a9f2005 commit eec77ce
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions website/context/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SET_PROGRESS = "SET_PROGRESS";
36 changes: 36 additions & 0 deletions website/context/user/UserState.js
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;
5 changes: 5 additions & 0 deletions website/context/user/userContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from "react";

const userContext = createContext();

export default userContext;
13 changes: 13 additions & 0 deletions website/context/user/userReducer.js
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;
}
}
11 changes: 8 additions & 3 deletions website/pages/_app.js
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;

0 comments on commit eec77ce

Please sign in to comment.