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 @@
}
}
},
"@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": {
"version": "4.10.0",
"resolved": "https://registry.npmjs.org/@material-ui/styles/-/styles-4.10.0.tgz",
......@@ -2934,7 +2942,6 @@
"version": "3.3.1",
"resolved": "https://registry.npmjs.org/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz",
"integrity": "sha512-iMIqiko6ooLrTh1joXodJK5X9xeEALT1kM5G3ZLhD3hszxBdIEd5C75U834D9mLcINgD4OyZf5uQXjkuYydWvA==",
"dev": true,
"requires": {
"@types/react": "*",
"hoist-non-react-statics": "^3.3.0"
......@@ -3037,7 +3044,6 @@
"version": "0.63.6",
"resolved": "https://registry.npmjs.org/@types/react-native/-/react-native-0.63.6.tgz",
"integrity": "sha512-qAv/VOyXAk4it9MOsQoyUjUnEJ3kAW1FCRGi0OvfQDKLH1/FFogVFvoB6xAlBNc6lPyBtCg+nvzj/ScYe0PqCQ==",
"dev": true,
"requires": {
"@types/react": "*"
}
......@@ -3080,7 +3086,6 @@
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/@types/styled-components/-/styled-components-5.1.2.tgz",
"integrity": "sha512-HNocYLfrsnNNm8NTS/W53OERSjRA8dx5Bn6wBd2rXXwt4Z3s+oqvY6/PbVt3e6sgtzI63GX//WiWiRhWur08qQ==",
"dev": true,
"requires": {
"@types/hoist-non-react-statics": "*",
"@types/react": "*",
......@@ -3091,8 +3096,7 @@
"csstype": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.0.2.tgz",
"integrity": "sha512-ofovWglpqoqbfLNOTBNZLSbMuGrblAf1efvvArGKOZMBrIoJeu5UsAipQolkijtyQx5MtAzT/J9IHj/CEY1mJw==",
"dev": true
"integrity": "sha512-ofovWglpqoqbfLNOTBNZLSbMuGrblAf1efvvArGKOZMBrIoJeu5UsAipQolkijtyQx5MtAzT/J9IHj/CEY1mJw=="
}
}
},
......
import React, { createContext, ReactNode, useReducer } from 'react';
import React, { createContext, ReactNode, useState } from 'react';
import { Profile } from '../types';
// Interface definitions
interface IWarnings extends Document {
......@@ -31,37 +32,29 @@ type Props = {
children: ReactNode;
};
interface IContextProps {
state: IProfile;
dispatch: ({ type }: { type: string }) => void;
interface ContextProps {
profile: Profile;
setProfile: (profile: Profile) => void;
}
// Context
const initialState = {
const initialState: Profile = {
external_id: 'abcd',
studentCardNumber: '1234',
roomNumber: 104,
roomNumber: '104',
picture: 'alma.jpg',
role: Role.User,
email: 'alma@gmail.com',
name: 'Nagy Gizike',
warning: [],
warnings: [],
};
export const userContext = createContext({} as IContextProps);
const { Provider } = userContext;
export const UserStateProvider: React.FunctionComponent<Props> = (props: Props) => {
const { children } = props;
const [state, dispatch] = useReducer((prevState: any, action: any) => {
switch (action.type) {
case 'update':
return action.payload;
default:
return prevState; // do nothing
}
}, initialState);
export const UserStateProvider: React.FC = ({ children }) => {
const [profile, setProfile] = useState<Profile>(initialState);
return <Provider value={{ state, dispatch }}>{children}</Provider>;
return <Provider value={{ profile, setProfile }}>{children}</Provider>;
};
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.
Please register or to comment