Merge branch 'feat/roles' into 'main'
Implemented roles management See merge request stackspin/dashboard!29
This commit is contained in:
commit
87007c95c5
20 changed files with 480 additions and 285 deletions
14
src/App.tsx
14
src/App.tsx
|
@ -1,6 +1,6 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Helmet } from 'react-helmet';
|
import { Helmet } from 'react-helmet';
|
||||||
import { Routes, Route, Navigate } from 'react-router-dom';
|
import { Routes, Route, Navigate, Outlet } from 'react-router-dom';
|
||||||
import { Toaster } from 'react-hot-toast';
|
import { Toaster } from 'react-hot-toast';
|
||||||
|
|
||||||
import { useAuth } from 'src/services/auth';
|
import { useAuth } from 'src/services/auth';
|
||||||
|
@ -10,7 +10,13 @@ import { LoginCallback } from './modules/login/LoginCallback';
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
function App() {
|
function App() {
|
||||||
const { authToken } = useAuth();
|
const { authToken, currentUser, isAdmin } = useAuth();
|
||||||
|
|
||||||
|
const redirectToLogin = !authToken || !currentUser?.app_roles;
|
||||||
|
|
||||||
|
const ProtectedRoute = () => {
|
||||||
|
return isAdmin ? <Outlet /> : <Navigate to="/dashboard" />;
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -26,7 +32,7 @@ function App() {
|
||||||
</Helmet>
|
</Helmet>
|
||||||
|
|
||||||
<div className="app bg-gray-50 min-h-screen flex flex-col">
|
<div className="app bg-gray-50 min-h-screen flex flex-col">
|
||||||
{!authToken ? (
|
{redirectToLogin ? (
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/login" element={<Login />} />
|
<Route path="/login" element={<Login />} />
|
||||||
<Route path="/login-callback" element={<LoginCallback />} />
|
<Route path="/login-callback" element={<LoginCallback />} />
|
||||||
|
@ -36,7 +42,9 @@ function App() {
|
||||||
<Layout>
|
<Layout>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/dashboard" element={<Dashboard />} />
|
<Route path="/dashboard" element={<Dashboard />} />
|
||||||
|
<Route path="/users" element={<ProtectedRoute />}>
|
||||||
<Route path="/users" element={<Users />} />
|
<Route path="/users" element={<Users />} />
|
||||||
|
</Route>
|
||||||
<Route path="*" element={<Navigate to="/dashboard" />} />
|
<Route path="*" element={<Navigate to="/dashboard" />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|
|
@ -8,20 +8,28 @@ import clsx from 'clsx';
|
||||||
import { CurrentUserModal } from './components/CurrentUserModal';
|
import { CurrentUserModal } from './components/CurrentUserModal';
|
||||||
|
|
||||||
const navigation = [
|
const navigation = [
|
||||||
{ name: 'Dashboard', to: '/dashboard' },
|
{ name: 'Dashboard', to: '/dashboard', requiresAdmin: false },
|
||||||
{ name: 'Users', to: '/users' },
|
{ name: 'Users', to: '/users', requiresAdmin: true },
|
||||||
];
|
];
|
||||||
|
|
||||||
function classNames(...classes: any[]) {
|
function classNames(...classes: any[]) {
|
||||||
return classes.filter(Boolean).join(' ');
|
return classes.filter(Boolean).join(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function filterNavigationByDashboardRole(isAdmin: boolean) {
|
||||||
|
if (isAdmin) {
|
||||||
|
return navigation;
|
||||||
|
}
|
||||||
|
|
||||||
|
return navigation.filter((item) => !item.requiresAdmin);
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||||
interface HeaderProps {}
|
interface HeaderProps {}
|
||||||
|
|
||||||
const Header: React.FC<HeaderProps> = () => {
|
const Header: React.FC<HeaderProps> = () => {
|
||||||
const [currentUserModal, setCurrentUserModal] = useState(false);
|
const [currentUserModal, setCurrentUserModal] = useState(false);
|
||||||
const { logOut, currentUser } = useAuth();
|
const { logOut, currentUser, isAdmin } = useAuth();
|
||||||
|
|
||||||
const { pathname } = useLocation();
|
const { pathname } = useLocation();
|
||||||
|
|
||||||
|
@ -30,6 +38,8 @@ const Header: React.FC<HeaderProps> = () => {
|
||||||
};
|
};
|
||||||
const currentUserModalClose = () => setCurrentUserModal(false);
|
const currentUserModalClose = () => setCurrentUserModal(false);
|
||||||
|
|
||||||
|
const navigationItems = filterNavigationByDashboardRole(isAdmin);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Disclosure as="nav" className="bg-white shadow relative z-10">
|
<Disclosure as="nav" className="bg-white shadow relative z-10">
|
||||||
|
@ -55,7 +65,7 @@ const Header: React.FC<HeaderProps> = () => {
|
||||||
</Link>
|
</Link>
|
||||||
<div className="hidden sm:ml-6 sm:flex sm:space-x-8">
|
<div className="hidden sm:ml-6 sm:flex sm:space-x-8">
|
||||||
{/* Current: "border-primary-500 text-gray-900", Default: "border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700" */}
|
{/* Current: "border-primary-500 text-gray-900", Default: "border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700" */}
|
||||||
{navigation.map((item) => (
|
{navigationItems.map((item) => (
|
||||||
<Link
|
<Link
|
||||||
key={item.name}
|
key={item.name}
|
||||||
to={item.to}
|
to={item.to}
|
||||||
|
|
|
@ -1,31 +1,33 @@
|
||||||
|
import _ from 'lodash';
|
||||||
import React, { useEffect } from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useFieldArray, useForm } from 'react-hook-form';
|
||||||
import { Modal, Banner } from 'src/components';
|
import { Modal } from 'src/components';
|
||||||
import { Input, Select } from 'src/components/Form';
|
import { Input, Select } from 'src/components/Form';
|
||||||
|
import { useAuth } from 'src/services/auth';
|
||||||
import { User, UserRole, useUsers } from 'src/services/users';
|
import { User, UserRole, useUsers } from 'src/services/users';
|
||||||
import { appAccessList } from './consts';
|
import { appAccessList, initialUserForm } from './consts';
|
||||||
import { UserModalProps } from './types';
|
import { UserModalProps } from './types';
|
||||||
|
|
||||||
export const CurrentUserModal = ({ open, onClose, user }: UserModalProps) => {
|
export const CurrentUserModal = ({ open, onClose, user }: UserModalProps) => {
|
||||||
const { editUserById, userModalLoading } = useUsers();
|
const { editUserById, userModalLoading } = useUsers();
|
||||||
|
const { isAdmin } = useAuth();
|
||||||
|
|
||||||
const { control, reset, handleSubmit } = useForm<User>({
|
const { control, reset, handleSubmit } = useForm<User>({
|
||||||
defaultValues: {
|
defaultValues: initialUserForm,
|
||||||
name: null,
|
});
|
||||||
email: null,
|
|
||||||
id: null,
|
const { fields } = useFieldArray({
|
||||||
role_id: null,
|
control,
|
||||||
status: null,
|
name: 'app_roles',
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (user) {
|
if (user && !_.isEmpty(user)) {
|
||||||
reset(user);
|
reset(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
reset({ name: null, email: null, id: null });
|
reset(initialUserForm);
|
||||||
};
|
};
|
||||||
}, [user, reset]);
|
}, [user, reset]);
|
||||||
|
|
||||||
|
@ -54,7 +56,7 @@ export const CurrentUserModal = ({ open, onClose, user }: UserModalProps) => {
|
||||||
return (
|
return (
|
||||||
<Modal onClose={handleClose} open={open} onSave={handleSave} isLoading={userModalLoading} useCancelButton>
|
<Modal onClose={handleClose} open={open} onSave={handleSave} isLoading={userModalLoading} useCancelButton>
|
||||||
<div className="bg-white px-4">
|
<div className="bg-white px-4">
|
||||||
<div className="space-y-4 divide-y divide-gray-200">
|
<div className="space-y-10 divide-y divide-gray-200">
|
||||||
<div>
|
<div>
|
||||||
<div>
|
<div>
|
||||||
<h3 className="text-lg leading-6 font-medium text-gray-900">Profile</h3>
|
<h3 className="text-lg leading-6 font-medium text-gray-900">Profile</h3>
|
||||||
|
@ -69,20 +71,23 @@ export const CurrentUserModal = ({ open, onClose, user }: UserModalProps) => {
|
||||||
<Input control={control} name="email" label="Email" type="email" onKeyPress={handleKeyPress} />
|
<Input control={control} name="email" label="Email" type="email" onKeyPress={handleKeyPress} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="sm:col-span-6">
|
{isAdmin && (
|
||||||
|
<>
|
||||||
|
<div className="sm:col-span-3">
|
||||||
|
{fields
|
||||||
|
.filter((field) => field.name === 'dashboard')
|
||||||
|
.map((item, index) => (
|
||||||
<Select
|
<Select
|
||||||
|
key={item.id}
|
||||||
control={control}
|
control={control}
|
||||||
name="role_id"
|
name={`app_roles.${index}.role`}
|
||||||
label="Role"
|
label="Role"
|
||||||
options={[
|
options={[
|
||||||
{ value: UserRole.Admin, name: 'Admin' },
|
{ value: UserRole.Admin, name: 'Admin' },
|
||||||
{ value: UserRole.User, name: 'User' },
|
{ value: UserRole.User, name: 'User' },
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
</div>
|
))}
|
||||||
|
|
||||||
<div className="sm:col-span-6">
|
|
||||||
<Banner title="Editing user status and app access coming soon." titleSm="Comming soon!" />
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="sm:col-span-3 opacity-40 cursor-default pointer-events-none select-none">
|
<div className="sm:col-span-3 opacity-40 cursor-default pointer-events-none select-none">
|
||||||
|
@ -102,44 +107,54 @@ export const CurrentUserModal = ({ open, onClose, user }: UserModalProps) => {
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="opacity-40 cursor-default pointer-events-none select-none">
|
{isAdmin && (
|
||||||
<div className="mt-4">
|
<div>
|
||||||
|
<div className="mt-8">
|
||||||
<h3 className="text-lg leading-6 font-medium text-gray-900">App Access</h3>
|
<h3 className="text-lg leading-6 font-medium text-gray-900">App Access</h3>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<div className="flow-root mt-6">
|
<div className="flow-root mt-6">
|
||||||
<ul className="-my-5 divide-y divide-gray-200 ">
|
<ul className="-my-5 divide-y divide-gray-200 ">
|
||||||
{appAccessList.map((app: any) => {
|
{fields
|
||||||
return (
|
.filter((field) => field.name !== 'dashboard')
|
||||||
<li className="py-4" key={app.name}>
|
.map((item, index) => (
|
||||||
|
<li className="py-4" key={item.name}>
|
||||||
<div className="flex items-center space-x-4">
|
<div className="flex items-center space-x-4">
|
||||||
<div className="flex-shrink-0 flex-1 flex items-center">
|
<div className="flex-shrink-0 flex-1 flex items-center">
|
||||||
<img className="h-10 w-10 rounded-md overflow-hidden" src={app.image} alt={app.name} />
|
<img
|
||||||
<h3 className="ml-4 text-md leading-6 font-medium text-gray-900">{app.name}</h3>
|
className="h-10 w-10 rounded-md overflow-hidden"
|
||||||
|
src={_.find(appAccessList, ['name', item.name!])?.image}
|
||||||
|
alt={item.name ?? 'Image'}
|
||||||
|
/>
|
||||||
|
<h3 className="ml-4 text-md leading-6 font-medium text-gray-900">
|
||||||
|
{_.find(appAccessList, ['name', item.name!])?.label}
|
||||||
|
</h3>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<select
|
<Select
|
||||||
id={app.name}
|
key={item.id}
|
||||||
name={app.name}
|
control={control}
|
||||||
className="shadow-sm focus:ring-primary-500 focus:border-primary-500 block w-full sm:text-sm border-gray-300 rounded-md"
|
name={`app_roles.${index}.role`}
|
||||||
>
|
options={[
|
||||||
<option>User</option>
|
{ value: UserRole.Admin, name: 'Admin' },
|
||||||
<option>Admin</option>
|
{ value: UserRole.User, name: 'User' },
|
||||||
<option>Super Admin</option>
|
]}
|
||||||
</select>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
);
|
))}
|
||||||
})}
|
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
|
@ -1,18 +1,55 @@
|
||||||
|
import { UserRole } from 'src/services/users';
|
||||||
|
|
||||||
export const appAccessList = [
|
export const appAccessList = [
|
||||||
{
|
{
|
||||||
|
name: 'wekan',
|
||||||
image: '/assets/wekan.svg',
|
image: '/assets/wekan.svg',
|
||||||
name: 'Wekan',
|
label: 'Wekan',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: 'wordpress',
|
||||||
image: '/assets/wordpress.svg',
|
image: '/assets/wordpress.svg',
|
||||||
name: 'Wordpress',
|
label: 'Wordpress',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: 'next-cloud',
|
||||||
image: '/assets/nextcloud.svg',
|
image: '/assets/nextcloud.svg',
|
||||||
name: 'NextCloud',
|
label: 'NextCloud',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: 'zulip',
|
||||||
image: '/assets/zulip.svg',
|
image: '/assets/zulip.svg',
|
||||||
name: 'Zulip',
|
label: 'Zulip',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const initialAppRoles = [
|
||||||
|
{
|
||||||
|
name: 'dashboard',
|
||||||
|
role: UserRole.User,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'wekan',
|
||||||
|
role: UserRole.User,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'wordpress',
|
||||||
|
role: UserRole.User,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'next-cloud',
|
||||||
|
role: UserRole.User,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'zulip',
|
||||||
|
role: UserRole.User,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export const initialUserForm = {
|
||||||
|
id: '',
|
||||||
|
name: '',
|
||||||
|
email: '',
|
||||||
|
app_roles: initialAppRoles,
|
||||||
|
status: '',
|
||||||
|
};
|
||||||
|
|
|
@ -3,5 +3,5 @@ import { User } from 'src/services/users';
|
||||||
export type UserModalProps = {
|
export type UserModalProps = {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
user: User;
|
user: User | null;
|
||||||
};
|
};
|
||||||
|
|
|
@ -148,6 +148,7 @@ export const Table = <T extends Record<string, unknown>>({
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
) : (
|
) : (
|
||||||
|
<tr>
|
||||||
<td colSpan={4} className="py-24">
|
<td colSpan={4} className="py-24">
|
||||||
<div className="flex flex-col justify-center items-center">
|
<div className="flex flex-col justify-center items-center">
|
||||||
<div className="flex justify-center items-center border border-transparent text-base font-medium rounded-md text-white transition ease-in-out duration-150">
|
<div className="flex justify-center items-center border border-transparent text-base font-medium rounded-md text-white transition ease-in-out duration-150">
|
||||||
|
@ -168,6 +169,7 @@ export const Table = <T extends Record<string, unknown>>({
|
||||||
<p className="text-sm text-primary-600 mt-2">Loading users</p>
|
<p className="text-sm text-primary-600 mt-2">Loading users</p>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
|
</tr>
|
||||||
)}
|
)}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { CogIcon, TrashIcon } from '@heroicons/react/outline';
|
||||||
import { useUsers } from 'src/services/users';
|
import { useUsers } from 'src/services/users';
|
||||||
import { Table } from 'src/components';
|
import { Table } from 'src/components';
|
||||||
import { debounce } from 'lodash';
|
import { debounce } from 'lodash';
|
||||||
|
import { useAuth } from 'src/services/auth';
|
||||||
import { UserModal } from './components/UserModal';
|
import { UserModal } from './components/UserModal';
|
||||||
|
|
||||||
export const Users: React.FC = () => {
|
export const Users: React.FC = () => {
|
||||||
|
@ -13,6 +14,7 @@ export const Users: React.FC = () => {
|
||||||
const [userId, setUserId] = useState(null);
|
const [userId, setUserId] = useState(null);
|
||||||
const [search, setSearch] = useState('');
|
const [search, setSearch] = useState('');
|
||||||
const { users, loadUsers, userTableLoading } = useUsers();
|
const { users, loadUsers, userTableLoading } = useUsers();
|
||||||
|
const { isAdmin } = useAuth();
|
||||||
|
|
||||||
const handleSearch = (event: any) => {
|
const handleSearch = (event: any) => {
|
||||||
setSearch(event.target.value);
|
setSearch(event.target.value);
|
||||||
|
@ -60,6 +62,7 @@ export const Users: React.FC = () => {
|
||||||
Cell: (props: any) => {
|
Cell: (props: any) => {
|
||||||
const { row } = props;
|
const { row } = props;
|
||||||
|
|
||||||
|
if (isAdmin) {
|
||||||
return (
|
return (
|
||||||
<div className="text-right lg:opacity-0 group-hover:opacity-100 transition-opacity">
|
<div className="text-right lg:opacity-0 group-hover:opacity-100 transition-opacity">
|
||||||
<button
|
<button
|
||||||
|
@ -72,11 +75,14 @@ export const Users: React.FC = () => {
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
},
|
},
|
||||||
width: 'auto',
|
width: 'auto',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
[],
|
[isAdmin],
|
||||||
);
|
);
|
||||||
|
|
||||||
const selectedRows = useCallback((rows: Record<string, boolean>) => {
|
const selectedRows = useCallback((rows: Record<string, boolean>) => {
|
||||||
|
@ -88,6 +94,8 @@ export const Users: React.FC = () => {
|
||||||
<div className="max-w-7xl mx-auto py-4 px-3 sm:px-6 lg:px-8 h-full flex-grow">
|
<div className="max-w-7xl mx-auto py-4 px-3 sm:px-6 lg:px-8 h-full flex-grow">
|
||||||
<div className="pb-5 mt-6 border-b border-gray-200 sm:flex sm:items-center sm:justify-between">
|
<div className="pb-5 mt-6 border-b border-gray-200 sm:flex sm:items-center sm:justify-between">
|
||||||
<h1 className="text-3xl leading-6 font-bold text-gray-900">Users</h1>
|
<h1 className="text-3xl leading-6 font-bold text-gray-900">Users</h1>
|
||||||
|
|
||||||
|
{isAdmin && (
|
||||||
<div className="mt-3 sm:mt-0 sm:ml-4">
|
<div className="mt-3 sm:mt-0 sm:ml-4">
|
||||||
<button
|
<button
|
||||||
onClick={() => configureModalOpen(null)}
|
onClick={() => configureModalOpen(null)}
|
||||||
|
@ -98,6 +106,7 @@ export const Users: React.FC = () => {
|
||||||
Add new user
|
Add new user
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex justify-between w-100 my-3 items-center mb-5 ">
|
<div className="flex justify-between w-100 my-3 items-center mb-5 ">
|
||||||
|
@ -153,7 +162,7 @@ export const Users: React.FC = () => {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<UserModal open={configureModal} onClose={configureModalClose} userId={userId} />
|
<UserModal open={configureModal} onClose={configureModalClose} userId={userId} setUserId={setUserId} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,26 +1,27 @@
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
|
import _ from 'lodash';
|
||||||
import { TrashIcon } from '@heroicons/react/outline';
|
import { TrashIcon } from '@heroicons/react/outline';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useFieldArray, useForm } from 'react-hook-form';
|
||||||
import { Modal, Banner, ConfirmationModal } from 'src/components';
|
import { Modal, ConfirmationModal } from 'src/components';
|
||||||
import { Input, Select } from 'src/components/Form';
|
import { Input, Select } from 'src/components/Form';
|
||||||
import { User, UserRole, useUsers } from 'src/services/users';
|
import { User, UserRole, useUsers } from 'src/services/users';
|
||||||
import { useAuth } from 'src/services/auth';
|
import { useAuth } from 'src/services/auth';
|
||||||
import { appAccessList } from './consts';
|
import { appAccessList, initialUserForm } from './consts';
|
||||||
import { UserModalProps } from './types';
|
import { UserModalProps } from './types';
|
||||||
|
|
||||||
export const UserModal = ({ open, onClose, userId }: UserModalProps) => {
|
export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) => {
|
||||||
const [deleteModal, setDeleteModal] = useState(false);
|
const [deleteModal, setDeleteModal] = useState(false);
|
||||||
const { user, loadUser, editUserById, createNewUser, userModalLoading, deleteUserById } = useUsers();
|
const { user, loadUser, editUserById, createNewUser, userModalLoading, deleteUserById, clearSelectedUser } =
|
||||||
const { currentUser } = useAuth();
|
useUsers();
|
||||||
|
const { currentUser, isAdmin } = useAuth();
|
||||||
|
|
||||||
const { control, reset, handleSubmit } = useForm<User>({
|
const { control, reset, handleSubmit } = useForm<User>({
|
||||||
defaultValues: {
|
defaultValues: initialUserForm,
|
||||||
name: null,
|
});
|
||||||
email: null,
|
|
||||||
id: null,
|
const { fields } = useFieldArray({
|
||||||
role_id: null,
|
control,
|
||||||
status: null,
|
name: 'app_roles',
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -28,19 +29,19 @@ export const UserModal = ({ open, onClose, userId }: UserModalProps) => {
|
||||||
loadUser(userId);
|
loadUser(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
reset({ name: null, email: null, id: null, status: null });
|
reset(initialUserForm);
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [userId]);
|
}, [userId, open]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (user) {
|
if (!_.isEmpty(user)) {
|
||||||
reset(user);
|
reset(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
reset({ name: null, email: null, id: null, status: null });
|
reset(initialUserForm);
|
||||||
};
|
};
|
||||||
}, [user, reset]);
|
}, [user, reset, open]);
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
try {
|
try {
|
||||||
|
@ -48,13 +49,14 @@ export const UserModal = ({ open, onClose, userId }: UserModalProps) => {
|
||||||
await handleSubmit((data) => editUserById(data))();
|
await handleSubmit((data) => editUserById(data))();
|
||||||
} else {
|
} else {
|
||||||
await handleSubmit((data) => createNewUser(data))();
|
await handleSubmit((data) => createNewUser(data))();
|
||||||
reset({ name: null, email: null, id: null, status: null });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onClose();
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
// Continue
|
// Continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onClose();
|
||||||
|
clearSelectedUser();
|
||||||
|
setUserId(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleKeyPress = (e: any) => {
|
const handleKeyPress = (e: any) => {
|
||||||
|
@ -65,6 +67,8 @@ export const UserModal = ({ open, onClose, userId }: UserModalProps) => {
|
||||||
|
|
||||||
const handleClose = () => {
|
const handleClose = () => {
|
||||||
onClose();
|
onClose();
|
||||||
|
clearSelectedUser();
|
||||||
|
setUserId(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteModalOpen = () => setDeleteModal(true);
|
const deleteModalOpen = () => setDeleteModal(true);
|
||||||
|
@ -74,6 +78,9 @@ export const UserModal = ({ open, onClose, userId }: UserModalProps) => {
|
||||||
if (userId) {
|
if (userId) {
|
||||||
deleteUserById(userId);
|
deleteUserById(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clearSelectedUser();
|
||||||
|
setUserId(null);
|
||||||
handleClose();
|
handleClose();
|
||||||
deleteModalClose();
|
deleteModalClose();
|
||||||
};
|
};
|
||||||
|
@ -87,7 +94,7 @@ export const UserModal = ({ open, onClose, userId }: UserModalProps) => {
|
||||||
isLoading={userModalLoading}
|
isLoading={userModalLoading}
|
||||||
leftActions={
|
leftActions={
|
||||||
userId &&
|
userId &&
|
||||||
user.email !== currentUser.email && (
|
user.email !== currentUser?.email && (
|
||||||
<button
|
<button
|
||||||
onClick={deleteModalOpen}
|
onClick={deleteModalOpen}
|
||||||
type="button"
|
type="button"
|
||||||
|
@ -101,7 +108,7 @@ export const UserModal = ({ open, onClose, userId }: UserModalProps) => {
|
||||||
useCancelButton
|
useCancelButton
|
||||||
>
|
>
|
||||||
<div className="bg-white px-4">
|
<div className="bg-white px-4">
|
||||||
<div className="space-y-4 divide-y divide-gray-200">
|
<div className="space-y-10 divide-y divide-gray-200">
|
||||||
<div>
|
<div>
|
||||||
<div>
|
<div>
|
||||||
<h3 className="text-lg leading-6 font-medium text-gray-900">{userId ? 'Edit user' : 'Add new user'}</h3>
|
<h3 className="text-lg leading-6 font-medium text-gray-900">{userId ? 'Edit user' : 'Add new user'}</h3>
|
||||||
|
@ -116,24 +123,25 @@ export const UserModal = ({ open, onClose, userId }: UserModalProps) => {
|
||||||
<Input control={control} name="email" label="Email" type="email" onKeyPress={handleKeyPress} />
|
<Input control={control} name="email" label="Email" type="email" onKeyPress={handleKeyPress} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="sm:col-span-6">
|
<div className="sm:col-span-3">
|
||||||
|
{fields
|
||||||
|
.filter((field) => field.name === 'dashboard')
|
||||||
|
.map((item, index) => (
|
||||||
<Select
|
<Select
|
||||||
|
key={item.name}
|
||||||
control={control}
|
control={control}
|
||||||
name="role_id"
|
name={`app_roles.${index}.role`}
|
||||||
label="Role"
|
label="Role"
|
||||||
options={[
|
options={[
|
||||||
{ value: UserRole.Admin, name: 'Admin' },
|
|
||||||
{ value: UserRole.User, name: 'User' },
|
{ value: UserRole.User, name: 'User' },
|
||||||
|
{ value: UserRole.Admin, name: 'Admin' },
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="sm:col-span-6">
|
{isAdmin && (
|
||||||
<Banner title="Editing user status and app access coming soon." titleSm="Comming soon!" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="sm:col-span-3 opacity-40 cursor-default pointer-events-none select-none">
|
<div className="sm:col-span-3 opacity-40 cursor-default pointer-events-none select-none">
|
||||||
{/* <Select control={control} name="status" label="Status" options={['Active', 'Inactive']} /> */}
|
|
||||||
<label htmlFor="status" className="block text-sm font-medium text-gray-700">
|
<label htmlFor="status" className="block text-sm font-medium text-gray-700">
|
||||||
Status
|
Status
|
||||||
</label>
|
</label>
|
||||||
|
@ -149,35 +157,47 @@ export const UserModal = ({ open, onClose, userId }: UserModalProps) => {
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="opacity-40 cursor-default pointer-events-none select-none">
|
{isAdmin && (
|
||||||
<div className="mt-4">
|
<div>
|
||||||
|
<div className="mt-8">
|
||||||
<h3 className="text-lg leading-6 font-medium text-gray-900">App Access</h3>
|
<h3 className="text-lg leading-6 font-medium text-gray-900">App Access</h3>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<div className="flow-root mt-6">
|
<div className="flow-root mt-6">
|
||||||
<ul className="-my-5 divide-y divide-gray-200 ">
|
<ul className="-my-5 divide-y divide-gray-200 ">
|
||||||
{appAccessList.map((app: any) => {
|
{fields.map((item, index) => {
|
||||||
|
if (item.name === 'dashboard') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li className="py-4" key={app.name}>
|
<li className="py-4" key={item.name}>
|
||||||
<div className="flex items-center space-x-4">
|
<div className="flex items-center space-x-4">
|
||||||
<div className="flex-shrink-0 flex-1 flex items-center">
|
<div className="flex-shrink-0 flex-1 flex items-center">
|
||||||
<img className="h-10 w-10 rounded-md overflow-hidden" src={app.image} alt={app.name} />
|
<img
|
||||||
<h3 className="ml-4 text-md leading-6 font-medium text-gray-900">{app.name}</h3>
|
className="h-10 w-10 rounded-md overflow-hidden"
|
||||||
|
src={_.find(appAccessList, ['name', item.name!])?.image}
|
||||||
|
alt={item.name ?? 'Image'}
|
||||||
|
/>
|
||||||
|
<h3 className="ml-4 text-md leading-6 font-medium text-gray-900">
|
||||||
|
{_.find(appAccessList, ['name', item.name!])?.label}
|
||||||
|
</h3>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<select
|
<Select
|
||||||
id={app.name}
|
key={item.id}
|
||||||
name={app.name}
|
control={control}
|
||||||
className="shadow-sm focus:ring-primary-500 focus:border-primary-500 block w-full sm:text-sm border-gray-300 rounded-md"
|
name={`app_roles.${index}.role`}
|
||||||
>
|
options={[
|
||||||
<option>User</option>
|
{ value: UserRole.User, name: 'User' },
|
||||||
<option>Admin</option>
|
{ value: UserRole.Admin, name: 'Admin' },
|
||||||
<option>Super Admin</option>
|
]}
|
||||||
</select>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
|
@ -187,6 +207,7 @@ export const UserModal = ({ open, onClose, userId }: UserModalProps) => {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
|
@ -1,18 +1,55 @@
|
||||||
|
import { UserRole } from 'src/services/users';
|
||||||
|
|
||||||
export const appAccessList = [
|
export const appAccessList = [
|
||||||
{
|
{
|
||||||
|
name: 'wekan',
|
||||||
image: '/assets/wekan.svg',
|
image: '/assets/wekan.svg',
|
||||||
name: 'Wekan',
|
label: 'Wekan',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: 'wordpress',
|
||||||
image: '/assets/wordpress.svg',
|
image: '/assets/wordpress.svg',
|
||||||
name: 'Wordpress',
|
label: 'Wordpress',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: 'nextcloud',
|
||||||
image: '/assets/nextcloud.svg',
|
image: '/assets/nextcloud.svg',
|
||||||
name: 'NextCloud',
|
label: 'NextCloud',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: 'zulip',
|
||||||
image: '/assets/zulip.svg',
|
image: '/assets/zulip.svg',
|
||||||
name: 'Zulip',
|
label: 'Zulip',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const initialAppRoles = [
|
||||||
|
{
|
||||||
|
name: 'dashboard',
|
||||||
|
role: UserRole.User,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'wekan',
|
||||||
|
role: UserRole.User,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'wordpress',
|
||||||
|
role: UserRole.User,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'nextcloud',
|
||||||
|
role: UserRole.User,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'zulip',
|
||||||
|
role: UserRole.User,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export const initialUserForm = {
|
||||||
|
id: '',
|
||||||
|
name: '',
|
||||||
|
email: '',
|
||||||
|
app_roles: initialAppRoles,
|
||||||
|
status: '',
|
||||||
|
};
|
||||||
|
|
|
@ -2,4 +2,5 @@ export type UserModalProps = {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
userId: string | null;
|
userId: string | null;
|
||||||
|
setUserId: any;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { useDispatch, useSelector } from 'react-redux';
|
import { useDispatch, useSelector } from 'react-redux';
|
||||||
import { getAuthToken, getCurrentUser, signIn, signOut } from '../redux';
|
import { getAuthToken, getCurrentUser, getIsAdmin, signIn, signOut } from '../redux';
|
||||||
|
|
||||||
export function useAuth() {
|
export function useAuth() {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const currentUser = useSelector(getCurrentUser);
|
const currentUser = useSelector(getCurrentUser);
|
||||||
const authToken = useSelector(getAuthToken);
|
const authToken = useSelector(getAuthToken);
|
||||||
|
const isAdmin = useSelector(getIsAdmin);
|
||||||
|
|
||||||
const logIn = useCallback(
|
const logIn = useCallback(
|
||||||
(params) => {
|
(params) => {
|
||||||
|
@ -21,6 +22,7 @@ export function useAuth() {
|
||||||
return {
|
return {
|
||||||
authToken,
|
authToken,
|
||||||
currentUser,
|
currentUser,
|
||||||
|
isAdmin,
|
||||||
logIn,
|
logIn,
|
||||||
logOut,
|
logOut,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
export { signIn, signOut, AuthActionTypes } from './actions';
|
export { signIn, signOut, AuthActionTypes } from './actions';
|
||||||
export { default as reducer } from './reducers';
|
export { default as reducer } from './reducers';
|
||||||
export { getAuth, getIsAuthLoading, getAuthToken, getCurrentUser } from './selectors';
|
export { getAuth, getIsAuthLoading, getAuthToken, getCurrentUser, getIsAdmin } from './selectors';
|
||||||
export * from './types';
|
export * from './types';
|
||||||
|
|
|
@ -6,12 +6,12 @@ import { AuthActionTypes } from './actions';
|
||||||
import { transformAuthUser } from '../transformations';
|
import { transformAuthUser } from '../transformations';
|
||||||
|
|
||||||
const initialCurrentUserState: User = {
|
const initialCurrentUserState: User = {
|
||||||
email: null,
|
email: '',
|
||||||
name: null,
|
name: '',
|
||||||
id: null,
|
id: '',
|
||||||
role_id: null,
|
app_roles: [],
|
||||||
status: null,
|
status: '',
|
||||||
preferredUsername: null,
|
preferredUsername: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
const initialState: AuthState = {
|
const initialState: AuthState = {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { State } from 'src/redux';
|
import { State } from 'src/redux';
|
||||||
|
|
||||||
import { isLoading } from 'src/services/api';
|
import { isLoading } from 'src/services/api';
|
||||||
|
import { UserRole } from 'src/services/users';
|
||||||
|
|
||||||
export const getAuth = (state: State) => state.auth;
|
export const getAuth = (state: State) => state.auth;
|
||||||
|
|
||||||
|
@ -8,6 +9,20 @@ export const getAuthToken = (state: State) => state.auth.token;
|
||||||
|
|
||||||
export const getCurrentUser = (state: State) => state.auth.userInfo;
|
export const getCurrentUser = (state: State) => state.auth.userInfo;
|
||||||
|
|
||||||
|
export const getIsAdmin = (state: State) => {
|
||||||
|
// check since old users wont have this
|
||||||
|
if (state.auth.userInfo) {
|
||||||
|
if (!state.auth.userInfo.app_roles) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const isAdmin = state.auth.userInfo.app_roles.find(
|
||||||
|
(role) => role.name === 'dashboard' && role.role === UserRole.Admin,
|
||||||
|
);
|
||||||
|
return !!isAdmin;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
export const getIsAuthLoading = (state: State) => isLoading(getAuth(state));
|
export const getIsAuthLoading = (state: State) => isLoading(getAuth(state));
|
||||||
|
|
||||||
export const getToken = (state: State) => state.auth.token;
|
export const getToken = (state: State) => state.auth.token;
|
||||||
|
|
|
@ -1,18 +1,21 @@
|
||||||
import { UserRole } from '../users';
|
import { User } from '../users';
|
||||||
import { Auth } from './types';
|
import { Auth } from './types';
|
||||||
|
import { transformAppRoles } from '../users/transformations';
|
||||||
|
|
||||||
|
const transformUser = (response: any): User => {
|
||||||
|
return {
|
||||||
|
id: response.id ?? '',
|
||||||
|
app_roles: response.app_roles ? response.app_roles.map(transformAppRoles) : [],
|
||||||
|
email: response.email ?? '',
|
||||||
|
name: response.name ?? '',
|
||||||
|
preferredUsername: response.preferredUsername ?? '',
|
||||||
|
status: response.state ?? '',
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const transformAuthUser = (response: any): Auth => {
|
export const transformAuthUser = (response: any): Auth => {
|
||||||
const resolvedUserRole = !response.userInfo.role_id ? UserRole.User : response.userInfo.role_id;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
token: response.accessToken,
|
token: response.accessToken,
|
||||||
userInfo: {
|
userInfo: response.userInfo ? transformUser(response.userInfo) : null,
|
||||||
id: response.userInfo.id,
|
|
||||||
role_id: resolvedUserRole,
|
|
||||||
email: response.userInfo.email ?? null,
|
|
||||||
name: response.userInfo.name ?? null,
|
|
||||||
preferredUsername: response.userInfo.preferredUsername,
|
|
||||||
status: response.userInfo.state ?? null,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,5 +2,5 @@ import { User } from '../users';
|
||||||
|
|
||||||
export interface Auth {
|
export interface Auth {
|
||||||
token: string | null;
|
token: string | null;
|
||||||
userInfo: User;
|
userInfo: User | null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,13 @@
|
||||||
import { useDispatch, useSelector } from 'react-redux';
|
import { useDispatch, useSelector } from 'react-redux';
|
||||||
import { getUsers, fetchUsers, fetchUserById, updateUserById, createUser, deleteUser } from '../redux';
|
import {
|
||||||
|
getUsers,
|
||||||
|
fetchUsers,
|
||||||
|
fetchUserById,
|
||||||
|
updateUserById,
|
||||||
|
createUser,
|
||||||
|
deleteUser,
|
||||||
|
clearCurrentUser,
|
||||||
|
} from '../redux';
|
||||||
import { getUserById, getUserModalLoading, getUserslLoading } from '../redux/selectors';
|
import { getUserById, getUserModalLoading, getUserslLoading } from '../redux/selectors';
|
||||||
|
|
||||||
export function useUsers() {
|
export function useUsers() {
|
||||||
|
@ -17,6 +25,10 @@ export function useUsers() {
|
||||||
return dispatch(fetchUserById(id));
|
return dispatch(fetchUserById(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clearSelectedUser() {
|
||||||
|
return dispatch(clearCurrentUser());
|
||||||
|
}
|
||||||
|
|
||||||
function editUserById(data: any) {
|
function editUserById(data: any) {
|
||||||
return dispatch(updateUserById(data));
|
return dispatch(updateUserById(data));
|
||||||
}
|
}
|
||||||
|
@ -39,5 +51,6 @@ export function useUsers() {
|
||||||
userTableLoading,
|
userTableLoading,
|
||||||
createNewUser,
|
createNewUser,
|
||||||
deleteUserById,
|
deleteUserById,
|
||||||
|
clearSelectedUser,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Dispatch } from 'redux';
|
import { Dispatch } from 'redux';
|
||||||
import { showToast, ToastType } from 'src/common/util/show-toast';
|
import { showToast, ToastType } from 'src/common/util/show-toast';
|
||||||
|
import { State } from 'src/redux/types';
|
||||||
import { performApiCall } from 'src/services/api';
|
import { performApiCall } from 'src/services/api';
|
||||||
import { AuthActionTypes } from 'src/services/auth';
|
import { AuthActionTypes } from 'src/services/auth';
|
||||||
import { transformRequestUser, transformUser } from '../transformations';
|
import { transformRequestUser, transformUser } from '../transformations';
|
||||||
|
@ -68,9 +69,11 @@ export const fetchUserById = (id: string) => async (dispatch: Dispatch<any>) =>
|
||||||
dispatch(setUserModalLoading(false));
|
dispatch(setUserModalLoading(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateUserById = (user: any) => async (dispatch: Dispatch<any>) => {
|
export const updateUserById = (user: any) => async (dispatch: Dispatch<any>, getState: any) => {
|
||||||
dispatch(setUserModalLoading(true));
|
dispatch(setUserModalLoading(true));
|
||||||
|
|
||||||
|
const state: State = getState();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { data } = await performApiCall({
|
const { data } = await performApiCall({
|
||||||
path: `/users/${user.id}`,
|
path: `/users/${user.id}`,
|
||||||
|
@ -83,10 +86,12 @@ export const updateUserById = (user: any) => async (dispatch: Dispatch<any>) =>
|
||||||
payload: transformUser(data),
|
payload: transformUser(data),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (state.auth.userInfo?.id === user.id) {
|
||||||
dispatch({
|
dispatch({
|
||||||
type: AuthActionTypes.UPDATE_AUTH_USER,
|
type: AuthActionTypes.UPDATE_AUTH_USER,
|
||||||
payload: transformUser(data),
|
payload: transformUser(data),
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
showToast('User updated successfully.', ToastType.Success);
|
showToast('User updated successfully.', ToastType.Success);
|
||||||
|
|
||||||
|
@ -148,3 +153,10 @@ export const deleteUser = (id: string) => async (dispatch: Dispatch<any>) => {
|
||||||
|
|
||||||
dispatch(setUserModalLoading(false));
|
dispatch(setUserModalLoading(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const clearCurrentUser = () => (dispatch: Dispatch<any>) => {
|
||||||
|
dispatch({
|
||||||
|
type: UserActionTypes.DELETE_USER,
|
||||||
|
payload: {},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
|
@ -1,33 +1,38 @@
|
||||||
import _ from 'lodash';
|
import { AppRoles, User, UserRole } from './types';
|
||||||
|
|
||||||
import { User, UserRole } from './types';
|
export const transformAppRoles = (data: any): AppRoles => {
|
||||||
|
const resolvedAdminRole = data.role_id === 1 ? UserRole.Admin : UserRole.User;
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: data.name ?? '',
|
||||||
|
role: resolvedAdminRole ?? UserRole.User,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const transformRequestAppRoles = (data: AppRoles): any => {
|
||||||
|
const resolvedRequestRole = data.role === UserRole.Admin ? 1 : null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: data.name ?? '',
|
||||||
|
role_id: resolvedRequestRole,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const transformUser = (response: any): User => {
|
export const transformUser = (response: any): User => {
|
||||||
const userResponse = _.get(response, 'user', response);
|
|
||||||
|
|
||||||
const resolvedUserRole = !userResponse.traits.role_id ? UserRole.User : userResponse.traits.role_id;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: userResponse.id,
|
id: response.id ?? '',
|
||||||
role_id: resolvedUserRole,
|
app_roles: response.traits.app_roles ? response.traits.app_roles.map(transformAppRoles) : [],
|
||||||
email: userResponse.traits.email,
|
email: response.traits.email ?? '',
|
||||||
name: userResponse.traits.name ?? null,
|
name: response.traits.name ?? '',
|
||||||
preferredUsername: userResponse.preferredUsername,
|
preferredUsername: response.preferredUsername ?? '',
|
||||||
status: userResponse.state,
|
status: response.state ?? '',
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const transformRequestUser = (data: Pick<User, 'role_id' | 'name' | 'email'>) => {
|
export const transformRequestUser = (data: Pick<User, 'app_roles' | 'name' | 'email'>) => {
|
||||||
if (data.role_id === UserRole.User) {
|
|
||||||
return {
|
return {
|
||||||
email: data.email,
|
app_roles: data.app_roles.map(transformRequestAppRoles),
|
||||||
name: data.name,
|
email: data.email ?? '',
|
||||||
};
|
name: data.name ?? '',
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
role_id: Number(data.role_id),
|
|
||||||
email: data.email,
|
|
||||||
name: data.name,
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
export interface User {
|
export interface User {
|
||||||
id: number | null;
|
id: string;
|
||||||
role_id: UserRole | null;
|
app_roles: AppRoles[];
|
||||||
email: string | null;
|
email: string;
|
||||||
name: string | null;
|
name: string;
|
||||||
preferredUsername: string | null;
|
preferredUsername: string;
|
||||||
status: string | null;
|
status: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FormUser extends User {
|
export interface FormUser extends User {
|
||||||
|
@ -13,8 +13,13 @@ export interface FormUser extends User {
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum UserRole {
|
export enum UserRole {
|
||||||
Admin = '1',
|
Admin = 'admin',
|
||||||
User = '2',
|
User = 'user',
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AppRoles {
|
||||||
|
name: string | null;
|
||||||
|
role: UserRole | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UserApiRequest {
|
export interface UserApiRequest {
|
||||||
|
|
Loading…
Reference in a new issue