Skip to content

Commit 85e6cd5

Browse files
committed
fix: Refactor user edit view to have only one
1 parent 79277ee commit 85e6cd5

File tree

10 files changed

+27
-52
lines changed

10 files changed

+27
-52
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
![](https://i.imgur.com/Hc0JsXs.png)
1414

15-
- Copy `.env.sample` to `.env` and adapt the database URL
15+
- Copy `.env.sample` to `.env` and adapt the variables
1616

1717
- Install dependencies: `npm i` (This should also setup your database from the `schema.prisma` file and the migrations)
1818

@@ -32,7 +32,7 @@
3232

3333
*This project should deploy successfully as-is on Heroku*
3434

35-
- Set the DATABASE_URL environment variable
35+
- Set the environment variables
3636

3737
- Install dependencies: `npm ci` (This should also setup your database from the `schema.prisma` file and the migrations)
3838

client/public/static/i18n/en/user.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"newUser": "New User",
3+
"editUser": "Edit User",
24
"informationCanBeEdited": "The information can be edited",
35
"profile": "Profile",
46
"firstName": "First Name",
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
2+
"newUser": "Nouvel utilisateur",
3+
"editUser": "Modification d'un utilisateur",
24
"informationCanBeEdited": "Ces informations peuvent être modifiées",
35
"profile": "Profil",
46
"firstName": "Prénom",
57
"lastName": "Nom",
68
"email": "Email",
79
"phone": "Téléphone",
810
"password": "Mot de passe",
9-
"passwordConfirm": "Confirmer le mot de passe",
11+
"passwordConfirm": "Confirmer le mot de passe",
1012
"giveAdminRights": "Donner les droits d'administrateur",
1113
"newUserAdded": "Nouvel utilisateur ajouté"
1214
}

client/src/api/RecordRoutes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type RecordWithAuthor = Record & {
1010
export type RecordUpdate = Partial<Record> & {
1111
author?: {
1212
create?: Prisma.RecordCreateWithoutAuthorInput;
13+
connect?: Prisma.RecordWhereUniqueInput;
1314
update?: Prisma.RecordUpdateWithoutAuthorInput;
1415
}
1516
};

client/src/api/UserRoutes.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export interface User extends Omit<_User, 'password'> {
99
export type UserUpdate = Partial<_User> & {
1010
person?: {
1111
create?: Prisma.PersonCreateWithoutUserInput;
12+
connect?: Prisma.PersonWhereUniqueInput;
1213
update?: Prisma.PersonUpdateWithoutUserInput;
13-
}
14+
},
1415
};
1516

1617
const UserRoutes = {

client/src/components/forms/UserForm.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const UserForm = ({ data }: Props) => {
6565
}).then(() => {
6666
Alert.open('success', t('common:saved'));
6767
}).catch(catchError(Alert));
68+
Loader.close();
6869
} else { // Addition
6970
processedData.password = formData.password;
7071
await UserRoutes.insert({
@@ -78,8 +79,8 @@ const UserForm = ({ data }: Props) => {
7879
navigate('/app/admin/user/list');
7980
reset();
8081
}).catch(catchError(Alert));
82+
Loader.close();
8183
}
82-
Loader.close();
8384
};
8485

8586
return (

client/src/hooks/useAuth.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface AuthContextInterface {
99
updateData: (data: User) => void,
1010
}
1111

12-
const emptyUser: User = {
12+
export const emptyUser: User = {
1313
id: 0,
1414
login: '',
1515
admin: false,

client/src/routes.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import AdminLayout from './layouts/AdminLayout';
55
import DashboardLayout from './layouts/DashboardLayout/DashboardLayout';
66
import MainLayout from './layouts/MainLayout/MainLayout';
77
import AccountView from './views/account/AccountView/AccountView';
8-
import UserAddView from './views/admin/user/UserAddView';
98
import UserEditView from './views/admin/user/UserEditView';
109
import UserListView from './views/admin/user/UserListView';
1110
import LoginView from './views/auth/LoginView';
@@ -26,7 +25,7 @@ const routes = [
2625
path: 'user',
2726
children: [
2827
{ path: 'list', element: <UserListView /> },
29-
{ path: 'add', element: <UserAddView /> },
28+
{ path: 'add', element: <UserEditView /> },
3029
{ path: 'edit/:id', element: <UserEditView /> },
3130
],
3231
},

client/src/views/admin/user/UserAddView.tsx

-34
This file was deleted.

client/src/views/admin/user/UserEditView.tsx

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
11
import { Card, CardContent, CardHeader, Divider } from '@mui/material';
22
import React, { useMemo } from 'react';
3+
import { useTranslation } from 'react-i18next';
34
import { useParams } from 'react-router-dom';
45
import UserRoutes from '../../../api/UserRoutes';
56
import UserForm from '../../../components/forms/UserForm';
67
import Loader from '../../../components/Loader';
78
import Page from '../../../components/Page';
9+
import { emptyUser } from '../../../hooks/useAuth';
810
import useStateAsync from '../../../hooks/useStateAsync';
911

1012
const UserEditView = () => {
11-
const { id } = useParams();
13+
const { t } = useTranslation('user');
14+
const { id: _id } = useParams();
15+
16+
const id = useMemo(() => (_id ? +_id : 0), [_id]);
1217

1318
const callParams = useMemo(() => ({
14-
id: id ? +id : 0,
19+
id,
1520
include: { person: true },
1621
}), [id]);
1722
const { data: user } = useStateAsync(
18-
null,
23+
emptyUser,
1924
UserRoutes.get,
2025
callParams,
2126
);
2227

2328
return (
24-
<Page
25-
title="New user"
26-
>
29+
<Page title={id ? t('editUser') : t('newUser')}>
2730
<Card>
2831
<CardHeader
29-
subheader="Edit the user information"
30-
title="Edit user"
32+
subheader={t('informationCanBeEdited')}
33+
title={id ? t('editUser') : t('newUser')}
3134
/>
3235
<Divider />
3336
<CardContent>
34-
{!user
37+
{(!!id && !user.id)
3538
? <Loader />
3639
: (
3740
<UserForm
3841
data={{
39-
id: id ? +id : 0,
42+
id: user.id,
4043
admin: user.admin,
4144
login: user.login,
4245
idperson: user.person.id ? +user.person.id : 0,

0 commit comments

Comments
 (0)