chore: store apiKey in redux store

This commit is contained in:
Sebastian Velez
2023-01-13 14:03:04 -05:00
parent def9fe1243
commit 6dfa40e026
9 changed files with 128 additions and 64 deletions

View File

@@ -1,21 +1,26 @@
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios, { AxiosResponse } from 'axios';
import { User, UserPayload } from '../types/user';
import { CurrentUser, User, UserPayload } from '../types/user';
import config from '../config/config';
interface setUserAction {
payload: User | undefined;
type: string;
}
type NameType = 'currentUser';
export const name: NameType = 'currentUser';
export const fetchCurrentUser = createAsyncThunk<User, undefined>(`[${name}]`, async () => {
const userPayload: AxiosResponse<UserPayload> = await axios.get(config.currentUserApiUrl);
return userPayload.data.data;
});
export const fetchCurrentUser = createAsyncThunk<User, string>(
`[${name}]`,
async (api_key = '') => {
const userPayload: AxiosResponse<UserPayload> = await axios.get(config.currentUserApiUrl, {
params: { api_key },
});
return userPayload.data.data;
},
);
export interface CurrentUser {
error?: unknown;
pending?: boolean;
user?: User;
}
export const initialState: CurrentUser = {};
const currentUser = createSlice({
@@ -30,8 +35,13 @@ const currentUser = createSlice({
},
initialState,
name,
reducers: {},
reducers: {
setUser: (state, action: setUserAction) => {
state.user = action.payload;
},
},
});
export const actions = currentUser.actions;
export const { setUser } = currentUser.actions;
export default currentUser.reducer;