diff --git a/src/core/WakaTimeCore.ts b/src/core/WakaTimeCore.ts
index 5616bf6..e66dd1d 100644
--- a/src/core/WakaTimeCore.ts
+++ b/src/core/WakaTimeCore.ts
@@ -31,7 +31,6 @@ class WakaTimeCore {
}
async checkAuth(api_key = ''): Promise
{
- console.log('api_keyapi_keyapi_keyapi_key', api_key);
const userPayload: AxiosResponse = await axios.get(
config.currentUserApiUrl,
{ params: { api_key } },
diff --git a/src/reducers/apiKey.ts b/src/reducers/apiKey.ts
new file mode 100644
index 0000000..442d20d
--- /dev/null
+++ b/src/reducers/apiKey.ts
@@ -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;
diff --git a/src/reducers/currentUser.ts b/src/reducers/currentUser.ts
index 6d3a1e0..e5bb3c4 100644
--- a/src/reducers/currentUser.ts
+++ b/src/reducers/currentUser.ts
@@ -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(`[${name}]`, async () => {
- const userPayload: AxiosResponse = await axios.get(config.currentUserApiUrl);
- return userPayload.data.data;
-});
+export const fetchCurrentUser = createAsyncThunk(
+ `[${name}]`,
+ async (api_key = '') => {
+ const userPayload: AxiosResponse = 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;
diff --git a/src/stores/createStore.ts b/src/stores/createStore.ts
index 1ec6584..5825ddc 100644
--- a/src/stores/createStore.ts
+++ b/src/stores/createStore.ts
@@ -2,11 +2,10 @@ import { configureStore, Store } from '@reduxjs/toolkit';
import { logger } from 'redux-logger';
import { reduxBatch } from '@manaflair/redux-batch';
import devToolsEnhancer from 'remote-redux-devtools';
-import currentUserReducer, {
- initialState as InitalCurrentUser,
- CurrentUser,
-} from '../reducers/currentUser';
+import currentUserReducer, { initialState as InitalCurrentUser } from '../reducers/currentUser';
+import apiKeyReducer from '../reducers/apiKey';
import isProd from '../utils/isProd';
+import { CurrentUser } from '../types/user';
export interface RootState {
currentUser: CurrentUser;
@@ -31,6 +30,7 @@ export default (appName: string): RootStore => {
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(logger),
preloadedState,
reducer: {
+ apiKey: apiKeyReducer,
currentUser: currentUserReducer,
},
});
diff --git a/src/types/store.ts b/src/types/store.ts
new file mode 100644
index 0000000..a472505
--- /dev/null
+++ b/src/types/store.ts
@@ -0,0 +1,10 @@
+import { CurrentUser } from './user';
+
+export interface ApiKeyReducer {
+ value: string;
+}
+
+export interface ReduxSelector {
+ apiKey: ApiKeyReducer;
+ currentUser: CurrentUser;
+}
diff --git a/src/types/user.ts b/src/types/user.ts
index d3431c1..4c93212 100644
--- a/src/types/user.ts
+++ b/src/types/user.ts
@@ -46,3 +46,9 @@ export interface User {
weekday_start: number;
writes_only: boolean;
}
+
+export interface CurrentUser {
+ error?: unknown;
+ pending?: boolean;
+ user?: User;
+}
diff --git a/src/utils/checkCurrentUser.ts b/src/utils/checkCurrentUser.ts
index c5c8f01..c2a06a1 100644
--- a/src/utils/checkCurrentUser.ts
+++ b/src/utils/checkCurrentUser.ts
@@ -1,13 +1,15 @@
import { RootStore } from '../stores/createStore';
import { fetchCurrentUser } from '../reducers/currentUser';
+import { ReduxSelector } from '../types/store';
type unsub = () => void;
export default (store: RootStore) =>
(time: number): unsub => {
const fetchUser = () => {
+ const apiKey: string = (store.getState() as ReduxSelector).apiKey.value;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
- store.dispatch(fetchCurrentUser());
+ store.dispatch(fetchCurrentUser(apiKey));
};
fetchUser();
const timeout = setInterval(fetchUser, time);