import { SDK } from '@dilvant/bizcard-sdk'; import { Storage } from '@dilvant/front-lib'; import { XM } from '@dilvant/xm-sdk'; import { SectionsSubscriber } from '../classes/SectionSubscriber'; import { STORAGE_KEYS } from '../constants/STORAGE'; import { IApp } from '../interfaces/IApp'; export class CrudSection { static async swapOrder(originSectionId: string, position: string) { if (!originSectionId) return; const app = await Storage.get(STORAGE_KEYS.APP); const { sections } = app; let preorigin = sections[sections.findIndex((s: any) => s._id === originSectionId)].order; let destinationId: any; if (position === 'up') { destinationId = sections[sections.findIndex((s: any) => s._id === originSectionId) - 1] ._id; sections[sections.findIndex((s: any) => s._id === originSectionId)] .order--; sections[sections.findIndex((s: any) => s._id === originSectionId) - 1] .order++; } else { destinationId = sections[sections.findIndex((s: any) => s._id === originSectionId) + 1] ._id; sections[sections.findIndex((s: any) => s._id === originSectionId)] .order++; sections[sections.findIndex((s: any) => s._id === originSectionId) + 1] .order--; } await XM.Section.swap(originSectionId, destinationId); sections.sort((a: any, b: any) => { return a.order - b.order; }); await Storage.set(STORAGE_KEYS.APP, app); SectionsSubscriber.refresh(originSectionId); SectionsSubscriber.refresh(destinationId); return destinationId; } static async visibleSection(sectionId: string, value: boolean) { if (!sectionId) return; let visible = await XM.Section.visible(sectionId, value); const app = await Storage.get(STORAGE_KEYS.APP); const section = app.sections.find((s: any) => s._id === sectionId); section['visible'] = value; await Storage.set(STORAGE_KEYS.APP, app); SectionsSubscriber.refresh(sectionId); return visible; } // TODO: REFACTORIZAR static async changeOrderByNewSection( order: number, { sourceSectionId, appId, pageId, key }: any, staticSection: boolean = false ) { let app: IApp = await Storage.get(STORAGE_KEYS.APP); let { sections } = app; let section = null; if (staticSection) { section = await SDK.Section.createSectionStatic( appId, pageId, key, order ); } else { try { section = await XM.Template.clone( sourceSectionId, appId, pageId, order ); } catch (error) { console.log('error'); } } const sortSection = sections!.sort((a, b) => { return a.order! - b.order!; }); const partOne = sortSection.slice(0, order - 1); const partTwoUnSort = sortSection.slice(order - 1); const partTwoSort = partTwoUnSort.map((p) => { return { ...p, order: p.order! + 1 }; }); const newSection = [...partOne, section, ...partTwoSort]; app.sections = newSection; Storage.set(STORAGE_KEYS.APP, app); return section; } }