From 08094e233ce74ec9bb886e8b827262b399d519c4 Mon Sep 17 00:00:00 2001 From: Davor Date: Mon, 1 Aug 2022 23:02:11 +0200 Subject: [PATCH] modify disable app modal --- src/components/Form/Checkbox/Checkbox.tsx | 43 ++++++++++ src/components/Form/Checkbox/index.ts | 1 + src/components/Form/Switch/Switch.tsx | 4 +- src/components/Form/index.ts | 1 + src/components/Modal/Modal/Modal.tsx | 6 +- src/components/Modal/Modal/types.ts | 2 + src/modules/apps/AppSingle.tsx | 84 +++++++++++++------ .../AppInstallModal/AppInstallModal.tsx | 4 + src/services/apps/hooks/use-apps.ts | 7 +- src/services/apps/redux/actions.ts | 30 ++++++- src/services/apps/redux/reducers.ts | 1 + src/services/apps/types.ts | 5 ++ 12 files changed, 153 insertions(+), 35 deletions(-) create mode 100644 src/components/Form/Checkbox/Checkbox.tsx create mode 100644 src/components/Form/Checkbox/index.ts diff --git a/src/components/Form/Checkbox/Checkbox.tsx b/src/components/Form/Checkbox/Checkbox.tsx new file mode 100644 index 0000000..e31c20c --- /dev/null +++ b/src/components/Form/Checkbox/Checkbox.tsx @@ -0,0 +1,43 @@ +import React from 'react'; +import { useController } from 'react-hook-form'; + +/* eslint-disable react/react-in-jsx-scope */ +export const Checkbox = ({ control, name, label, ...props }: CheckboxProps) => { + const { + field, + // fieldState: { invalid, isTouched, isDirty }, + // formState: { touchedFields, dirtyFields }, + } = useController({ + name, + control, + defaultValue: false, + }); + + return ( + <> + {label && ( + + )} + + + ); +}; + +type CheckboxProps = { + control: any; + name: string; + id?: string; + label?: string; + className?: string; +}; diff --git a/src/components/Form/Checkbox/index.ts b/src/components/Form/Checkbox/index.ts new file mode 100644 index 0000000..8f59e4f --- /dev/null +++ b/src/components/Form/Checkbox/index.ts @@ -0,0 +1 @@ +export { Checkbox } from './Checkbox'; diff --git a/src/components/Form/Switch/Switch.tsx b/src/components/Form/Switch/Switch.tsx index 4c10780..d1596da 100644 --- a/src/components/Form/Switch/Switch.tsx +++ b/src/components/Form/Switch/Switch.tsx @@ -6,7 +6,7 @@ function classNames(...classes: any) { return classes.filter(Boolean).join(' '); } /* eslint-disable react/react-in-jsx-scope */ -export const Switch = ({ control, name, label, required }: SwitchProps) => { +export const Switch = ({ control, name, label }: SwitchProps) => { const { field, // fieldState: { invalid, isTouched, isDirty }, @@ -14,7 +14,6 @@ export const Switch = ({ control, name, label, required }: SwitchProps) => { } = useController({ name, control, - rules: { required }, defaultValue: '', }); @@ -49,5 +48,4 @@ type SwitchProps = { control: any; name: string; label?: string; - required?: boolean; }; diff --git a/src/components/Form/index.ts b/src/components/Form/index.ts index 19728f2..efb5065 100644 --- a/src/components/Form/index.ts +++ b/src/components/Form/index.ts @@ -3,3 +3,4 @@ export { Select } from './Select'; export { Switch } from './Switch'; export { CodeEditor } from './CodeEditor'; export { TextArea } from './TextArea'; +export { Checkbox } from './Checkbox'; diff --git a/src/components/Modal/Modal/Modal.tsx b/src/components/Modal/Modal/Modal.tsx index e61aaee..d64acfe 100644 --- a/src/components/Modal/Modal/Modal.tsx +++ b/src/components/Modal/Modal/Modal.tsx @@ -7,9 +7,11 @@ export const Modal: React.FC = ({ open, onClose, onSave, + saveButtonTitle = 'Save Changes', children, title = '', useCancelButton = false, + cancelButtonTitle = 'Cancel', isLoading = false, leftActions = <>, saveButtonDisabled = false, @@ -94,7 +96,7 @@ export const Modal: React.FC = ({ ref={saveButtonRef} disabled={saveButtonDisabled} > - Save Changes + {saveButtonTitle} {useCancelButton && ( )} diff --git a/src/components/Modal/Modal/types.ts b/src/components/Modal/Modal/types.ts index e679e69..52cf784 100644 --- a/src/components/Modal/Modal/types.ts +++ b/src/components/Modal/Modal/types.ts @@ -5,7 +5,9 @@ export type ModalProps = { onClose: () => void; title?: string; onSave?: () => void; + saveButtonTitle?: string; useCancelButton?: boolean; + cancelButtonTitle?: string; isLoading?: boolean; leftActions?: React.ReactNode; saveButtonDisabled?: boolean; diff --git a/src/modules/apps/AppSingle.tsx b/src/modules/apps/AppSingle.tsx index d65b825..2d074a2 100644 --- a/src/modules/apps/AppSingle.tsx +++ b/src/modules/apps/AppSingle.tsx @@ -1,29 +1,53 @@ import React, { useEffect, useState } from 'react'; -import { useParams } from 'react-router-dom'; +import { useNavigate, useParams } from 'react-router-dom'; +import { useForm, useWatch } from 'react-hook-form'; import _ from 'lodash'; import { XCircleIcon } from '@heroicons/react/outline'; -import { useApps } from 'src/services/apps'; +import { DisableAppForm, useApps } from 'src/services/apps'; import { Modal, Tabs } from 'src/components'; +import { Checkbox } from 'src/components/Form'; import { appAccessList } from 'src/components/UserModal/consts'; import { AdvancedTab, GeneralTab } from './components'; export const AppSingle: React.FC = () => { - const [disableApp, setDisableApp] = useState(false); + const [disableAppModal, setDisableAppModal] = useState(false); const [removeAppData, setRemoveAppData] = useState(false); const params = useParams(); const appSlug = params.slug; - const { app, loadApp } = useApps(); + const { app, loadApp, disableApp, clearSelectedApp } = useApps(); + const navigate = useNavigate(); + + const initialDisableData = { slug: appSlug, removeAppData: false }; + + const { control, reset, handleSubmit } = useForm({ + defaultValues: initialDisableData, + }); + + const removeAppDataWatch = useWatch({ + control, + name: 'removeAppData', + }); + + useEffect(() => { + setRemoveAppData(removeAppDataWatch); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [removeAppDataWatch]); useEffect(() => { if (appSlug) { loadApp(appSlug); } + + return () => { + clearSelectedApp(); + }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [appSlug]); if (!app) { return null; } + const appImageSrc = _.find(appAccessList, { name: appSlug })?.image; const appDocumentationUrl = _.find(appAccessList, { name: appSlug })?.documentationUrl; @@ -31,20 +55,28 @@ export const AppSingle: React.FC = () => { window.open(appDocumentationUrl, '_blank', 'noopener,noreferrer'); }; - const handleAutomaticUpdatesChange = () => { - app.automaticUpdates = !app.automaticUpdates; - }; - const tabs = [ { name: 'General', - component: , + component: , }, { name: 'Advanced Configuration', component: }, ]; - const onDisableApp = () => { - // TODO: implement + const onDisableApp = async () => { + try { + await handleSubmit((data) => disableApp(data))(); + } catch (e: any) { + // Continue + } + setDisableAppModal(false); + clearSelectedApp(); + navigate('/apps'); + }; + + const handleCloseDisableModal = () => { + reset(initialDisableData); + setDisableAppModal(false); }; return ( @@ -62,7 +94,7 @@ export const AppSingle: React.FC = () => {