import { XM } from '@dilvant/xm-sdk'; import { SDK } from '@dilvant/bizcard-sdk'; import { Storage } from '@dilvant/front-lib'; import { STORAGE_KEYS } from '../constants/STORAGE'; import { IButton, ISection } from '../interfaces/ISection'; import { InAppLookup } from './../classes/InAppLookup'; import { InObjectLookup } from './../classes/InObjectLookup'; import { SectionsSubscriber } from './../classes/SectionSubscriber'; export class CrudButton { static async saveButton(button: any) { if (!button) return; let { action } = button; delete button.action; let dbButton: any = undefined; if (action && action === 'visible') { dbButton = await XM.Button.visible(button._id, button.visible); } else { // TODO: XM Button save // dbButton = await XM.Button.save(button); dbButton = await SDK.Button.save(button); } if (!dbButton?._id) return; await InAppLookup.findAndReplace(button._id, dbButton, Object.keys(button)); SectionsSubscriber.refresh(dbButton.parent); return dbButton; } static async resizingLayout(sectionId: string, columns: string) { const { buttonAttributes, sectionAttribute } = await XM.App.resizeLayout( sectionId, columns ); let newArray = [...buttonAttributes, ...sectionAttribute]; let a = await InAppLookup.findAndReplaceArray(newArray); SectionsSubscriber.refresh(sectionId); return sectionId; } static async removeButton(button: any) { if (!button) return; // TODO: XM Button removeById // button = await XM.Button.removeById(button._id); button = await SDK.Button.removeById(button._id); await InAppLookup.removeItem(button.parent, button, 'buttons'); SectionsSubscriber.refresh(button.parent); return button; } static async sortButton( orders: { _id: string; order: number }[], sectionId: string ) { if (!orders) return; orders = await XM.Button.sort({ request: orders }); const app = await Storage.get(STORAGE_KEYS.APP); const section: ISection = await InObjectLookup.find(app, sectionId); const newOrder = section .buttons!.map((b) => { const item = orders.find((o) => o._id === b._id); if (!item) return; const newOrder = orders .map((b) => { const item = section.buttons!.find((o) => o._id === b._id); if (!item) return; item.order = b.order; return item; }) .filter((r) => r); return b; }) .filter((r) => r); section.buttons = newOrder; await Storage.set(STORAGE_KEYS.APP, app); return orders; } static async createButon(params: { sectionId: string }) { if (!params) return; const { button } = await XM.Button.contactLinkButtonCreate( params.sectionId ); const newButton = await XM.Button.viewer(button._id); await InAppLookup.addItem(params.sectionId, newButton, 'buttons'); SectionsSubscriber.refresh(params.sectionId); return newButton; } static async cloneButtonStyle(buttonId: string, sectionId: string) { if (!buttonId) return; let buttons: any; try { await XM.Button.cloneFormat(buttonId); setTimeout(async () => { buttons = await XM.Section.viewerContactLink(sectionId); await InAppLookup.findAndReplaceCompleteArrays( sectionId, buttons.buttons, 'buttons' ); SectionsSubscriber.refresh(sectionId); }, 1000); } catch (error) { console.log(error); } return buttons; } }