-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
29 lines (26 loc) · 1.07 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
'use client';
import { useAppDispatch } from '@/redux/hooks';
import { setUserJson } from '@/redux/features/userSlice';
import { listUsers } from '@/controllers/users';
import { User } from '@/model/userModel';
export default function App({ children }: { children: React.ReactNode }) {
//TODO: useUser hook (all over the entire app. Search for useAppDispatch, useAppSelector and dispatch)
const dispatch = useAppDispatch();
const onChangeUser = (user: User) => dispatch(setUserJson(JSON.stringify(user)));
listUsers().then(users => {
const localStoredUserId = ((typeof window !== 'undefined') && localStorage) ? parseInt(localStorage.getItem('user') ?? '') : null;
if (localStoredUserId) {
const user = users.find(user => user.id === localStoredUserId);
if (user) {
onChangeUser(user)
} else if (users.length > 0) {
onChangeUser(users[0])
}
} else if (users.length > 0) {
onChangeUser(users[0])
}
})
return (
<>{children}</>
)
}