refactor: Move functions inside component

refactor: Remove application param as it is
already present in component props
This commit is contained in:
Aman Harwara
2021-10-01 21:15:38 +05:30
parent 7aea7f330c
commit ce1c519053

View File

@@ -56,11 +56,18 @@ const getEditorIconType = (identifier: string): IconType | null => {
return null; return null;
}; };
const makeEditorDefault = ( export const Defaults: FunctionComponent<Props> = ({ application }) => {
application: WebApplication, const [editorItems, setEditorItems] = useState<DropdownItem[]>([]);
const [defaultEditorValue] = useState(
() =>
getDefaultEditor()?.package_info?.identifier ||
EditorIdentifier.PlainEditor
);
const makeEditorDefault = (
component: SNComponent, component: SNComponent,
currentDefault: SNComponent currentDefault: SNComponent
) => { ) => {
if (currentDefault) { if (currentDefault) {
application.changeItem(currentDefault.uuid, (m) => { application.changeItem(currentDefault.uuid, (m) => {
const mutator = m as ComponentMutator; const mutator = m as ComponentMutator;
@@ -71,31 +78,20 @@ const makeEditorDefault = (
const mutator = m as ComponentMutator; const mutator = m as ComponentMutator;
mutator.defaultEditor = true; mutator.defaultEditor = true;
}); });
}; };
const removeEditorDefault = ( const removeEditorDefault = (component: SNComponent) => {
application: WebApplication,
component: SNComponent
) => {
application.changeAndSaveItem(component.uuid, (m) => { application.changeAndSaveItem(component.uuid, (m) => {
const mutator = m as ComponentMutator; const mutator = m as ComponentMutator;
mutator.defaultEditor = false; mutator.defaultEditor = false;
}); });
}; };
const getDefaultEditor = (application: WebApplication) => { const getDefaultEditor = () => {
return application.componentManager return application.componentManager
.componentsForArea(ComponentArea.Editor) .componentsForArea(ComponentArea.Editor)
.filter((e) => e.isDefaultEditor())[0]; .filter((e) => e.isDefaultEditor())[0];
}; };
export const Defaults: FunctionComponent<Props> = ({ application }) => {
const [editorItems, setEditorItems] = useState<DropdownItem[]>([]);
const [defaultEditorValue] = useState(
() =>
getDefaultEditor(application)?.package_info?.identifier ||
EditorIdentifier.PlainEditor
);
useEffect(() => { useEffect(() => {
const editors = application.componentManager const editors = application.componentManager
@@ -128,15 +124,15 @@ export const Defaults: FunctionComponent<Props> = ({ application }) => {
const editors = application.componentManager.componentsForArea( const editors = application.componentManager.componentsForArea(
ComponentArea.Editor ComponentArea.Editor
); );
const currentDefault = getDefaultEditor(application); const currentDefault = getDefaultEditor();
if (value !== EditorIdentifier.PlainEditor) { if (value !== EditorIdentifier.PlainEditor) {
const editorComponent = editors.filter( const editorComponent = editors.filter(
(e) => e.package_info.identifier === value (e) => e.package_info.identifier === value
)[0]; )[0];
makeEditorDefault(application, editorComponent, currentDefault); makeEditorDefault(editorComponent, currentDefault);
} else { } else {
removeEditorDefault(application, currentDefault); removeEditorDefault(currentDefault);
} }
}; };