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

25
src/reducers/apiKey.ts Normal file
View File

@@ -0,0 +1,25 @@
import { createSlice } from '@reduxjs/toolkit';
interface setValueAction {
payload: string;
type: string;
}
export interface ApiKey {
value: string;
}
export const initialState: ApiKey = { value: '' };
const apiKeySlice = createSlice({
initialState,
name: 'spiKey',
reducers: {
setValue: (state, action: setValueAction) => {
state.value = action.payload;
},
},
});
export const actions = apiKeySlice.actions;
export const { setValue } = apiKeySlice.actions;
export default apiKeySlice.reducer;

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;