wallet-core/packages/merchant-backoffice-ui/src/Application.tsx

184 lines
5.4 KiB
TypeScript
Raw Normal View History

2022-12-19 16:11:50 +01:00
/*
This file is part of GNU Taler
(C) 2021-2023 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
2022-12-19 16:23:39 +01:00
*
* @author Sebastian Javier Marchano (sebasjm)
*/
2022-12-19 16:11:50 +01:00
2023-09-04 19:17:55 +02:00
import { HttpStatusCode, LibtoolVersion } from "@gnu-taler/taler-util";
import {
ErrorType,
TranslationProvider,
useTranslationContext,
2023-05-05 13:38:28 +02:00
} from "@gnu-taler/web-util/browser";
2023-09-04 19:17:55 +02:00
import { Fragment, VNode, h } from "preact";
2022-12-19 16:23:39 +01:00
import { route } from "preact-router";
2023-09-04 19:17:55 +02:00
import { useMemo } from "preact/hooks";
2022-12-19 16:11:50 +01:00
import { ApplicationReadyRoutes } from "./ApplicationReadyRoutes.js";
import { Loading } from "./components/exception/loading.js";
2022-12-19 16:23:39 +01:00
import {
2023-09-04 19:17:55 +02:00
NotConnectedAppMenu,
NotificationCard
2022-12-19 16:23:39 +01:00
} from "./components/menu/index.js";
import {
BackendContextProvider,
useBackendContext,
} from "./context/backend.js";
import { ConfigContextProvider } from "./context/config.js";
2022-12-19 16:11:50 +01:00
import { useBackendConfig } from "./hooks/backend.js";
import { strings } from "./i18n/strings.js";
2022-12-19 16:23:39 +01:00
import LoginPage from "./paths/login/index.js";
2022-12-19 16:11:50 +01:00
export function Application(): VNode {
return (
2022-12-19 16:23:39 +01:00
<BackendContextProvider>
<TranslationProvider source={strings}>
2022-12-19 16:23:39 +01:00
<ApplicationStatusRoutes />
</TranslationProvider>
</BackendContextProvider>
2022-12-19 16:11:50 +01:00
);
}
2023-09-04 19:17:55 +02:00
/**
* Check connection testing against /config
*
* @returns
*/
2022-12-19 16:11:50 +01:00
function ApplicationStatusRoutes(): VNode {
2023-09-04 19:17:55 +02:00
const { url, updateLoginStatus, triedToLog } = useBackendContext();
2022-12-19 16:11:50 +01:00
const result = useBackendConfig();
const { i18n } = useTranslationContext();
2022-12-19 16:11:50 +01:00
const updateLoginInfoAndGoToRoot = (url: string, token?: string) => {
2022-12-19 16:23:39 +01:00
updateLoginStatus(url, token);
route("/");
};
2022-12-19 16:11:50 +01:00
2022-12-19 16:23:39 +01:00
const { currency, version } = result.ok
? result.data
: { currency: "unknown", version: "unknown" };
const ctx = useMemo(() => ({ currency, version }), [currency, version]);
2022-12-19 16:11:50 +01:00
if (!triedToLog) {
2022-12-19 16:23:39 +01:00
return (
<Fragment>
2023-09-04 19:17:55 +02:00
<NotConnectedAppMenu title="Welcome!" />
2022-12-19 16:23:39 +01:00
<LoginPage onConfirm={updateLoginInfoAndGoToRoot} />
</Fragment>
2022-12-19 16:23:39 +01:00
);
2022-12-19 16:11:50 +01:00
}
if (!result.ok) {
if (result.loading) return <Loading />;
if (
result.type === ErrorType.CLIENT &&
result.status === HttpStatusCode.Unauthorized
) {
return (
<Fragment>
2023-09-04 19:17:55 +02:00
<NotConnectedAppMenu title="Login" />
<LoginPage onConfirm={updateLoginInfoAndGoToRoot} />
</Fragment>
);
}
if (
result.type === ErrorType.CLIENT &&
result.status === HttpStatusCode.NotFound
) {
return (
<Fragment>
2023-09-04 19:17:55 +02:00
<NotConnectedAppMenu title="Error" />
<NotificationCard
notification={{
message: i18n.str`Server not found`,
type: "ERROR",
description: `Check your url`,
}}
/>
<LoginPage onConfirm={updateLoginInfoAndGoToRoot} />
</Fragment>
);
}
if (result.type === ErrorType.SERVER) {
<Fragment>
2023-09-04 19:17:55 +02:00
<NotConnectedAppMenu title="Error" />
2022-12-19 16:23:39 +01:00
<NotificationCard
notification={{
message: i18n.str`Server response with an error code`,
2022-12-19 16:23:39 +01:00
type: "ERROR",
description: i18n.str`Got message ${result.message} from ${result.info?.url}`,
2022-12-19 16:23:39 +01:00
}}
/>
<LoginPage onConfirm={updateLoginInfoAndGoToRoot} />
</Fragment>;
}
if (result.type === ErrorType.UNREADABLE) {
<Fragment>
2023-09-04 19:17:55 +02:00
<NotConnectedAppMenu title="Error" />
2022-12-19 16:23:39 +01:00
<NotificationCard
notification={{
message: i18n.str`Response from server is unreadable, http status: ${result.status}`,
2022-12-19 16:23:39 +01:00
type: "ERROR",
description: i18n.str`Got message ${result.message} from ${result.info?.url}`,
2022-12-19 16:23:39 +01:00
}}
/>
<LoginPage onConfirm={updateLoginInfoAndGoToRoot} />
</Fragment>;
}
2022-12-19 16:23:39 +01:00
return (
<Fragment>
2023-09-04 19:17:55 +02:00
<NotConnectedAppMenu title="Error" />
2022-12-19 16:23:39 +01:00
<NotificationCard
notification={{
message: i18n.str`Unexpected Error`,
2022-12-19 16:23:39 +01:00
type: "ERROR",
description: i18n.str`Got message ${result.message} from ${result.info?.url}`,
2022-12-19 16:23:39 +01:00
}}
/>
<LoginPage onConfirm={updateLoginInfoAndGoToRoot} />
</Fragment>
2022-12-19 16:23:39 +01:00
);
}
2022-12-19 16:23:39 +01:00
2023-09-04 19:17:55 +02:00
const SUPPORTED_VERSION = "5:0:1"
if (!LibtoolVersion.compare(
SUPPORTED_VERSION,
result.data.version,
)?.compatible) {
return <Fragment>
<NotConnectedAppMenu title="Error" />
<NotificationCard
notification={{
message: i18n.str`Incompatible version`,
type: "ERROR",
description: i18n.str`Merchant backend server version ${result.data.version} is not compatible with the supported version ${SUPPORTED_VERSION}`,
}}
/>
<LoginPage onConfirm={updateLoginInfoAndGoToRoot} />
</Fragment>
}
2022-12-19 16:23:39 +01:00
return (
<div class="has-navbar-fixed-top">
2022-12-19 16:23:39 +01:00
<ConfigContextProvider value={ctx}>
<ApplicationReadyRoutes />
</ConfigContextProvider>
</div>
);
2022-12-19 16:11:50 +01:00
}