From ce1c51905304af72072b49820c76f2dc09b5949e Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Fri, 1 Oct 2021 21:15:38 +0530 Subject: [PATCH] refactor: Move functions inside component refactor: Remove application param as it is already present in component props --- .../panes/general-segments/Defaults.tsx | 70 +++++++++---------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/app/assets/javascripts/preferences/panes/general-segments/Defaults.tsx b/app/assets/javascripts/preferences/panes/general-segments/Defaults.tsx index 6c3bb2423..a3620b812 100644 --- a/app/assets/javascripts/preferences/panes/general-segments/Defaults.tsx +++ b/app/assets/javascripts/preferences/panes/general-segments/Defaults.tsx @@ -56,47 +56,43 @@ const getEditorIconType = (identifier: string): IconType | null => { return null; }; -const makeEditorDefault = ( - application: WebApplication, - component: SNComponent, - currentDefault: SNComponent -) => { - if (currentDefault) { - application.changeItem(currentDefault.uuid, (m) => { - const mutator = m as ComponentMutator; - mutator.defaultEditor = false; - }); - } - application.changeAndSaveItem(component.uuid, (m) => { - const mutator = m as ComponentMutator; - mutator.defaultEditor = true; - }); -}; - -const removeEditorDefault = ( - application: WebApplication, - component: SNComponent -) => { - application.changeAndSaveItem(component.uuid, (m) => { - const mutator = m as ComponentMutator; - mutator.defaultEditor = false; - }); -}; - -const getDefaultEditor = (application: WebApplication) => { - return application.componentManager - .componentsForArea(ComponentArea.Editor) - .filter((e) => e.isDefaultEditor())[0]; -}; - export const Defaults: FunctionComponent = ({ application }) => { const [editorItems, setEditorItems] = useState([]); const [defaultEditorValue] = useState( () => - getDefaultEditor(application)?.package_info?.identifier || + getDefaultEditor()?.package_info?.identifier || EditorIdentifier.PlainEditor ); + const makeEditorDefault = ( + component: SNComponent, + currentDefault: SNComponent + ) => { + if (currentDefault) { + application.changeItem(currentDefault.uuid, (m) => { + const mutator = m as ComponentMutator; + mutator.defaultEditor = false; + }); + } + application.changeAndSaveItem(component.uuid, (m) => { + const mutator = m as ComponentMutator; + mutator.defaultEditor = true; + }); + }; + + const removeEditorDefault = (component: SNComponent) => { + application.changeAndSaveItem(component.uuid, (m) => { + const mutator = m as ComponentMutator; + mutator.defaultEditor = false; + }); + }; + + const getDefaultEditor = () => { + return application.componentManager + .componentsForArea(ComponentArea.Editor) + .filter((e) => e.isDefaultEditor())[0]; + }; + useEffect(() => { const editors = application.componentManager .componentsForArea(ComponentArea.Editor) @@ -128,15 +124,15 @@ export const Defaults: FunctionComponent = ({ application }) => { const editors = application.componentManager.componentsForArea( ComponentArea.Editor ); - const currentDefault = getDefaultEditor(application); + const currentDefault = getDefaultEditor(); if (value !== EditorIdentifier.PlainEditor) { const editorComponent = editors.filter( (e) => e.package_info.identifier === value )[0]; - makeEditorDefault(application, editorComponent, currentDefault); + makeEditorDefault(editorComponent, currentDefault); } else { - removeEditorDefault(application, currentDefault); + removeEditorDefault(currentDefault); } };