From 3968d98d969cbb283b7adb9c0dd39ecd3ab6dc38 Mon Sep 17 00:00:00 2001
From: Albin Henriksson <albhe428@student.liu.se>
Date: Tue, 6 Apr 2021 14:15:53 +0200
Subject: [PATCH] Fix more bugs originating from changes to api

---
 client/src/App.test.tsx                       | 15 +++++----
 client/src/__mocks__/axios.js                 |  1 +
 client/src/actions/user.ts                    |  9 ++----
 client/src/interfaces/Competition.ts          |  7 ++--
 client/src/interfaces/Role.ts                 |  9 ++++++
 .../pages/admin/components/AddCompetition.tsx |  5 +--
 .../components/CompetitionManager.test.tsx    | 32 +++++++++++--------
 .../admin/components/CompetitionManager.tsx   |  2 +-
 client/src/reducers/userReducer.ts            |  8 +++--
 client/src/utils/checkAuthentication.ts       |  7 +---
 10 files changed, 53 insertions(+), 42 deletions(-)
 create mode 100644 client/src/interfaces/Role.ts

diff --git a/client/src/App.test.tsx b/client/src/App.test.tsx
index 2eca756c..3750ecd1 100644
--- a/client/src/App.test.tsx
+++ b/client/src/App.test.tsx
@@ -1,13 +1,16 @@
 import { render } from '@testing-library/react'
 import React from 'react'
+import { act } from 'react-dom/test-utils'
 import { Provider } from 'react-redux'
 import App from './App'
 import store from './store'
 
-test('renders app', () => {
-  render(
-    <Provider store={store}>
-      <App />
-    </Provider>
-  )
+test('renders app', async () => {
+  await act(async () => {
+    render(
+      <Provider store={store}>
+        <App />
+      </Provider>
+    )
+  })
 })
diff --git a/client/src/__mocks__/axios.js b/client/src/__mocks__/axios.js
index c3547a13..c2953961 100644
--- a/client/src/__mocks__/axios.js
+++ b/client/src/__mocks__/axios.js
@@ -1,3 +1,4 @@
 export default {
   get: jest.fn().mockImplementation(),
+  post: jest.fn().mockImplementation(),
 }
diff --git a/client/src/actions/user.ts b/client/src/actions/user.ts
index 104722eb..b543a68a 100644
--- a/client/src/actions/user.ts
+++ b/client/src/actions/user.ts
@@ -30,15 +30,10 @@ export const getUserData = () => async (dispatch: AppDispatch) => {
   await axios
     .get('/users')
     .then((res) => {
+      console.log(res.data)
       dispatch({
         type: Types.SET_USER,
-        payload: {
-          id: res.data.id,
-          name: res.data.name,
-          email: res.data.email,
-          roleId: res.data.role_id,
-          cityId: res.data.city_id,
-        },
+        payload: res.data,
       })
     })
     .catch((err) => {
diff --git a/client/src/interfaces/Competition.ts b/client/src/interfaces/Competition.ts
index 433ae5a3..7a9c7032 100644
--- a/client/src/interfaces/Competition.ts
+++ b/client/src/interfaces/Competition.ts
@@ -1,7 +1,8 @@
+import { City } from './City'
+
 export interface Competition {
   name: string
-  city_id: number
-  style_id: number
-  year: number
   id: number
+  city: City
+  year: number
 }
diff --git a/client/src/interfaces/Role.ts b/client/src/interfaces/Role.ts
new file mode 100644
index 00000000..4165fe7b
--- /dev/null
+++ b/client/src/interfaces/Role.ts
@@ -0,0 +1,9 @@
+import { City } from './City'
+
+export interface Competition {
+  name: string
+  city: City
+  style_id: number
+  year: number
+  id: number
+}
diff --git a/client/src/pages/admin/components/AddCompetition.tsx b/client/src/pages/admin/components/AddCompetition.tsx
index bc06fdb2..d80195e5 100644
--- a/client/src/pages/admin/components/AddCompetition.tsx
+++ b/client/src/pages/admin/components/AddCompetition.tsx
@@ -37,8 +37,9 @@ const competitionSchema: Yup.SchemaOf<AddCompetitionFormModel> = Yup.object({
 
 const AddCompetition: React.FC = (props: any) => {
   const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(null)
-  const [selectedCity, setSelectedCity] = React.useState<City | undefined>()
   const cities = useAppSelector((state) => state.cities.cities)
+  const userCity = useAppSelector((state) => state.user.userInfo?.city)
+  const [selectedCity, setSelectedCity] = React.useState<City | undefined>(userCity)
   const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
     setAnchorEl(event.currentTarget)
   }
@@ -80,7 +81,7 @@ const AddCompetition: React.FC = (props: any) => {
   }
 
   const competitionInitialValues: AddCompetitionFormModel = {
-    model: { name: '', city: noCitySelected, year: currentYear },
+    model: { name: '', city: userCity?.name ? userCity.name : noCitySelected, year: currentYear },
   }
   return (
     <div>
diff --git a/client/src/pages/admin/components/CompetitionManager.test.tsx b/client/src/pages/admin/components/CompetitionManager.test.tsx
index 3fb03b69..f47d8045 100644
--- a/client/src/pages/admin/components/CompetitionManager.test.tsx
+++ b/client/src/pages/admin/components/CompetitionManager.test.tsx
@@ -8,37 +8,41 @@ import CompetitionManager from './CompetitionManager'
 
 it('renders competition manager', () => {
   const cityRes: any = {
-    data: [
-      {
-        id: 1,
-        name: 'Link\u00f6ping',
-      },
-      {
-        id: 2,
-        name: 'Stockholm',
-      },
-    ],
+    data: {
+      items: [
+        {
+          id: 1,
+          name: 'Link\u00f6ping',
+        },
+        {
+          id: 2,
+          name: 'Stockholm',
+        },
+      ],
+      count: 2,
+      total_count: 3,
+    },
   }
   const compRes: any = {
     data: {
-      competitions: [
+      items: [
         {
           id: 21,
           name: 'ggff',
           year: 2021,
           style_id: 1,
-          city_id: 1,
+          city: cityRes.data.items[0],
         },
         {
           id: 22,
           name: 'sssss',
           year: 2021,
           style_id: 1,
-          city_id: 1,
+          city: cityRes.data.items[1],
         },
       ],
       count: 2,
-      total: 3,
+      total_count: 3,
     },
   }
 
diff --git a/client/src/pages/admin/components/CompetitionManager.tsx b/client/src/pages/admin/components/CompetitionManager.tsx
index 2acce64b..ff73a994 100644
--- a/client/src/pages/admin/components/CompetitionManager.tsx
+++ b/client/src/pages/admin/components/CompetitionManager.tsx
@@ -152,7 +152,7 @@ const CompetitionManager: React.FC = (props: any) => {
                       {row.name}
                     </Button>
                   </TableCell>
-                  <TableCell align="right">{cities.find((city) => city.id === row.city_id)?.name || ''}</TableCell>
+                  <TableCell align="right">{cities.find((city) => city.id === row.city.id)?.name || ''}</TableCell>
                   <TableCell align="right">{row.year}</TableCell>
                   <TableCell align="right">
                     <Button onClick={(event) => handleClick(event, row.id)}>
diff --git a/client/src/reducers/userReducer.ts b/client/src/reducers/userReducer.ts
index 8b651426..9a26b1c9 100644
--- a/client/src/reducers/userReducer.ts
+++ b/client/src/reducers/userReducer.ts
@@ -1,12 +1,14 @@
-//in userReducer.ts
 import { AnyAction } from 'redux'
 import Types from '../actions/types'
+import { City } from '../interfaces/City'
+import { Competition } from './../interfaces/Competition'
 
 interface UserInfo {
   name: string
   email: string
-  roleId: number
-  cityId: number
+  role: Competition
+  city: City
+  id: number
 }
 
 interface UserState {
diff --git a/client/src/utils/checkAuthentication.ts b/client/src/utils/checkAuthentication.ts
index 7f41ef47..83b73554 100644
--- a/client/src/utils/checkAuthentication.ts
+++ b/client/src/utils/checkAuthentication.ts
@@ -22,12 +22,7 @@ export const CheckAuthentication = async () => {
           store.dispatch({ type: Types.SET_AUTHENTICATED })
           store.dispatch({
             type: Types.SET_USER,
-            payload: {
-              name: res.data.name,
-              email: res.data.email,
-              roleId: res.data.role_id,
-              cityId: res.data.city_id,
-            },
+            payload: res.data,
           })
         })
         .catch((error) => {
-- 
GitLab