2019-11-03 20:20:11 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2019-11-04 01:37:06 +00:00
|
|
|
import { Actions, useStoreActions, useStoreState } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
2020-03-26 04:58:37 +00:00
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import AddSubuserButton from '@/components/server/users/AddSubuserButton';
|
2020-03-27 22:32:33 +00:00
|
|
|
import UserRow from '@/components/server/users/UserRow';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import getServerSubusers from '@/api/server/users/getServerSubusers';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2020-03-29 21:19:17 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2019-11-03 20:20:11 +00:00
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const [ loading, setLoading ] = useState(true);
|
2019-11-03 20:32:07 +00:00
|
|
|
|
2019-11-03 20:20:11 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const subusers = ServerContext.useStoreState(state => state.subusers.data);
|
2020-03-27 22:32:33 +00:00
|
|
|
const setSubusers = ServerContext.useStoreActions(actions => actions.subusers.setSubusers);
|
2019-11-03 20:20:11 +00:00
|
|
|
|
2019-11-04 01:37:06 +00:00
|
|
|
const permissions = useStoreState((state: ApplicationStore) => state.permissions.data);
|
|
|
|
const getPermissions = useStoreActions((actions: Actions<ApplicationStore>) => actions.permissions.getPermissions);
|
2020-03-27 22:32:33 +00:00
|
|
|
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2019-11-04 01:37:06 +00:00
|
|
|
|
2019-11-03 20:20:11 +00:00
|
|
|
useEffect(() => {
|
2020-03-27 22:32:33 +00:00
|
|
|
clearFlashes('users');
|
|
|
|
getServerSubusers(uuid)
|
|
|
|
.then(subusers => {
|
|
|
|
setSubusers(subusers);
|
|
|
|
setLoading(false);
|
|
|
|
})
|
2019-11-03 20:20:11 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2020-03-27 22:32:33 +00:00
|
|
|
addError({ key: 'users', message: httpErrorToHuman(error) });
|
2019-11-03 20:20:11 +00:00
|
|
|
});
|
2020-03-27 22:32:33 +00:00
|
|
|
}, []);
|
2019-11-03 20:20:11 +00:00
|
|
|
|
2020-04-10 17:46:00 +00:00
|
|
|
useEffect(() => {
|
|
|
|
getPermissions().catch(error => {
|
|
|
|
addError({ key: 'users', message: httpErrorToHuman(error) });
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (!subusers.length && (loading || !Object.keys(permissions).length)) {
|
2020-03-26 04:58:37 +00:00
|
|
|
return <Spinner size={'large'} centered={true}/>;
|
|
|
|
}
|
|
|
|
|
2019-11-03 20:20:11 +00:00
|
|
|
return (
|
2020-03-30 05:04:38 +00:00
|
|
|
<div className={'mt-10 mb-6'}>
|
2020-03-27 22:32:33 +00:00
|
|
|
<FlashMessageRender byKey={'users'} className={'mb-4'}/>
|
2020-03-26 04:58:37 +00:00
|
|
|
{!subusers.length ?
|
|
|
|
<p className={'text-center text-sm text-neutral-400'}>
|
|
|
|
It looks like you don't have any subusers.
|
|
|
|
</p>
|
|
|
|
:
|
|
|
|
subusers.map(subuser => (
|
2020-03-27 22:32:33 +00:00
|
|
|
<UserRow key={subuser.uuid} subuser={subuser}/>
|
2020-03-26 04:58:37 +00:00
|
|
|
))
|
2019-11-03 20:32:07 +00:00
|
|
|
}
|
2020-03-29 21:19:17 +00:00
|
|
|
<Can action={'user.create'}>
|
|
|
|
<div className={'flex justify-end mt-6'}>
|
|
|
|
<AddSubuserButton/>
|
|
|
|
</div>
|
|
|
|
</Can>
|
2019-11-03 20:20:11 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|