From 79562da05a28e23eb28c0a793bc64b6524d9aee5 Mon Sep 17 00:00:00 2001 From: robban64 <carl@schonfelder.se> Date: Fri, 16 Apr 2021 19:59:01 +0200 Subject: [PATCH] add: reducer for all types --- client/src/Main.tsx | 9 ++++++++- client/src/actions/types.ts | 1 + client/src/actions/typesAction.ts | 15 ++++++++++++++ client/src/interfaces/ApiModels.ts | 11 ++++++++++- client/src/pages/admin/AdminPage.tsx | 15 +++++++------- client/src/reducers/allReducers.ts | 2 ++ client/src/reducers/typesReducer.ts | 29 ++++++++++++++++++++++++++++ 7 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 client/src/actions/typesAction.ts create mode 100644 client/src/reducers/typesReducer.ts diff --git a/client/src/Main.tsx b/client/src/Main.tsx index f32aad9f..45286d54 100644 --- a/client/src/Main.tsx +++ b/client/src/Main.tsx @@ -1,5 +1,7 @@ -import React from 'react' +import React, { useEffect } from 'react' import { BrowserRouter, Route, Switch } from 'react-router-dom' +import { getTypes } from './actions/typesAction' +import { useAppDispatch } from './hooks' import AdminPage from './pages/admin/AdminPage' import LoginPage from './pages/login/LoginPage' import PresentationEditorPage from './pages/presentationEditor/PresentationEditorPage' @@ -11,6 +13,11 @@ import ViewSelectPage from './pages/views/ViewSelectPage' import SecureRoute from './utils/SecureRoute' const Main: React.FC = () => { + const dispatch = useAppDispatch() + useEffect(() => { + dispatch(getTypes()) + }, []) + return ( <BrowserRouter> <Switch> diff --git a/client/src/actions/types.ts b/client/src/actions/types.ts index a8736c4b..d1084c20 100644 --- a/client/src/actions/types.ts +++ b/client/src/actions/types.ts @@ -23,4 +23,5 @@ export default { SET_CITIES: 'SET_CITIES', SET_CITIES_TOTAL: 'SET_CITIES_TOTAL', SET_CITIES_COUNT: 'SET_CITIES_COUNT', + SET_TYPES: 'SET_TYPES', } diff --git a/client/src/actions/typesAction.ts b/client/src/actions/typesAction.ts new file mode 100644 index 00000000..4fcde4f5 --- /dev/null +++ b/client/src/actions/typesAction.ts @@ -0,0 +1,15 @@ +import axios from 'axios' +import { AppDispatch } from './../store' +import Types from './types' + +export const getTypes = () => async (dispatch: AppDispatch) => { + await axios + .get('/misc/types') + .then((res) => { + dispatch({ + type: Types.SET_TYPES, + payload: res.data, + }) + }) + .catch((err) => console.log(err)) +} diff --git a/client/src/interfaces/ApiModels.ts b/client/src/interfaces/ApiModels.ts index 194fb2e7..0ffc91af 100644 --- a/client/src/interfaces/ApiModels.ts +++ b/client/src/interfaces/ApiModels.ts @@ -1,4 +1,4 @@ -interface NameID { +export interface NameID { id: number name: string } @@ -6,6 +6,15 @@ export interface City extends NameID {} export interface Role extends NameID {} export interface MediaType extends NameID {} export interface QuestionType extends NameID {} +export interface ComponentType extends NameID {} +export interface ViewType extends NameID {} + +export interface AllTypes { + media_types: MediaType[] + question_types: QuestionType[] + component_types: ComponentType[] + view_types: ViewType[] +} export interface Media { id: number diff --git a/client/src/pages/admin/AdminPage.tsx b/client/src/pages/admin/AdminPage.tsx index a0dbe2bd..60c0fde2 100644 --- a/client/src/pages/admin/AdminPage.tsx +++ b/client/src/pages/admin/AdminPage.tsx @@ -57,12 +57,16 @@ const AdminView: React.FC = () => { const classes = useStyles() const [openIndex, setOpenIndex] = React.useState(0) const { path, url } = useRouteMatch() + const currentUser = useAppSelector((state) => state.user.userInfo) + const isAdmin = () => currentUser && currentUser.role.name === 'Admin' + const dispatch = useAppDispatch() const handleLogout = () => { dispatch(logoutUser()) } - const dispatch = useAppDispatch() - const currentUser = useAppSelector((state) => state.user.userInfo) - const isAdmin = () => currentUser && currentUser.role.name === 'Admin' + useEffect(() => { + dispatch(getCities()) + dispatch(getRoles()) + }, []) const menuAdminItems = [ { text: 'Startsida', icon: DashboardIcon }, @@ -93,11 +97,6 @@ const AdminView: React.FC = () => { )) } - useEffect(() => { - dispatch(getCities()) - dispatch(getRoles()) - }, []) - return ( <div className={classes.root}> <CssBaseline /> diff --git a/client/src/reducers/allReducers.ts b/client/src/reducers/allReducers.ts index 94743ff1..d0f9e801 100644 --- a/client/src/reducers/allReducers.ts +++ b/client/src/reducers/allReducers.ts @@ -6,6 +6,7 @@ import competitionsReducer from './competitionsReducer' import presentationReducer from './presentationReducer' import rolesReducer from './rolesReducer' import searchUserReducer from './searchUserReducer' +import typesReducer from './typesReducer' import uiReducer from './uiReducer' import userReducer from './userReducer' @@ -18,5 +19,6 @@ const allReducers = combineReducers({ presentation: presentationReducer, roles: rolesReducer, searchUsers: searchUserReducer, + types: typesReducer, }) export default allReducers diff --git a/client/src/reducers/typesReducer.ts b/client/src/reducers/typesReducer.ts new file mode 100644 index 00000000..3540ef86 --- /dev/null +++ b/client/src/reducers/typesReducer.ts @@ -0,0 +1,29 @@ +import { AnyAction } from 'redux' +import Types from '../actions/types' +import { ComponentType, MediaType, QuestionType, ViewType } from '../interfaces/ApiModels' + +interface TypesState { + componentTypes: ComponentType[] + viewTypes: ViewType[] + questionTypes: QuestionType[] + mediaTypes: MediaType[] +} +const initialState: TypesState = { + componentTypes: [], + viewTypes: [], + questionTypes: [], + mediaTypes: [], +} + +export default function (state = initialState, action: AnyAction) { + switch (action.type) { + case Types.SET_TYPES: + state.componentTypes = action.payload.component_types as ComponentType[] + state.viewTypes = action.payload.view_types as ViewType[] + state.questionTypes = action.payload.question_types as QuestionType[] + state.mediaTypes = action.payload.media_types as MediaType[] + return state + default: + return state + } +} -- GitLab