From b1929fc21fb194a7536e8623fcbd46ac00cc2613 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 11 May 2021 19:03:19 +0200 Subject: [PATCH] Can send requests to server --- public/config.json | 3 +++ src/App.tsx | 46 ++++++++++++++++++------------------ src/helpers/APIHelper.ts | 36 ++++++++++++++++++++++++++++ src/helpers/AccountHelper.ts | 37 +++++++++++++++++++++-------- src/helpers/ConfigHelper.ts | 22 +++++++++++++++++ src/ui/routes/LoginRoute.tsx | 7 ++++-- 6 files changed, 116 insertions(+), 35 deletions(-) create mode 100644 public/config.json create mode 100644 src/helpers/APIHelper.ts create mode 100644 src/helpers/ConfigHelper.ts diff --git a/public/config.json b/public/config.json new file mode 100644 index 0000000..9d4851d --- /dev/null +++ b/public/config.json @@ -0,0 +1,3 @@ +{ + "api_url": "https://devweb.local/comunic/api-v2/admin/" +} diff --git a/src/App.tsx b/src/App.tsx index 1e67068..c4540e7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,30 +1,30 @@ -import React from 'react'; -import './App.css'; -import { createMuiTheme, ThemeProvider } from '@material-ui/core'; -import { ApplicationDialogsProvider } from './ui/widgets/DialogsProvider'; -import { lightBlue, pink } from '@material-ui/core/colors'; -import { InitWidget } from './ui/widgets/InitWidget'; +import React from "react"; +import "./App.css"; +import { createMuiTheme, ThemeProvider } from "@material-ui/core"; +import { ApplicationDialogsProvider } from "./ui/widgets/DialogsProvider"; +import { lightBlue, pink } from "@material-ui/core/colors"; +import { InitWidget } from "./ui/widgets/InitWidget"; function App() { - const darkTheme = createMuiTheme({ - palette: { - type: 'dark', - primary: lightBlue, - secondary: pink, - }, - }); + const darkTheme = createMuiTheme({ + palette: { + type: "dark", + primary: lightBlue, + secondary: pink, + }, + }); - return ( - -
- {/* Holder of dialogs */} - + return ( + +
+ {/* Holder of dialogs */} + - {/* Initialization widget */} - -
-
- ); + {/* Initialization widget */} + +
+
+ ); } export default App; diff --git a/src/helpers/APIHelper.ts b/src/helpers/APIHelper.ts new file mode 100644 index 0000000..ac67c9b --- /dev/null +++ b/src/helpers/APIHelper.ts @@ -0,0 +1,36 @@ +/** + * API request helper + * + * @author Pierre Hubert + */ + +import { ConfigHelper } from "./ConfigHelper"; + +/** + * Perform an API request + * + * @param uri Target URI on the server + * @param args Arguments to include in the request + * @returns The result of the request, in case of success, + * @throws An exception in case of failure + */ +export async function serverRequest(uri: string, args?: any): Promise { + const requestArguments = args || {}; + + const fd = new FormData(); + for (let arg in requestArguments) { + if (requestArguments.hasOwnProperty(arg)) + fd.append(arg, requestArguments[arg]); + } + + // TODO : add access token, once supported + + const result = await ( + await fetch((await ConfigHelper.apiURL()) + uri, { + method: "POST", + body: fd, + }) + ).json(); + + return result; +} diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 09b59f9..2f98272 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -1,16 +1,33 @@ /** * Account helper - * + * * @author Pierre Hubert */ +import { serverRequest } from "./APIHelper"; + +export interface AuthOptions { + reset_token: string; +} + export class AccountHelper { - /** - * Check whether access token are available - * or not - */ - static get hasAccessToken() : boolean { - // TODO : implement support - return false; - } -} \ No newline at end of file + /** + * Check whether access token are available + * or not + */ + static get hasAccessToken(): boolean { + // TODO : implement support + return false; + } + + /** + * Get authentication options for a given email address + * + * @param mail Requested email + */ + static async getAuthOptions(mail: string): Promise { + return await serverRequest("accounts/auth_options", { + mail: mail, + }); + } +} diff --git a/src/helpers/ConfigHelper.ts b/src/helpers/ConfigHelper.ts new file mode 100644 index 0000000..ed40972 --- /dev/null +++ b/src/helpers/ConfigHelper.ts @@ -0,0 +1,22 @@ +/** + * Configuration helper + * + * @author Pierre Hubert + */ + +let cache: any; + +export class ConfigHelper { + static async load() { + if (!cache) + cache = JSON.parse(await (await fetch("/config.json")).text()); + } + + /** + * Get server API url + */ + static async apiURL(): Promise { + await this.load(); + return cache.api_url; + } +} diff --git a/src/ui/routes/LoginRoute.tsx b/src/ui/routes/LoginRoute.tsx index 731818b..a121def 100644 --- a/src/ui/routes/LoginRoute.tsx +++ b/src/ui/routes/LoginRoute.tsx @@ -21,6 +21,7 @@ import { import LockOutlinedIcon from "@material-ui/icons/LockOutlined"; import React from "react"; +import { AccountHelper } from "../../helpers/AccountHelper"; import { matAlert } from "../widgets/DialogsProvider"; function Copyright() { @@ -63,7 +64,9 @@ export class LoginRoute extends React.Component<{}, LoginRouteState> { if (!this.canSubmit) return; - matAlert("good"); + AccountHelper.getAuthOptions(this.state.currEmail).then((r) => + matAlert("reset token => " + r.reset_token) + ); } render() { @@ -97,7 +100,7 @@ export class LoginRoute extends React.Component<{}, LoginRouteState> { Comunic Console