aboutsummaryrefslogtreecommitdiff
path: root/src/App.jsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/App.jsx')
-rw-r--r--src/App.jsx31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/App.jsx b/src/App.jsx
index 4fe34f64..b6e864b2 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -6,9 +6,10 @@ import makeStyles from '@mui/styles/makeStyles';
import BottomMenu from './common/components/BottomMenu';
import SocketController from './SocketController';
import CachingController from './CachingController';
-import { useEffectAsync } from './reactHelper';
+import { useCatch, useEffectAsync } from './reactHelper';
import { sessionActions } from './store';
import UpdateController from './UpdateController';
+import TermsDialog from './common/components/TermsDialog';
const useStyles = makeStyles(() => ({
page: {
@@ -29,10 +30,24 @@ const App = () => {
const desktop = useMediaQuery(theme.breakpoints.up('md'));
const newServer = useSelector((state) => state.session.server.newServer);
- const initialized = useSelector((state) => !!state.session.user);
+ const termsUrl = useSelector((state) => state.session.server.attributes.termsUrl);
+ const user = useSelector((state) => state.session.user);
+
+ const acceptTerms = useCatch(async () => {
+ const response = await fetch(`/api/users/${user.id}`, {
+ method: 'PUT',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ ...user, attributes: { ...user.attributes, termsAccepted: true } }),
+ });
+ if (response.ok) {
+ dispatch(sessionActions.updateUser(await response.json()));
+ } else {
+ throw Error(await response.text());
+ }
+ });
useEffectAsync(async () => {
- if (!initialized) {
+ if (!user) {
const response = await fetch('/api/session');
if (response.ok) {
dispatch(sessionActions.updateUser(await response.json()));
@@ -43,9 +58,15 @@ const App = () => {
}
}
return null;
- }, [initialized]);
+ }, [user]);
- return !initialized ? (<LinearProgress />) : (
+ if (user == null) {
+ return (<LinearProgress />);
+ }
+ if (termsUrl && !user.attributes.termsAccepted) {
+ return (<TermsDialog open onCancel={() => navigate('/login')} onAccept={() => acceptTerms()} />);
+ }
+ return (
<>
<SocketController />
<CachingController />