WEB APP: ApplicationContext отвязан от использования глобальных объектов
This commit is contained in:
parent
8592c870d8
commit
ea90de6240
@ -45,13 +45,13 @@ export const INPUTS = {
|
||||
};
|
||||
|
||||
//Типовые сообщения об ошибках
|
||||
export const ERROR = {
|
||||
export const ERRORS = {
|
||||
UNDER_CONSTRUCTION: "Панель в разработке",
|
||||
P8O_API_UNAVAILABLE: '"ПАРУС 8 Онлайн" недоступен',
|
||||
DEFAULT: "Неожиданная ошибка"
|
||||
};
|
||||
|
||||
//Типовые сообщения для ошибок HTTP
|
||||
export const ERROR_HTTP = {
|
||||
export const ERRORS_HTTP = {
|
||||
404: "Адрес не найден"
|
||||
};
|
||||
|
@ -15,7 +15,7 @@ import { NavigationContext, NavigationCtx, getRootLocation } from "./context/nav
|
||||
import { P8PAppErrorPage } from "./components/p8p_app_error_page"; //Страница с ошибкой
|
||||
import { P8PAppWorkspace } from "./components/p8p_app_workspace"; //Рабочее пространство панели
|
||||
import { P8PPanelsMenuGrid, PANEL_SHAPE } from "./components/p8p_panels_menu"; //Меню панелей
|
||||
import { TITLES, BUTTONS, ERROR, ERROR_HTTP } from "../app.text"; //Текстовые ресурсы и константы
|
||||
import { TITLES, BUTTONS, ERRORS, ERRORS_HTTP } from "../app.text"; //Текстовые ресурсы и константы
|
||||
|
||||
//--------------------------
|
||||
//Вспомогательные компоненты
|
||||
@ -35,7 +35,7 @@ const RouterError = ({ homePath }) => {
|
||||
//Генерация содержимого
|
||||
return (
|
||||
<P8PAppErrorPage
|
||||
errorMessage={ERROR_HTTP[routeError.status] ? ERROR_HTTP[routeError.status] : ERROR.DEFAULT}
|
||||
errorMessage={ERRORS_HTTP[routeError.status] ? ERRORS_HTTP[routeError.status] : ERRORS.DEFAULT}
|
||||
onNavigate={handleNavigate}
|
||||
navigateCaption={BUTTONS.NAVIGATE_HOME}
|
||||
/>
|
||||
|
@ -9,11 +9,9 @@
|
||||
|
||||
import React, { useReducer, createContext, useEffect, useContext, useCallback } from "react"; //ReactJS
|
||||
import PropTypes from "prop-types"; //Контроль свойств компонента
|
||||
import { getDisplaySize } from "../core/utils"; //Вспомогательные функции
|
||||
import { APP_AT, INITIAL_STATE, applicationReducer } from "./application_reducer"; //Редьюсер состояния
|
||||
import { MessagingСtx } from "./messaging"; //Контекст отображения сообщений
|
||||
import { BackEndСtx } from "./backend"; //Контекст взаимодействия с сервером
|
||||
import { ERROR } from "../../app.text"; //Текстовые ресурсы и константы
|
||||
|
||||
//---------
|
||||
//Константы
|
||||
@ -22,9 +20,10 @@ import { ERROR } from "../../app.text"; //Текстовые ресурсы и
|
||||
//Клиентский API "ПАРУС 8 Онлайн"
|
||||
const P8O_API = window.parent?.parus?.clientApi;
|
||||
|
||||
//--------------------------------
|
||||
//Вспомогательные классы и функции
|
||||
//--------------------------------
|
||||
//Структура объекта с описанием ошибок
|
||||
const APPLICATION_CONTEXT_ERRORS_SHAPE = PropTypes.shape({
|
||||
P8O_API_UNAVAILABLE: PropTypes.string.isRequired
|
||||
});
|
||||
|
||||
//----------------
|
||||
//Интерфейс модуля
|
||||
@ -34,7 +33,7 @@ const P8O_API = window.parent?.parus?.clientApi;
|
||||
export const ApplicationСtx = createContext();
|
||||
|
||||
//Провайдер контекста приложения
|
||||
export const ApplicationContext = ({ guidGenerator, children }) => {
|
||||
export const ApplicationContext = ({ errors, displaySizeGetter, guidGenerator, children }) => {
|
||||
//Подключим редьюсер состояния
|
||||
const [state, dispatch] = useReducer(applicationReducer, INITIAL_STATE);
|
||||
|
||||
@ -63,18 +62,18 @@ export const ApplicationContext = ({ guidGenerator, children }) => {
|
||||
const _id = id || guidGenerator();
|
||||
P8O_API.ui.openTab({ id: _id, url, caption, onClose: () => (onClose ? onClose(_id) : null) });
|
||||
return _id;
|
||||
} else showMsgErr(ERROR.P8O_API_UNAVAILABLE);
|
||||
} else showMsgErr(errors.P8O_API_UNAVAILABLE);
|
||||
},
|
||||
[showMsgErr, guidGenerator]
|
||||
[showMsgErr, guidGenerator, errors.P8O_API_UNAVAILABLE]
|
||||
);
|
||||
|
||||
//Отображение раздела "ПАРУС 8 Онлайн"
|
||||
const pOnlineShowUnit = useCallback(
|
||||
({ unitCode, showMethod = "main", inputParameters }) => {
|
||||
if (P8O_API) P8O_API.fn.openDocumentModal({ unitcode: unitCode, method: showMethod, inputParameters });
|
||||
else showMsgErr(ERROR.P8O_API_UNAVAILABLE);
|
||||
else showMsgErr(errors.P8O_API_UNAVAILABLE);
|
||||
},
|
||||
[showMsgErr]
|
||||
[showMsgErr, errors.P8O_API_UNAVAILABLE]
|
||||
);
|
||||
|
||||
//Отображение документа "ПАРУС 8 Онлайн"
|
||||
@ -82,9 +81,9 @@ export const ApplicationContext = ({ guidGenerator, children }) => {
|
||||
({ unitCode, document, showMethod = "main", inRnParameter = "in_RN" }) => {
|
||||
if (P8O_API)
|
||||
P8O_API.fn.openDocumentModal({ unitcode: unitCode, method: showMethod, inputParameters: [{ name: inRnParameter, value: document }] });
|
||||
else showMsgErr(ERROR.P8O_API_UNAVAILABLE);
|
||||
else showMsgErr(errors.P8O_API_UNAVAILABLE);
|
||||
},
|
||||
[showMsgErr]
|
||||
[showMsgErr, errors.P8O_API_UNAVAILABLE]
|
||||
);
|
||||
|
||||
//Отображение словаря "ПАРУС 8 Онлайн"
|
||||
@ -92,27 +91,27 @@ export const ApplicationContext = ({ guidGenerator, children }) => {
|
||||
({ unitCode, showMethod = "main", inputParameters, callBack }) => {
|
||||
if (P8O_API)
|
||||
P8O_API.fn.openDictionary({ unitcode: unitCode, method: showMethod, inputParameters }, res => (callBack ? callBack(res) : null));
|
||||
else showMsgErr(ERROR.P8O_API_UNAVAILABLE);
|
||||
else showMsgErr(errors.P8O_API_UNAVAILABLE);
|
||||
},
|
||||
[showMsgErr]
|
||||
[showMsgErr, errors.P8O_API_UNAVAILABLE]
|
||||
);
|
||||
|
||||
//Исполнение пользовательской процедуры "ПАРУС 8 Онлайн"
|
||||
const pOnlineUserProcedure = useCallback(
|
||||
({ code, inputParameters, callBack }) => {
|
||||
if (P8O_API) P8O_API.fn.performUserProcedureSync({ code, inputParameters }, res => (callBack ? callBack(res) : null));
|
||||
else showMsgErr(ERROR.P8O_API_UNAVAILABLE);
|
||||
else showMsgErr(errors.P8O_API_UNAVAILABLE);
|
||||
},
|
||||
[showMsgErr]
|
||||
[showMsgErr, errors.P8O_API_UNAVAILABLE]
|
||||
);
|
||||
|
||||
//Исполнение пользовательского отчёта "ПАРУС 8 Онлайн"
|
||||
const pOnlineUserReport = useCallback(
|
||||
({ code, inputParameters }) => {
|
||||
if (P8O_API) P8O_API.fn.performUserReport({ code, inputParameters });
|
||||
else showMsgErr(ERROR.P8O_API_UNAVAILABLE);
|
||||
else showMsgErr(errors.P8O_API_UNAVAILABLE);
|
||||
},
|
||||
[showMsgErr]
|
||||
[showMsgErr, errors.P8O_API_UNAVAILABLE]
|
||||
);
|
||||
|
||||
//Инициализация приложения
|
||||
@ -130,12 +129,12 @@ export const ApplicationContext = ({ guidGenerator, children }) => {
|
||||
if (!state.initialized) {
|
||||
//Слушаем изменение размеров окна
|
||||
window.addEventListener("resize", () => {
|
||||
setDisplaySize(getDisplaySize());
|
||||
if (displaySizeGetter) setDisplaySize(displaySizeGetter());
|
||||
});
|
||||
//Инициализируем приложение
|
||||
initApp();
|
||||
}
|
||||
}, [state.initialized, initApp]);
|
||||
}, [state.initialized, initApp, displaySizeGetter]);
|
||||
|
||||
//Вернём компонент провайдера
|
||||
return (
|
||||
@ -158,6 +157,8 @@ export const ApplicationContext = ({ guidGenerator, children }) => {
|
||||
|
||||
//Контроль свойств - Провайдер контекста приложения
|
||||
ApplicationContext.propTypes = {
|
||||
errors: APPLICATION_CONTEXT_ERRORS_SHAPE.isRequired,
|
||||
displaySizeGetter: PropTypes.func,
|
||||
guidGenerator: PropTypes.func.isRequired,
|
||||
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node])
|
||||
};
|
||||
|
@ -10,7 +10,7 @@
|
||||
import React, { useContext } from "react"; //Классы React
|
||||
import { NavigationCtx } from "../../context/navigation"; //Контекст навигации
|
||||
import { P8PAppErrorPage } from "../../components/p8p_app_error_page"; //Страница с ошибкой
|
||||
import { BUTTONS, ERROR } from "../../../app.text"; //Текстовые ресурсы и константы
|
||||
import { BUTTONS, ERRORS } from "../../../app.text"; //Текстовые ресурсы и константы
|
||||
|
||||
//-----------
|
||||
//Тело модуля
|
||||
@ -22,7 +22,7 @@ const Dummy = () => {
|
||||
const { navigateBack } = useContext(NavigationCtx);
|
||||
|
||||
//Генерация содержимого
|
||||
return <P8PAppErrorPage errorMessage={ERROR.UNDER_CONSTRUCTION} onNavigate={() => navigateBack()} navigateCaption={BUTTONS.NAVIGATE_BACK} />;
|
||||
return <P8PAppErrorPage errorMessage={ERRORS.UNDER_CONSTRUCTION} onNavigate={() => navigateBack()} navigateCaption={BUTTONS.NAVIGATE_BACK} />;
|
||||
};
|
||||
|
||||
//----------------
|
||||
|
@ -12,7 +12,8 @@ import { MessagingContext } from "./context/messaging"; //Контекст со
|
||||
import { BackEndContext } from "./context/backend"; //Контекст взаимодействия с сервером
|
||||
import { ApplicationContext } from "./context/application"; //Контекст приложения
|
||||
import { App } from "./app"; //Приложение
|
||||
import { genGUID } from "./core/utils"; //Вспомогательные функции
|
||||
import { ERRORS } from "../app.text"; //Текстовые ресурсы и константы
|
||||
import { getDisplaySize, genGUID } from "./core/utils"; //Вспомогательные функции
|
||||
import client from "./core/client"; //Клиент для взаимодействия с сервером
|
||||
|
||||
//-----------
|
||||
@ -24,7 +25,7 @@ const Root = () => {
|
||||
return (
|
||||
<MessagingContext>
|
||||
<BackEndContext client={client}>
|
||||
<ApplicationContext guidGenerator={genGUID}>
|
||||
<ApplicationContext errors={ERRORS} displaySizeGetter={getDisplaySize} guidGenerator={genGUID}>
|
||||
<App />
|
||||
</ApplicationContext>
|
||||
</BackEndContext>
|
||||
|
Loading…
x
Reference in New Issue
Block a user