P8-Panels/app/context/settings_hooks.js

121 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Парус 8 - Панели мониторинга
Контекст: Параметры - вспомогательные хуки
*/
//---------------------
//Подключение библиотек
//---------------------
import { useCallback, useContext } from "react"; //Классы React
import { BackEndCtx } from "./backend"; //Контекст взаимодействия с сервером
import { object2Base64XML, formatErrorMessage } from "../core/utils"; //Вспомогательные функции
//-----------
//Тело модуля
//-----------
//Хук для SettingsContext
const useSettingsContext = showMsgErr => {
//Подключение к контексту взаимодействия с сервером
const { executeStored, SERV_DATA_TYPE_CLOB } = useContext(BackEndCtx);
//Добавление параметров панели
const putPanelSettings = useCallback(
async ({ kind, panelSettings, loader = true }) => {
//Добавляем параметры
try {
await executeStored({
stored: "PKG_P8PANELS_SETTINGS.PUT",
args: {
NKIND: kind,
CPANELS: {
VALUE: object2Base64XML({
XPANELS: panelSettings
}),
SDATA_TYPE: SERV_DATA_TYPE_CLOB
}
},
loader
});
} catch (e) {
//Разбираем текст ошибки
let errMsg = formatErrorMessage(e.message);
//Отображаем ошибку
showMsgErr(errMsg.text, null, errMsg.fullErrorText);
throw e;
}
},
[SERV_DATA_TYPE_CLOB, executeStored, showMsgErr]
);
//Считывание параметров
const getPanelSettings = useCallback(
async ({ kind, code = null, panel = null, full = false, loader = true }) => {
//Считываем параметры
try {
const res = await executeStored({
stored: "PKG_P8PANELS_SETTINGS.GET",
args: {
NKIND: kind,
SCODE: code,
SPANEL: panel,
NFULL: full ? 1 : 0
},
loader,
respArg: "COUT"
});
return res || {};
} catch (e) {
//Разбираем текст ошибки
let errMsg = formatErrorMessage(e.message);
//Отображаем ошибку
showMsgErr(errMsg.text, null, errMsg.fullErrorText);
throw e;
}
},
[executeStored, showMsgErr]
);
//Инициализация параметров панелей
const initPanelSettings = useCallback(
async ({ kind, panelSettings, full = false, loader = true }) => {
//Инициализируем параметры
try {
const res = await executeStored({
stored: "PKG_P8PANELS_SETTINGS.INIT",
args: {
NKIND: kind,
CPANELS: {
VALUE: object2Base64XML({
XPANELS: panelSettings
}),
SDATA_TYPE: SERV_DATA_TYPE_CLOB
},
NFULL: full ? 1 : 0
},
loader,
respArg: "COUT"
});
return res;
} catch (e) {
//Разбираем текст ошибки
let errMsg = formatErrorMessage(e.message);
//Отображаем ошибку
showMsgErr(errMsg.text, null, errMsg.fullErrorText);
throw e;
}
},
[SERV_DATA_TYPE_CLOB, executeStored, showMsgErr]
);
//Возвращаем результат
return { putPanelSettings, getPanelSettings, initPanelSettings };
};
//----------------
//Интерфейс модуля
//----------------
export { useSettingsContext };