diff --git a/assets/less/app.less b/assets/less/app.less index fbaa6bf..2c3fdf1 100644 --- a/assets/less/app.less +++ b/assets/less/app.less @@ -28,3 +28,11 @@ canvas#icon { div#status { display: none; } + +[type='text'].border-danger, +.border-danger { + box-shadow: inset 0 -2px 0 @brand-danger; + &:focus { + .box-shadow(inset 0 -2px 0 @brand-danger); + } +} diff --git a/src/components/NavBar.tsx b/src/components/NavBar.tsx index 50d3249..6c5294e 100644 --- a/src/components/NavBar.tsx +++ b/src/components/NavBar.tsx @@ -1,32 +1,13 @@ -import React, { useEffect, useState } from 'react'; -import { useDispatch, useSelector } from 'react-redux'; -import { configLogout, setApiKey } from '../reducers/configReducer'; -import { userLogout } from '../reducers/currentUser'; -import { ApiKeyReducer, ReduxSelector } from '../types/store'; +import React from 'react'; +import { useSelector } from 'react-redux'; +import { ReduxSelector } from '../types/store'; import { User } from '../types/user'; -import apiKeyInvalid from '../utils/apiKey'; -import { fetchUserData } from '../utils/user'; export default function NavBar(): JSX.Element { - const [state, setState] = useState({ - apiKey: '', - apiKeyError: '', - loading: false, - }); - - const dispatch = useDispatch(); - - const { apiKey: apiKeyFromRedux }: ApiKeyReducer = useSelector( - (selector: ReduxSelector) => selector.config, - ); const user: User | undefined = useSelector( (selector: ReduxSelector) => selector.currentUser.user, ); - useEffect(() => { - setState({ ...state, apiKey: apiKeyFromRedux }); - }, [apiKeyFromRedux]); - const signedInAs = () => { if (user) { return ( @@ -127,50 +108,6 @@ export default function NavBar(): JSX.Element { -
  • -
    - {state.apiKeyError && ( -
    - {state.apiKeyError} -
    - )} -
    - { - const key = e.target.value; - const isApiKeyInvalid = apiKeyInvalid(key); - setState({ ...state, apiKey: key, apiKeyError: isApiKeyInvalid }); - }} - /> - - - -
    -
    -
  • diff --git a/src/components/Options.tsx b/src/components/Options.tsx index f85faf4..aa03784 100644 --- a/src/components/Options.tsx +++ b/src/components/Options.tsx @@ -1,11 +1,14 @@ import React, { useRef, useState, useEffect } from 'react'; import config, { SuccessOrFailType } from '../config/config'; +import apiKeyInvalid from '../utils/apiKey'; +import { logUserIn } from '../utils/user'; import Alert from './Alert'; import SitesList from './SitesList'; interface State { alertText: string; alertType: SuccessOrFailType; + apiKey: string; blacklist: string; displayAlert: boolean; loading: boolean; @@ -18,6 +21,7 @@ export default function Options(): JSX.Element { const [state, setState] = useState({ alertText: config.alert.success.text, alertType: config.alert.success.type, + apiKey: '', blacklist: '', displayAlert: false, loading: false, @@ -31,6 +35,7 @@ export default function Options(): JSX.Element { const restoreSettings = async (): Promise => { const items = await browser.storage.sync.get({ + apiKey: config.apiKey, blacklist: '', loggingStyle: config.loggingStyle, loggingType: config.loggingType, @@ -39,11 +44,12 @@ export default function Options(): JSX.Element { }); setState({ ...state, - blacklist: items.blacklist, - loggingStyle: items.loggingStyle, - loggingType: items.loggingType, - theme: items.theme, - whitelist: items.whitelist, + apiKey: items.apiKey as string, + blacklist: items.blacklist as string, + loggingStyle: items.loggingStyle as string, + loggingType: items.loggingType as string, + theme: items.theme as string, + whitelist: items.whitelist as string, }); }; @@ -63,6 +69,7 @@ export default function Options(): JSX.Element { if (state.loading) return; setState({ ...state, loading: true }); + const apiKey = state.apiKey; const theme = state.theme; const loggingType = state.loggingType; const loggingStyle = state.loggingStyle; @@ -72,23 +79,26 @@ export default function Options(): JSX.Element { // Sync options with google storage. await browser.storage.sync.set({ - blacklist: blacklist, - loggingStyle: loggingStyle, - loggingType: loggingType, - theme: theme, - whitelist: whitelist, + apiKey, + blacklist, + loggingStyle, + loggingType, + theme, + whitelist, }); // Set state to be newly entered values. setState({ ...state, - blacklist: blacklist, + apiKey, + blacklist, displayAlert: true, - loggingStyle: loggingStyle, - loggingType: loggingType, - theme: theme, - whitelist: whitelist, + loggingStyle, + loggingType, + theme, + whitelist, }); + await logUserIn(state.apiKey); }; const updateBlacklistState = (sites: string) => { @@ -142,11 +152,14 @@ export default function Options(): JSX.Element { ); }; + const isApiKeyValid = apiKeyInvalid(state.apiKey) === ''; + return (
    +
    + + +
    + setState({ ...state, apiKey: e.target.value })} + /> +
    +
    +
    diff --git a/src/components/WakaTime.tsx b/src/components/WakaTime.tsx index 9df0ec9..34d9fee 100644 --- a/src/components/WakaTime.tsx +++ b/src/components/WakaTime.tsx @@ -15,7 +15,7 @@ export default function WakaTime(): JSX.Element { }: ApiKeyReducer = useSelector((selector: ReduxSelector) => selector.config); useEffect(() => { - fetchUserData(apiKeyFromRedux, dispatch); + void fetchUserData(apiKeyFromRedux, dispatch); }, []); return ( diff --git a/src/utils/user.ts b/src/utils/user.ts index d87b741..e5c8c20 100644 --- a/src/utils/user.ts +++ b/src/utils/user.ts @@ -5,6 +5,26 @@ import WakaTimeCore from '../core/WakaTimeCore'; import { setUser } from '../reducers/currentUser'; import changeExtensionState from './changeExtensionState'; +export const logUserIn = async (apiKey: string): Promise => { + if (!apiKey) { + await changeExtensionState('notSignedIn'); + return; + } + + try { + await WakaTimeCore.checkAuth(apiKey); + const items = await browser.storage.sync.get({ loggingEnabled: config.loggingEnabled }); + + if (items.loggingEnabled === true) { + await changeExtensionState('allGood'); + } else { + await changeExtensionState('notLogging'); + } + } catch (err: unknown) { + await changeExtensionState('notSignedIn'); + } +}; + export const fetchUserData = async ( apiKey: string, dispatch: Dispatch, @@ -18,7 +38,7 @@ export const fetchUserData = async ( } if (!apiKey) { - await changeExtensionState('notSignedIn'); + return changeExtensionState('notSignedIn'); } try {