Skip to content
Snippets Groups Projects
Commit 84f070d4 authored by Chif Gergő's avatar Chif Gergő
Browse files

Refactor context, add types from request

parent a57ef9fc
No related branches found
No related tags found
2 merge requests!7update master to dev,!5Add api data types
...@@ -2512,6 +2512,14 @@ ...@@ -2512,6 +2512,14 @@
} }
} }
}, },
"@material-ui/icons": {
"version": "4.9.1",
"resolved": "https://registry.npmjs.org/@material-ui/icons/-/icons-4.9.1.tgz",
"integrity": "sha512-GBitL3oBWO0hzBhvA9KxqcowRUsA0qzwKkURyC8nppnC3fw54KPKZ+d4V1Eeg/UnDRSzDaI9nGCdel/eh9AQMg==",
"requires": {
"@babel/runtime": "^7.4.4"
}
},
"@material-ui/styles": { "@material-ui/styles": {
"version": "4.10.0", "version": "4.10.0",
"resolved": "https://registry.npmjs.org/@material-ui/styles/-/styles-4.10.0.tgz", "resolved": "https://registry.npmjs.org/@material-ui/styles/-/styles-4.10.0.tgz",
...@@ -2934,7 +2942,6 @@ ...@@ -2934,7 +2942,6 @@
"version": "3.3.1", "version": "3.3.1",
"resolved": "https://registry.npmjs.org/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz", "resolved": "https://registry.npmjs.org/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz",
"integrity": "sha512-iMIqiko6ooLrTh1joXodJK5X9xeEALT1kM5G3ZLhD3hszxBdIEd5C75U834D9mLcINgD4OyZf5uQXjkuYydWvA==", "integrity": "sha512-iMIqiko6ooLrTh1joXodJK5X9xeEALT1kM5G3ZLhD3hszxBdIEd5C75U834D9mLcINgD4OyZf5uQXjkuYydWvA==",
"dev": true,
"requires": { "requires": {
"@types/react": "*", "@types/react": "*",
"hoist-non-react-statics": "^3.3.0" "hoist-non-react-statics": "^3.3.0"
...@@ -3037,7 +3044,6 @@ ...@@ -3037,7 +3044,6 @@
"version": "0.63.6", "version": "0.63.6",
"resolved": "https://registry.npmjs.org/@types/react-native/-/react-native-0.63.6.tgz", "resolved": "https://registry.npmjs.org/@types/react-native/-/react-native-0.63.6.tgz",
"integrity": "sha512-qAv/VOyXAk4it9MOsQoyUjUnEJ3kAW1FCRGi0OvfQDKLH1/FFogVFvoB6xAlBNc6lPyBtCg+nvzj/ScYe0PqCQ==", "integrity": "sha512-qAv/VOyXAk4it9MOsQoyUjUnEJ3kAW1FCRGi0OvfQDKLH1/FFogVFvoB6xAlBNc6lPyBtCg+nvzj/ScYe0PqCQ==",
"dev": true,
"requires": { "requires": {
"@types/react": "*" "@types/react": "*"
} }
...@@ -3080,7 +3086,6 @@ ...@@ -3080,7 +3086,6 @@
"version": "5.1.2", "version": "5.1.2",
"resolved": "https://registry.npmjs.org/@types/styled-components/-/styled-components-5.1.2.tgz", "resolved": "https://registry.npmjs.org/@types/styled-components/-/styled-components-5.1.2.tgz",
"integrity": "sha512-HNocYLfrsnNNm8NTS/W53OERSjRA8dx5Bn6wBd2rXXwt4Z3s+oqvY6/PbVt3e6sgtzI63GX//WiWiRhWur08qQ==", "integrity": "sha512-HNocYLfrsnNNm8NTS/W53OERSjRA8dx5Bn6wBd2rXXwt4Z3s+oqvY6/PbVt3e6sgtzI63GX//WiWiRhWur08qQ==",
"dev": true,
"requires": { "requires": {
"@types/hoist-non-react-statics": "*", "@types/hoist-non-react-statics": "*",
"@types/react": "*", "@types/react": "*",
...@@ -3091,8 +3096,7 @@ ...@@ -3091,8 +3096,7 @@
"csstype": { "csstype": {
"version": "3.0.2", "version": "3.0.2",
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.0.2.tgz", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.0.2.tgz",
"integrity": "sha512-ofovWglpqoqbfLNOTBNZLSbMuGrblAf1efvvArGKOZMBrIoJeu5UsAipQolkijtyQx5MtAzT/J9IHj/CEY1mJw==", "integrity": "sha512-ofovWglpqoqbfLNOTBNZLSbMuGrblAf1efvvArGKOZMBrIoJeu5UsAipQolkijtyQx5MtAzT/J9IHj/CEY1mJw=="
"dev": true
} }
} }
}, },
......
import React, { createContext, ReactNode, useReducer } from 'react'; import React, { createContext, ReactNode, useState } from 'react';
import { Profile } from '../types';
// Interface definitions // Interface definitions
interface IWarnings extends Document { interface IWarnings extends Document {
...@@ -31,37 +32,29 @@ type Props = { ...@@ -31,37 +32,29 @@ type Props = {
children: ReactNode; children: ReactNode;
}; };
interface IContextProps { interface ContextProps {
state: IProfile; profile: Profile;
dispatch: ({ type }: { type: string }) => void; setProfile: (profile: Profile) => void;
} }
// Context // Context
const initialState = { const initialState: Profile = {
external_id: 'abcd', external_id: 'abcd',
studentCardNumber: '1234', studentCardNumber: '1234',
roomNumber: 104, roomNumber: '104',
picture: 'alma.jpg', picture: 'alma.jpg',
role: Role.User, role: Role.User,
email: 'alma@gmail.com', email: 'alma@gmail.com',
name: 'Nagy Gizike', name: 'Nagy Gizike',
warning: [], warnings: [],
}; };
export const userContext = createContext({} as IContextProps); export const userContext = createContext({} as IContextProps);
const { Provider } = userContext; const { Provider } = userContext;
export const UserStateProvider: React.FunctionComponent<Props> = (props: Props) => { export const UserStateProvider: React.FC = ({ children }) => {
const { children } = props; const [profile, setProfile] = useState<Profile>(initialState);
const [state, dispatch] = useReducer((prevState: any, action: any) => {
switch (action.type) {
case 'update':
return action.payload;
default:
return prevState; // do nothing
}
}, initialState);
return <Provider value={{ state, dispatch }}>{children}</Provider>; return <Provider value={{ profile, setProfile }}>{children}</Provider>;
}; };
export default { userContext, UserStateProvider }; export default { userContext, UserStateProvider };
export enum Role {
Admin,
Staff,
User,
}
export interface Profile {
external_id: string;
studentCardNumber: string;
roomNumber?: string;
picture: string;
role: Role.Admin | Role.Staff | Role.User;
email?: string;
name?: string;
warnings: [Warnings] | [];
}
export interface Warnings {
text: string;
date: Date;
given_by: {
_id: string;
name: string;
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment