web-base/react/admin-panel/src/App.jsx

104 lines
3.2 KiB
React
Raw Normal View History

2022-12-01 01:28:38 +01:00
import React, {useCallback, useContext, useEffect, useMemo, useState} from 'react';
2022-11-29 14:17:11 +01:00
import API from "shared/api";
import Icon from "shared/elements/icon";
import LoginForm from "./views/login";
import {Alert} from "@material-ui/lab";
import {Button} from "@material-ui/core";
2022-11-30 23:15:52 +01:00
import { LocaleContext } from "shared/locale";
2022-12-01 01:28:38 +01:00
import AdminDashboard from "./AdminDashboard";
2022-11-30 23:15:52 +01:00
2022-12-01 01:28:38 +01:00
export default function App() {
2022-11-29 14:17:11 +01:00
2022-12-01 01:28:38 +01:00
const api = useMemo(() => new API(), []);
const [user, setUser] = useState(null);
const [loaded, setLoaded] = useState(false);
const [info, setInfo] = useState({});
const [error, setError] = useState(null);
const {translate: L} = useContext(LocaleContext);
2022-11-29 14:17:11 +01:00
2022-12-01 01:28:38 +01:00
const fetchUser = useCallback(() => {
api.fetchUser().then(data => {
if (data.success) {
setUser(data.user || null);
setLoaded(true);
} else {
setError(data.msg);
}
});
}, [api]);
2022-11-29 14:17:11 +01:00
2022-12-01 01:28:38 +01:00
const onInit = useCallback((force = false) => {
if (loaded && !force) {
return;
}
2022-11-29 14:17:11 +01:00
2022-12-01 01:28:38 +01:00
setError(false);
setLoaded(false);
api.getLanguageEntries("general").then(data => {
2022-11-29 14:17:11 +01:00
if (data.success) {
2022-12-01 01:28:38 +01:00
api.info().then(data => {
2022-11-29 14:17:11 +01:00
if (data.success) {
2022-12-01 01:28:38 +01:00
setInfo(data.info);
fetchUser();
2022-11-29 14:17:11 +01:00
} else {
2022-12-01 01:28:38 +01:00
setError(data.msg);
2022-11-29 14:17:11 +01:00
}
});
} else {
2022-12-01 01:28:38 +01:00
setError(data.msg);
2022-11-29 14:17:11 +01:00
}
});
2022-12-01 01:28:38 +01:00
}, [api, loaded, fetchUser]);
2022-11-29 14:17:11 +01:00
2022-12-01 01:28:38 +01:00
useEffect(() => {
onInit();
}, []);
2022-11-29 14:17:11 +01:00
2022-12-01 01:28:38 +01:00
/*
const onTotp2FA = useCallback((code, callback) => {
2022-11-29 14:17:11 +01:00
this.setState({ ...this.state, error: "" });
return this.api.verifyTotp2FA(code).then((res) => {
if (res.success) {
this.api.fetchUser().then(() => {
this.setState({ ...this.state, user: res });
callback(res);
})
} else {
callback(res);
}
});
2022-12-01 01:28:38 +01:00
}, [api]);
2022-11-29 14:17:11 +01:00
onKey2FA(credentialID, clientDataJson, authData, signature, callback) {
this.setState({ ...this.state, error: "" });
return this.api.verifyKey2FA(credentialID, clientDataJson, authData, signature).then((res) => {
if (res.success) {
this.api.fetchUser().then(() => {
this.setState({ ...this.state, user: res });
callback(res);
})
} else {
callback(res);
}
});
}
2022-12-01 01:28:38 +01:00
*/
2022-11-29 14:17:11 +01:00
2022-12-01 01:28:38 +01:00
if (!loaded) {
if (error) {
return <Alert severity={"error"} title={L("general.error_occurred")}>
<div>{error}</div>
<Button type={"button"} variant={"outlined"} onClick={() => onInit(true)}>
Retry
</Button>
</Alert>
} else {
return <b>{L("general.loading")} <Icon icon={"spinner"}/></b>
2022-11-29 14:17:11 +01:00
}
2022-12-01 01:28:38 +01:00
} else if (!user || !api.loggedIn) {
return <LoginForm api={api} info={info} onLogin={fetchUser} />
} else {
return <AdminDashboard api={api} info={info} />
2022-11-29 14:17:11 +01:00
}
}