start refactor
This commit is contained in:
@@ -1,11 +1,31 @@
|
||||
import { AnyAction, Dispatch } from '@reduxjs/toolkit';
|
||||
import axios, { AxiosResponse } from 'axios';
|
||||
|
||||
import moment from 'moment';
|
||||
import config from '../config/config';
|
||||
import WakaTimeCore from '../core/WakaTimeCore';
|
||||
import { setApiKey, setLoggingEnabled, setTotalTimeLoggedToday } from '../reducers/configReducer';
|
||||
import { setUser } from '../reducers/currentUser';
|
||||
import { getHtmlContentByTabId } from '.';
|
||||
import { GrandTotal, Summaries } from '../types/summaries';
|
||||
import { ApiKeyPayload, AxiosUserResponse, User } from '../types/user';
|
||||
import changeExtensionState from './changeExtensionState';
|
||||
|
||||
/**
|
||||
* Checks if the user is logged in.
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
const checkAuth = async (api_key = ''): Promise<User> => {
|
||||
const items = await browser.storage.sync.get({
|
||||
apiUrl: config.apiUrl,
|
||||
currentUserApiEndPoint: config.currentUserApiEndPoint,
|
||||
});
|
||||
const userPayload: AxiosResponse<AxiosUserResponse> = await axios.get(
|
||||
`${items.apiUrl}${items.currentUserApiEndPoint}`,
|
||||
{ params: { api_key } },
|
||||
);
|
||||
return userPayload.data.data;
|
||||
};
|
||||
|
||||
export const logUserIn = async (apiKey: string): Promise<void> => {
|
||||
if (!apiKey) {
|
||||
await changeExtensionState('notSignedIn');
|
||||
@@ -13,7 +33,7 @@ export const logUserIn = async (apiKey: string): Promise<void> => {
|
||||
}
|
||||
|
||||
try {
|
||||
await WakaTimeCore.checkAuth(apiKey);
|
||||
await checkAuth(apiKey);
|
||||
const items = await browser.storage.sync.get({ loggingEnabled: config.loggingEnabled });
|
||||
|
||||
if (items.loggingEnabled === true) {
|
||||
@@ -26,6 +46,47 @@ export const logUserIn = async (apiKey: string): Promise<void> => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches the api token for logged users in wakatime website
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
const fetchApiKey = async (): Promise<string> => {
|
||||
try {
|
||||
const items = await browser.storage.sync.get({
|
||||
apiUrl: config.apiUrl,
|
||||
currentUserApiEndPoint: config.currentUserApiEndPoint,
|
||||
});
|
||||
|
||||
const apiKeyResponse: AxiosResponse<ApiKeyPayload> = await axios.post(
|
||||
`${items.apiUrl}${items.currentUserApiEndPoint}/get_api_key`,
|
||||
);
|
||||
return apiKeyResponse.data.data.api_key;
|
||||
} catch (err: unknown) {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
const getTotalTimeLoggedToday = async (api_key = ''): Promise<GrandTotal> => {
|
||||
const items = await browser.storage.sync.get({
|
||||
apiUrl: config.apiUrl,
|
||||
summariesApiEndPoint: config.summariesApiEndPoint,
|
||||
});
|
||||
|
||||
const today = moment().format('YYYY-MM-DD');
|
||||
const summariesAxiosPayload: AxiosResponse<Summaries> = await axios.get(
|
||||
`${items.apiUrl}${items.summariesApiEndPoint}`,
|
||||
{
|
||||
params: {
|
||||
api_key,
|
||||
end: today,
|
||||
start: today,
|
||||
},
|
||||
},
|
||||
);
|
||||
return summariesAxiosPayload.data.data[0].grand_total;
|
||||
};
|
||||
|
||||
export const fetchUserData = async (
|
||||
apiKey: string,
|
||||
dispatch: Dispatch<AnyAction>,
|
||||
@@ -36,7 +97,7 @@ export const fetchUserData = async (
|
||||
});
|
||||
apiKey = storage.apiKey as string;
|
||||
if (!apiKey) {
|
||||
apiKey = await WakaTimeCore.fetchApiKey();
|
||||
apiKey = await fetchApiKey();
|
||||
if (apiKey) {
|
||||
await browser.storage.sync.set({ apiKey });
|
||||
}
|
||||
@@ -51,8 +112,8 @@ export const fetchUserData = async (
|
||||
|
||||
try {
|
||||
const [data, totalTimeLoggedTodayResponse, items] = await Promise.all([
|
||||
WakaTimeCore.checkAuth(apiKey),
|
||||
WakaTimeCore.getTotalTimeLoggedToday(apiKey),
|
||||
checkAuth(apiKey),
|
||||
getTotalTimeLoggedToday(apiKey),
|
||||
browser.storage.sync.get({ loggingEnabled: config.loggingEnabled }),
|
||||
]);
|
||||
dispatch(setUser(data));
|
||||
@@ -65,21 +126,6 @@ export const fetchUserData = async (
|
||||
|
||||
dispatch(setLoggingEnabled(items.loggingEnabled as boolean));
|
||||
dispatch(setTotalTimeLoggedToday(totalTimeLoggedTodayResponse.text));
|
||||
|
||||
const tabs = await browser.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true,
|
||||
});
|
||||
let html = '';
|
||||
const tabId = tabs[0]?.id;
|
||||
if (tabId) {
|
||||
try {
|
||||
html = await getHtmlContentByTabId(tabId);
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch (error: unknown) {}
|
||||
}
|
||||
|
||||
await WakaTimeCore.recordHeartbeat(html);
|
||||
} catch (err: unknown) {
|
||||
await changeExtensionState('notSignedIn');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user