/* eslint-disable @typescript-eslint/no-explicit-any */ import { getDeserializedScreenViewList } from "../helpers/deserializedScreenView"; import { createScreenService, deleteScreenService, loadScreenByIdService, loadScreenService, markDefaultScreenService, updateScreenService, } from "../services"; import { ToolbarOptions } from "../../../type"; import { filterCustomViewData } from "../helpers/filterCustomViewData"; export type ScreenViewProps = { id: string; viewName: string; selected: boolean; }; export const loadScreensAction = async ( uiElementGroupId: string, getUniqueViewId?: (callBack: (response: string) => void) => void, ) => { let uniqueViewId = uiElementGroupId; if (getUniqueViewId) { getUniqueViewId?.((id: string) => { uniqueViewId = id + "-" + uniqueViewId; }); } const response = await loadScreenService(uniqueViewId); const screens: ScreenViewProps[] = response?.data?.map((item: any) => ({ id: item.id, selected: item.selected, viewName: item.viewName, })); return getDeserializedScreenViewList(screens); }; export const loadScreenByIdAction = async (id: string) => { const response = await loadScreenByIdService(id); if (response?.data?.screenViewMetaData) { return JSON.parse(response.data.screenViewMetaData); } return null; }; export const createScreenAction = async ( screenName: string, uiElementGroupId: string, getUniqueViewId?: (callBack: (response: string) => void) => void, screenViewMetaData?: Record | null, ) => { let uniqueViewId = uiElementGroupId; if (getUniqueViewId) { getUniqueViewId?.((id: string) => { uniqueViewId = id + "-" + uniqueViewId; }); } try { const response = await createScreenService(screenName, uniqueViewId); const id = response.data.id; // Seed configuration (current applied config, or a copied view) is written // with the same PATCH the Save button uses, so a created view is persisted // exactly like a saved one — the create endpoint takes name + group only. if (screenViewMetaData && id) { try { await updateScreenService(id, { screenViewMetaData: JSON.stringify(screenViewMetaData), }); } catch (metaError) { // Roll back so the user is never left with a view that exists but // silently lost the configuration they asked to start from. Failing // the whole create keeps the popup's "error → nothing added" contract. try { await deleteScreenService(id); } catch (rollbackError) { console.error( "Failed to roll back a view whose configuration could not be saved:", rollbackError, ); } console.error("Error saving the new view's configuration:", metaError); return { id: "", viewName: "", error: "The view could not be created with the selected configuration. Please try again.", }; } } return { id, viewName: response.data.viewName, error: "", }; } catch (error: any) { if (error.status === 422) { return { id: "", viewName: "", error: "A screen with the same name already exists. Please choose a different name.", }; } return { id: "", viewName: "", error: "An unexpected error occurred while creating the screen.", }; } }; export const updateScreenAction = async ( id: string, data: Record, toolBarOptions: ToolbarOptions, ) => { const dataToSave = filterCustomViewData(data, toolBarOptions); await updateScreenService(id, { screenViewMetaData: JSON.stringify(dataToSave), }); }; /** * Rename a saved view. * * The same `PATCH /api/metadata` the Save button uses, with `viewName` instead * of `screenViewMetaData` — a rename touches the name and nothing else, so a * view's configuration cannot be disturbed by renaming it. * * Returns `{ error }` rather than throwing, and maps **422** to the same * message `createScreenAction` uses for the same cause: the server owns name * uniqueness, and this is the other way a duplicate can be produced. */ export const renameScreenAction = async ( id: string, viewName: string, ): Promise<{ error: string }> => { try { await updateScreenService(id, { viewName }); return { error: "" }; } catch (error: any) { if (error?.status === 422 || error?.response?.status === 422) { return { error: "A screen with the same name already exists. Please choose a different name.", }; } console.error("Error renaming the screen:", error); return { error: "An unexpected error occurred while renaming the screen." }; } }; export const markDefaultScreenAction = async ( id: string, uiElementGroupId: string, getUniqueViewId?: (callBack: (response: string) => void) => void, ) => { let uniqueViewId = uiElementGroupId; if (getUniqueViewId) { getUniqueViewId?.((id: string) => { uniqueViewId = id + "-" + uniqueViewId; }); } await markDefaultScreenService(id, uniqueViewId); }; export const deleteScreenAction = async (id: string) => { await deleteScreenService(id); };