Can request admin settings update

This commit is contained in:
Pierre HUBERT 2021-05-13 16:58:50 +02:00
parent d02d025418
commit 6dde0f40c4
5 changed files with 428 additions and 206 deletions

View File

@ -17,6 +17,12 @@ export interface AdminAccount {
time_create: number; time_create: number;
} }
export interface NewAdminGeneralSettings {
id: number;
name: string;
email: string;
}
const SESSION_STORAGE_TOKEN = "auth_token"; const SESSION_STORAGE_TOKEN = "auth_token";
let currentAccount: AdminAccount | null = null; let currentAccount: AdminAccount | null = null;
@ -114,4 +120,17 @@ export class AccountHelper {
sessionStorage.removeItem(SESSION_STORAGE_TOKEN); sessionStorage.removeItem(SESSION_STORAGE_TOKEN);
document.location.href = document.location.href + ""; document.location.href = document.location.href + "";
} }
/**
* Update an admin's settings
*
* @param a New settings
*/
static async UpdateGeneralSettings(s: NewAdminGeneralSettings) {
await serverRequest("admins/update_general_settings", {
id: s.id,
name: s.name,
email: s.email,
});
}
} }

View File

@ -4,10 +4,19 @@
* @author Pierre Hubert * @author Pierre Hubert
*/ */
import {
Button,
Divider,
Grid,
Paper,
TextField,
Typography,
} from "@material-ui/core";
import React from "react"; import React from "react";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { AccountHelper, AdminAccount } from "../../helpers/AccountHelper"; import { AccountHelper, AdminAccount } from "../../helpers/AccountHelper";
import { AsyncWidget } from "../widgets/AsyncWidget"; import { AsyncWidget } from "../widgets/AsyncWidget";
import { matAlert, snackbar } from "../widgets/DialogsProvider";
export function AccountSettingsRoute() { export function AccountSettingsRoute() {
let params: any = useParams(); let params: any = useParams();
@ -52,6 +61,110 @@ class AccountSettingsRouteInner extends React.Component<
} }
build() { build() {
return <p>{this.state.account.email}</p>; return (
<Grid container spacing={2}>
<GeneralSettings admin={this.state.account}></GeneralSettings>
</Grid>
);
} }
} }
class GeneralSettings extends React.Component<
{ admin: AdminAccount },
{ newName: string; newEmail: string }
> {
constructor(p: any) {
super(p);
this.state = {
newName: this.props.admin.name,
newEmail: this.props.admin.email,
};
this.changedName = this.changedName.bind(this);
this.changedEmail = this.changedEmail.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
changedName(e: React.ChangeEvent<HTMLInputElement>) {
this.setState({ newName: e.target.value });
}
changedEmail(e: React.ChangeEvent<HTMLInputElement>) {
this.setState({ newEmail: e.target.value });
}
get isValid(): boolean {
return this.state.newEmail.length > 2 && this.state.newName.length > 2;
}
async handleSubmit() {
try {
if (!this.isValid) return;
await AccountHelper.UpdateGeneralSettings({
id: this.props.admin.id,
name: this.state.newName,
email: this.state.newEmail,
});
snackbar("Successfully updated admin settings!");
} catch (e) {
console.error(e);
matAlert("Failed to update admin settings!");
}
}
render() {
return (
<SettingsSection title="General settings">
<TextField
required
label="Name"
value={this.state.newName}
onChange={this.changedName}
style={{ width: "100%", paddingBottom: "20px" }}
/>
<TextField
required
label="Email"
value={this.state.newEmail}
onChange={this.changedEmail}
type="mail"
style={{ width: "100%", paddingBottom: "20px" }}
/>
<Button
style={{ alignSelf: "end", marginRight: "10px" }}
disabled={!this.isValid}
onClick={this.handleSubmit}
>
Update
</Button>
</SettingsSection>
);
}
}
function SettingsSection(p: { title: string; children?: React.ReactNode }) {
return (
<Grid item xs={6} spacing={2}>
<Paper>
<Typography variant="h5" style={{ padding: "10px 10px " }}>
General settings
</Typography>
<Divider />
<div
style={{
padding: "10px 10px",
display: "flex",
flexDirection: "column",
}}
>
{p.children}
</div>
</Paper>
</Grid>
);
}

View File

@ -155,7 +155,14 @@ export function MainRoute() {
<Menu></Menu> <Menu></Menu>
</Paper> </Paper>
<div style={{ flex: "1", padding: "50px" }}> <div style={{ flex: "1", padding: "50px", height: "100%" }}>
<div
style={{
height: "100%",
margin: "auto",
maxWidth: "1280px",
}}
>
<Switch> <Switch>
<Route exact path="/"> <Route exact path="/">
<HomeRoute></HomeRoute> <HomeRoute></HomeRoute>
@ -171,6 +178,7 @@ export function MainRoute() {
</Switch> </Switch>
</div> </div>
</div> </div>
</div>
</Router> </Router>
); );
} }

View File

@ -59,7 +59,7 @@ export class AsyncWidget extends React.Component<
} }
componentDidUpdate() { componentDidUpdate() {
if (this.state.key != this.props.key) { if (this.state.key !== this.props.key) {
this.setState({ key: this.props.key }); this.setState({ key: this.props.key });
this.reload(); this.reload();
} }

View File

@ -4,40 +4,56 @@
* @author Pierre Hubert * @author Pierre Hubert
*/ */
import { Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, Button, TextField } from "@material-ui/core"; import {
Dialog,
DialogTitle,
DialogContent,
DialogContentText,
DialogActions,
Button,
TextField,
Snackbar,
IconButton,
} from "@material-ui/core";
import { Close } from "@material-ui/icons";
import React from "react"; import React from "react";
let cache : ApplicationDialogsProvider; let cache: ApplicationDialogsProvider;
export interface TextInputOptions { export interface TextInputOptions {
title ?: string, title?: string;
message ?: string, message?: string;
initialValue ?: string, initialValue?: string;
minLength ?: number, minLength?: number;
maxLength ?: number, maxLength?: number;
label : string, label: string;
} }
interface AppDiagProvState { interface AppDiagProvState {
// Alert dialog // Alert dialog
alertMessage : string, alertMessage: string;
showAlert : boolean, showAlert: boolean;
// Simple snackbar
snackBarMessage: string;
showSnackBar: boolean;
// Confirm dialog // Confirm dialog
confirmMessage: string, confirmMessage: string;
showConfirm: boolean, showConfirm: boolean;
resolveConfirm ?: (confirmed: boolean) => void, resolveConfirm?: (confirmed: boolean) => void;
// Text input dialog // Text input dialog
showInputDialog: boolean, showInputDialog: boolean;
inputValue: string, inputValue: string;
inputOptions: TextInputOptions, inputOptions: TextInputOptions;
resolveInput ?: (text: string) => void, resolveInput?: (text: string) => void;
} }
export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvState> { export class ApplicationDialogsProvider extends React.Component<
{},
AppDiagProvState
> {
private acceptConfirm: () => void; private acceptConfirm: () => void;
private rejectConfirm: () => void; private rejectConfirm: () => void;
private cancelInput: () => void; private cancelInput: () => void;
@ -47,11 +63,14 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
super(props); super(props);
this.state = { this.state = {
// Alert dialog // Alert dialog
alertMessage: "", alertMessage: "",
showAlert: false, showAlert: false,
// Simple snackbar
snackBarMessage: "",
showSnackBar: false,
// Confirm dialog // Confirm dialog
showConfirm: false, showConfirm: false,
confirmMessage: "", confirmMessage: "",
@ -61,13 +80,15 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
showInputDialog: false, showInputDialog: false,
inputValue: "", inputValue: "",
inputOptions: { inputOptions: {
label: "" label: "",
}, },
resolveInput: undefined, resolveInput: undefined,
}; };
this.handleCloseAlert = this.handleCloseAlert.bind(this); this.handleCloseAlert = this.handleCloseAlert.bind(this);
this.handleCloseSnackbar = this.handleCloseSnackbar.bind(this);
this.acceptConfirm = this.handleCloseConfirm.bind(this, true); this.acceptConfirm = this.handleCloseConfirm.bind(this, true);
this.rejectConfirm = this.handleCloseConfirm.bind(this, false); this.rejectConfirm = this.handleCloseConfirm.bind(this, false);
@ -80,64 +101,78 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
this.setState({ this.setState({
showAlert: true, showAlert: true,
alertMessage: message, alertMessage: message,
}) });
} }
handleCloseAlert() { handleCloseAlert() {
this.setState({showAlert: false}); this.setState({ showAlert: false });
} }
showConfirm(message: string) : Promise<boolean> { showSnackbar(message: string) {
this.setState({ showSnackBar: true, snackBarMessage: message });
}
handleCloseSnackbar(
_event: React.SyntheticEvent | React.MouseEvent,
reason?: string
) {
if (reason === "clickaway") {
return;
}
this.setState({ showSnackBar: false });
}
showConfirm(message: string): Promise<boolean> {
return new Promise((res, _rej) => { return new Promise((res, _rej) => {
this.setState({ this.setState({
showConfirm: true, showConfirm: true,
confirmMessage: message, confirmMessage: message,
resolveConfirm: res resolveConfirm: res,
}); });
}); });
} }
handleCloseConfirm(accept: boolean) { handleCloseConfirm(accept: boolean) {
this.setState({ this.setState({
showConfirm: false showConfirm: false,
}); });
this.state.resolveConfirm && this.state.resolveConfirm(accept); this.state.resolveConfirm && this.state.resolveConfirm(accept);
} }
showInput(options: TextInputOptions) : Promise<string> { showInput(options: TextInputOptions): Promise<string> {
return new Promise((res, _rej) => { return new Promise((res, _rej) => {
this.setState({ this.setState({
showInputDialog: true, showInputDialog: true,
inputOptions: options, inputOptions: options,
resolveInput: res, resolveInput: res,
inputValue: options.initialValue || "" inputValue: options.initialValue || "",
}); });
}); });
} }
handleInputValueChanged(e: React.ChangeEvent<HTMLInputElement>){ handleInputValueChanged(e: React.ChangeEvent<HTMLInputElement>) {
this.setState({inputValue: e.target.value}); this.setState({ inputValue: e.target.value });
} }
handleCloseInput(cancel: boolean) { handleCloseInput(cancel: boolean) {
this.setState({ this.setState({
showInputDialog: false showInputDialog: false,
}); });
if (!cancel) if (!cancel)
this.state.resolveInput && this.state.resolveInput(this.state.inputValue); this.state.resolveInput &&
this.state.resolveInput(this.state.inputValue);
} }
get isInputValid() : boolean { get isInputValid(): boolean {
const options = this.state.inputOptions; const options = this.state.inputOptions;
const value = this.state.inputValue; const value = this.state.inputValue;
if (options.minLength && value.length < options.minLength) if (options.minLength && value.length < options.minLength) return false;
return false;
if (options.maxLength && value.length > options.maxLength) if (options.maxLength && value.length > options.maxLength) return false;
return false;
return true; return true;
} }
@ -145,11 +180,10 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
render() { render() {
cache = this; cache = this;
if(this.state == null) if (this.state == null) return <div></div>;
return(<div></div>);
return (<div>
return (
<div>
{/* Simple alert dialog */} {/* Simple alert dialog */}
<Dialog <Dialog
open={this.state.showAlert} open={this.state.showAlert}
@ -157,7 +191,12 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
aria-labelledby="alert-dialog-title" aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description" aria-describedby="alert-dialog-description"
> >
<DialogTitle id="alert-dialog-title" style={{minWidth: "300px"}}>Message</DialogTitle> <DialogTitle
id="alert-dialog-title"
style={{ minWidth: "300px" }}
>
Message
</DialogTitle>
<DialogContent> <DialogContent>
<DialogContentText id="alert-dialog-description"> <DialogContentText id="alert-dialog-description">
{this.state.alertMessage} {this.state.alertMessage}
@ -170,6 +209,30 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
</DialogActions> </DialogActions>
</Dialog> </Dialog>
{/* Simple snackbar */}
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left",
}}
open={this.state.showSnackBar}
autoHideDuration={6000}
onClose={this.handleCloseSnackbar}
message={this.state.snackBarMessage}
action={
<React.Fragment>
<IconButton
size="small"
aria-label="close"
color="inherit"
onClick={this.handleCloseSnackbar}
>
<Close fontSize="small" />
</IconButton>
</React.Fragment>
}
/>
{/* Confirm dialog */} {/* Confirm dialog */}
<Dialog <Dialog
open={this.state.showConfirm} open={this.state.showConfirm}
@ -177,7 +240,12 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
aria-labelledby="alert-dialog-title" aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description" aria-describedby="alert-dialog-description"
> >
<DialogTitle id="alert-dialog-title" style={{minWidth: "300px"}}>Confirm action</DialogTitle> <DialogTitle
id="alert-dialog-title"
style={{ minWidth: "300px" }}
>
Confirm action
</DialogTitle>
<DialogContent> <DialogContent>
<DialogContentText id="alert-dialog-description"> <DialogContentText id="alert-dialog-description">
{this.state.confirmMessage} {this.state.confirmMessage}
@ -201,31 +269,41 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
aria-describedby="alert-dialog-description" aria-describedby="alert-dialog-description"
> >
<DialogTitle id="alert-dialog-title"> <DialogTitle id="alert-dialog-title">
{this.state.inputOptions.title || this.state.inputOptions.label} {this.state.inputOptions.title ||
this.state.inputOptions.label}
</DialogTitle> </DialogTitle>
<DialogContent> <DialogContent>
{this.state.inputOptions.message ? <DialogContentText id="alert-dialog-description"> {this.state.inputOptions.message ? (
{this.state.inputOptions.message} <br /><br /> <DialogContentText id="alert-dialog-description">
</DialogContentText> : <span></span> } {this.state.inputOptions.message} <br />
<br />
</DialogContentText>
) : (
<span></span>
)}
<TextField <TextField
label={this.state.inputOptions.label} label={this.state.inputOptions.label}
variant="outlined" variant="outlined"
value={this.state.inputValue} value={this.state.inputValue}
onChange={this.handleInputValueChanged} onChange={this.handleInputValueChanged}
/> />
</DialogContent> </DialogContent>
<DialogActions> <DialogActions>
<Button onClick={this.cancelInput} color="default"> <Button onClick={this.cancelInput} color="default">
Cancel Cancel
</Button> </Button>
<Button onClick={this.confirmInput} color="default" disabled={!this.isInputValid}> <Button
onClick={this.confirmInput}
color="default"
disabled={!this.isInputValid}
>
OK OK
</Button> </Button>
</DialogActions> </DialogActions>
</Dialog> </Dialog>
</div>) </div>
);
} }
} }
@ -233,10 +311,14 @@ export function matAlert(msg: string) {
cache.showAlert(msg); cache.showAlert(msg);
} }
export function matConfirm(msg: string) : Promise<boolean> { export function snackbar(msg: string) {
cache.showSnackbar(msg);
}
export function matConfirm(msg: string): Promise<boolean> {
return cache.showConfirm(msg); return cache.showConfirm(msg);
} }
export function input(options: TextInputOptions) : Promise<string> { export function input(options: TextInputOptions): Promise<string> {
return cache.showInput(options); return cache.showInput(options);
} }