133 lines
3.3 KiB
TypeScript
133 lines
3.3 KiB
TypeScript
import {
|
|
getUserData,
|
|
setIsLoggedInData,
|
|
setUsernameData,
|
|
setHasSeenTutorialData,
|
|
setAccessTokenData,
|
|
getAccessTokenData,
|
|
setActiveSessionData,
|
|
} from '../dataApi';
|
|
import { ActionType } from '../../util/types';
|
|
import { UserState } from './user.state';
|
|
import { isValidToken } from '../../context/jwt/utils';
|
|
import axios from 'axios';
|
|
import { endpoints } from '../../pages/MyLogin/endpoints';
|
|
|
|
export const loadUserData = () => async (dispatch: React.Dispatch<any>) => {
|
|
dispatch(setLoading(true));
|
|
|
|
const data = await getUserData();
|
|
dispatch(setData(data));
|
|
|
|
dispatch(setLoading(false));
|
|
};
|
|
|
|
export const setLoading = (isLoading: boolean) =>
|
|
({
|
|
type: 'set-user-loading',
|
|
isLoading,
|
|
} as const);
|
|
|
|
export const setData = (data: Partial<UserState>) =>
|
|
({
|
|
type: 'set-user-data',
|
|
data,
|
|
} as const);
|
|
|
|
export const logoutUser = () => async (dispatch: React.Dispatch<any>) => {
|
|
//
|
|
await setIsLoggedInData(false);
|
|
dispatch(setUsername());
|
|
};
|
|
|
|
export const setIsLoggedIn = (loggedIn: boolean) => async (dispatch: React.Dispatch<any>) => {
|
|
await setIsLoggedInData(loggedIn);
|
|
return {
|
|
type: 'set-is-loggedin',
|
|
loggedIn,
|
|
} as const;
|
|
};
|
|
|
|
export const setUsername = (username?: string) => async (dispatch: React.Dispatch<any>) => {
|
|
await setUsernameData(username);
|
|
console.log('setUsername triggered');
|
|
|
|
return {
|
|
type: 'set-username',
|
|
username,
|
|
} as const;
|
|
};
|
|
|
|
export const setAccessToken = (token?: string) => async (dispatch: React.Dispatch<any>) => {
|
|
await setAccessTokenData(token);
|
|
|
|
return {
|
|
type: 'set-access-token',
|
|
token,
|
|
} as const;
|
|
};
|
|
|
|
export const setActiveSession = (session: any) => async (dispatch: React.Dispatch<any>) => {
|
|
await setActiveSessionData(session);
|
|
return {
|
|
type: 'set-active-session',
|
|
session,
|
|
} as const;
|
|
};
|
|
|
|
export const checkUserSession = () => async (dispatch: React.Dispatch<any>) => {
|
|
let accessToken = (await getAccessTokenData()).value;
|
|
console.log('check user session');
|
|
let sessionValid = false;
|
|
|
|
try {
|
|
if (accessToken && isValidToken(accessToken)) {
|
|
const res = await axios.get(endpoints.auth.me, {
|
|
headers: { Authorization: `Bearer ${accessToken}` },
|
|
});
|
|
|
|
const { user } = res.data;
|
|
|
|
setActiveSession({ user: { ...user, accessToken }, loading: false });
|
|
sessionValid = true;
|
|
console.log('session valid');
|
|
} else {
|
|
setActiveSession({ user: null, loading: false });
|
|
console.log('session not valid');
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
setActiveSession({ user: null, loading: false });
|
|
}
|
|
|
|
return {
|
|
type: 'check-user-session',
|
|
sessionValid,
|
|
} as const;
|
|
};
|
|
|
|
export const setHasSeenTutorial =
|
|
(hasSeenTutorial: boolean) => async (dispatch: React.Dispatch<any>) => {
|
|
await setHasSeenTutorialData(hasSeenTutorial);
|
|
return {
|
|
type: 'set-has-seen-tutorial',
|
|
hasSeenTutorial,
|
|
} as const;
|
|
};
|
|
|
|
export const setDarkMode = (darkMode: boolean) =>
|
|
({
|
|
type: 'set-dark-mode',
|
|
darkMode,
|
|
} as const);
|
|
|
|
export type UserActions =
|
|
| ActionType<typeof setLoading>
|
|
| ActionType<typeof setData>
|
|
| ActionType<typeof setIsLoggedIn>
|
|
| ActionType<typeof setUsername>
|
|
| ActionType<typeof setHasSeenTutorial>
|
|
| ActionType<typeof setDarkMode>
|
|
| ActionType<typeof setAccessToken>
|
|
| ActionType<typeof checkUserSession>;
|