From 0ee5d08f016b2cf63a77f8a9ee79a59ca11202ca Mon Sep 17 00:00:00 2001 From: abhijithvijayan <34790378+abhijithvijayan@users.noreply.github.com> Date: Mon, 10 Feb 2020 03:53:36 +0530 Subject: [PATCH] feat: show apikey validation response in options page --- src/Options/OptionsForm.tsx | 53 ++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/Options/OptionsForm.tsx b/src/Options/OptionsForm.tsx index f06d80a..19b8b8a 100644 --- a/src/Options/OptionsForm.tsx +++ b/src/Options/OptionsForm.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useEffect } from 'react'; import { withFormik, Field, Form, FormikBag, FormikProps, FormikErrors } from 'formik'; import Icon from '../components/Icon'; @@ -15,11 +15,6 @@ export type OptionsFormValuesProperties = { history: boolean; }; -type ProcessedRequestProperties = { - error: boolean | null; - message: string; -}; - // eslint-disable-next-line @typescript-eslint/no-explicit-any const onSave = (values: OptionsFormValuesProperties): Promise => { // should always return a Promise @@ -28,15 +23,27 @@ const onSave = (values: OptionsFormValuesProperties): Promise => { // Note: The default key-value pairs are not saved to storage without any first interaction const InnerForm: React.FC> = props => { - // ToDo: Replace `Saving` text with Spinning Loader - const { isSubmitting, handleSubmit } = props; + const { isSubmitting, handleSubmit, setStatus, status } = props; + // ToDo: add custom domain form input + + // run on component mount + useEffect(() => { + setStatus({ error: null, message: '' }); + }, [setStatus]); return (
@@ -45,10 +52,12 @@ const InnerForm: React.FC> = props => {
+ + {/* auto save hook component */} { - return isSaving ? 'Saving' : null; + render={({ isSaving }: { isSaving: boolean }): JSX.Element | null => { + return isSaving ? : null; }} /> @@ -90,29 +99,35 @@ const OptionsForm = withFormik + { setSubmitting, setStatus }: FormikBag ) => { + // request API validation request const response: SuccessfulApiKeyCheckProperties | ApiErroredProperties = await messageUtil.send(CHECK_API_KEY, { apikey: values.apikey.trim(), }); if (!response.error) { - // ToDo: show valid api key status - console.log('Valid API Key'); + // set top-level status + setStatus({ error: false, message: 'Valid API Key' }); - const { domains, email } = response.data; // Store user account information + const { domains, email } = response.data; await updateExtensionSettings({ user: { domains, email } }); } else { // ---- errored ---- // + setStatus({ error: true, message: response.message }); + // Delete `user` field from settings await updateExtensionSettings({ user: null }); - - console.log(response.message); } - // enable validate button - setSubmitting(false); + setTimeout(() => { + // Reset status + setStatus({ error: null, message: '' }); + + // enable validate button + setSubmitting(false); + }, 1000); }, displayName: 'OptionsForm',