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,47 +56,43 @@ const getEditorIconType = (identifier: string): IconType | null => {
return 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<Props> = ({ application }) => { export const Defaults: FunctionComponent<Props> = ({ application }) => {
const [editorItems, setEditorItems] = useState<DropdownItem[]>([]); const [editorItems, setEditorItems] = useState<DropdownItem[]>([]);
const [defaultEditorValue] = useState( const [defaultEditorValue] = useState(
() => () =>
getDefaultEditor(application)?.package_info?.identifier || getDefaultEditor()?.package_info?.identifier ||
EditorIdentifier.PlainEditor 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(() => { useEffect(() => {
const editors = application.componentManager const editors = application.componentManager
.componentsForArea(ComponentArea.Editor) .componentsForArea(ComponentArea.Editor)
@@ -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);
} }
}; };