diff --git a/.eslintrc.js b/.eslintrc.js index 4c0c2c768013e91386c543796b94bfe05a4f4429..518fe41d0379f3dde5e05f5645fc505ce9be3961 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -14,5 +14,6 @@ module.exports = { rules: { 'implicit-arrow-linebreak': 0, + 'react/prop-types': 0, }, }; diff --git a/package-lock.json b/package-lock.json index 1809a4e2106aef7b676b8dd119da39e14f77a134..a1fa745d694cc2f577fd86ec711188fb7d69af0b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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==" } } }, diff --git a/src/core/context/UserContext.tsx b/src/core/context/UserContext.tsx index d7f84377340ab90295155068e6a502dafc59623e..0c8730888f2d7442b21a156755c96ab92c60975d 100644 --- a/src/core/context/UserContext.tsx +++ b/src/core/context/UserContext.tsx @@ -1,67 +1,29 @@ -import React, { createContext, ReactNode, useReducer } from 'react'; +import React, { createContext, useState } from 'react'; +import { Profile, Role } from '../types'; -// Interface definitions -interface IWarnings extends Document { - text: string; - date: Date; - given_by: { - _id: string; - name: string; - }; -} - -enum Role { - Admin, - Staff, - User, -} - -export interface IProfile { - external_id: string; - studentCardNumber?: string; - roomNumber?: string; - picture?: string; - role: Role.Admin | Role.Staff | Role.User; - email: string; - name: string; - warnings: [IWarnings] | []; -} - -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); +export const userContext = createContext({} as ContextProps); 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 }; diff --git a/src/core/types.ts b/src/core/types.ts new file mode 100644 index 0000000000000000000000000000000000000000..23e1422b2a7f798168c27ff6a29b9d89c0b7e753 --- /dev/null +++ b/src/core/types.ts @@ -0,0 +1,31 @@ +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; + }; +} + +export interface News { + title: string; + text: string; + publishedAt: string; +} diff --git a/src/hooks/types.ts b/src/hooks/types.ts index d639a344b70e94866b43d4923f40304dcbb6421c..ab423a80b1fbedf8aebb5dfd10604e9440956365 100644 --- a/src/hooks/types.ts +++ b/src/hooks/types.ts @@ -21,9 +21,3 @@ export interface RequestParams { export type ApiRequest<T> = (params: RequestParams) => AxiosPromise<T>; export type Refetch = (params?: RequestParams) => void; - -export interface INews { - title: string; - text: string; - publishedAt: string; -} diff --git a/src/hooks/useAddNews.ts b/src/hooks/useAddNews.ts index bbc5b5b5df9f740af6b330a5558e549083107460..51d2eb199c2749d2cbdf945825a17b80f0de649b 100644 --- a/src/hooks/useAddNews.ts +++ b/src/hooks/useAddNews.ts @@ -1,12 +1,12 @@ -import { INews } from './types'; +import { News } from '../core/types'; import useRequest from './useRequest'; import useRestQueries from './useRestQueries'; const useAddNews = () => { const { post } = useRestQueries(); - const request = post<INews>('/api/v1/news'); + const request = post<News>('/api/v1/news'); - return useRequest<INews>({ request }); + return useRequest<News>({ request }); }; export default useAddNews; diff --git a/src/hooks/useGetNewsList.ts b/src/hooks/useGetNewsList.ts index 2b36ca743948d4ea6d1320b33ab5117687707389..850572353916fa2cc58ba0ab293c4b32e50c4fc7 100644 --- a/src/hooks/useGetNewsList.ts +++ b/src/hooks/useGetNewsList.ts @@ -1,12 +1,12 @@ -import { INews } from './types'; +import { News } from '../core/types'; import useRequest from './useRequest'; import useRestQueries from './useRestQueries'; -const useGetNewsList = (data?: INews[]) => { +const useGetNewsList = (data?: News[]) => { const { get } = useRestQueries(); - const request = get<INews[]>('/api/v1/news'); + const request = get<News[]>('/api/v1/news'); - return useRequest<INews[]>({ request, initialData: data }); + return useRequest<News[]>({ request, initialData: data }); }; export default useGetNewsList;