diff --git a/client/src/Main.tsx b/client/src/Main.tsx index f32aad9fbf044a2fd055fa29e30e0ea830898e5b..45286d547308e5f6280687066140ade4455a424f 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 a8736c4b26b751a48f6f01d888dc46e630c4f6ea..d1084c2077290b65b27531d9d9234f5eb521eb1d 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 0000000000000000000000000000000000000000..4fcde4f55ee72cb9f72f6d79f0e5f1db6995fa4b --- /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 194fb2e727449b39befe2e1d43ee9913cca1ee00..0ffc91af0f04c49e11595d1aa5f7a40a5ae56c45 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 a0dbe2bd54f721ddad785b5b03776c64f87d2030..60c0fde2293baa77f1fadcd59210c8808cb6b232 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 94743ff18c546c80e9ce7400e5afdace7e871bf4..d0f9e8012d5e85f9352ac239499d49fc0a7e7322 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 0000000000000000000000000000000000000000..3540ef86fbd4a921738d896b2b0bebb14b3216e0 --- /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 + } +}