Can send requests to server

This commit is contained in:
Pierre HUBERT 2021-05-11 19:03:19 +02:00
parent e9c631e78a
commit b1929fc21f
6 changed files with 116 additions and 35 deletions

3
public/config.json Normal file
View File

@ -0,0 +1,3 @@
{
"api_url": "https://devweb.local/comunic/api-v2/admin/"
}

View File

@ -1,14 +1,14 @@
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',
type: "dark",
primary: lightBlue,
secondary: pink,
},

36
src/helpers/APIHelper.ts Normal file
View File

@ -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<any> {
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;
}

View File

@ -4,6 +4,12 @@
* @author Pierre Hubert
*/
import { serverRequest } from "./APIHelper";
export interface AuthOptions {
reset_token: string;
}
export class AccountHelper {
/**
* Check whether access token are available
@ -13,4 +19,15 @@ export class AccountHelper {
// TODO : implement support
return false;
}
/**
* Get authentication options for a given email address
*
* @param mail Requested email
*/
static async getAuthOptions(mail: string): Promise<AuthOptions> {
return await serverRequest("accounts/auth_options", {
mail: mail,
});
}
}

View File

@ -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<string> {
await this.load();
return cache.api_url;
}
}

View File

@ -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> {
<Typography
component="h1"
variant="h5"
style={{ margin: "10px" }}
style={{ margin: "10px", marginBottom: "50px" }}
>
Comunic Console
</Typography>