Skip to content

Commit d2e0cc2

Browse files
committed
fix: Refactored user type definition to align with server returns
1 parent 5d42b3b commit d2e0cc2

File tree

15 files changed

+76
-62
lines changed

15 files changed

+76
-62
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"home": "Home",
23
"userList": "User list",
34
"Examples": "Examples",
45
"giveAdminRights": "Give admin rights",

client/public/static/i18n/fr/common.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"home": "Accueil",
23
"userList": "Utilisateurs",
34
"Examples": "Exemples",
45
"giveAdminRights": "Donner les droits admin",

client/src/api/RecordRoutes.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import { Prisma, Record } from '@fullstack-typescript-monorepo/prisma';
2-
import Fetch from '../utils/fetcher';
32
import Super from './Super';
4-
import { User } from './UserRoutes';
3+
import { User, UserWithPerson } from './UserRoutes';
54

65
export type RecordWithAuthor = Record & {
76
author: User;
87
};
8+
export type RecordWithAuthorWithPerson = Record & {
9+
author: UserWithPerson;
10+
};
911

1012
const RecordRoutes = {
11-
...Super<Record, Prisma.RecordUpdateInput>('record'),
12-
list: (object?: string) => Fetch<RecordWithAuthor[]>('/api/record/list', {
13-
object,
14-
}),
13+
...Super<Record, Prisma.RecordInclude, Prisma.RecordWhereInput, Prisma.RecordUpdateInput>('record'),
1514
};
1615

1716
export default RecordRoutes;

client/src/api/RequestRoutes.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { Prisma, Request } from '@fullstack-typescript-monorepo/prisma';
22
import Super from './Super';
33

4-
export type RequestUpdate = Partial<Request>;
5-
64
const RequestRoutes = {
7-
...Super<Request, Prisma.RequestUpdateInput>('request'),
5+
...Super<Request, never, Prisma.RequestWhereInput, Prisma.RequestUpdateInput>('request'),
86
};
97

108
export default RequestRoutes;

client/src/api/Super.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
import { PrismaInclude } from '@fullstack-typescript-monorepo/core';
21
import { TableState } from '../components/Datatable';
32
import Fetch from '../utils/fetcher';
43

5-
export interface getProps {
6-
id: number;
7-
include?: PrismaInclude;
8-
}
9-
10-
const Super = <Model, Setter extends object>(model: string) => ({
4+
const Super = <Model, Include, Where, Setter extends object>(model: string) => ({
115
insert: (data: Setter) => Fetch<Model>(`/api/${model}`, data, 'PUT'),
12-
get: ({ id, include }: getProps) => Fetch<Model>(`/api/${model}/${id}/get`, {
13-
include,
14-
}, 'POST'),
15-
getAll: (include: PrismaInclude) => Fetch<Model[]>(`/api/${model}/all`, {
6+
get: ({ id, include }: { id: number, include?: Include }) => Fetch<Model>(`/api/${model}/${id}/get`, {
167
include,
178
}, 'POST'),
9+
list: (params: { include?: Include, where?: Where }) => Fetch<Model[]>(`/api/${model}/list`, params, 'POST'),
1810
getAllAsCsv: (title: string) => Fetch<Blob>(`/api/${model}/all/csv`, {
1911
title,
2012
}),
2113
table: (
2214
state: TableState,
23-
include?: PrismaInclude,
15+
include?: Include,
2416
) => Fetch<{ data: Model[], count: number }>(`/api/${model}/table`, {
2517
state,
2618
include,
2719
}, 'POST'),
2820
update: (
2921
id: number,
3022
data: Setter,
31-
include?: PrismaInclude
23+
include?: Include
3224
) => Fetch<Model>(`/api/${model}/${id}/update`, {
3325
data,
3426
include,

client/src/api/UserRoutes.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import { Person, Prisma, User as _User } from '@fullstack-typescript-monorepo/prisma';
2+
import { TableState } from '../components/Datatable';
23
import Fetch from '../utils/fetcher';
34
import Super from './Super';
45

5-
export interface User extends Omit<_User, 'password'> {
6+
export type User = Omit<_User, 'password'>;
7+
8+
export type UserWithPerson = User & {
69
person: Person;
7-
}
10+
};
811

912
const UserRoutes = {
10-
...Super<User, Prisma.UserUpdateInput>('user'),
11-
authenticate: (login: string, password: string): Promise<User> => Fetch<User>('/api/user/authenticate', {
13+
...Super<User, Prisma.UserInclude, Prisma.UserWhereInput, Prisma.UserUpdateInput>('user'),
14+
table: (
15+
state: TableState,
16+
include?: Prisma.UserInclude,
17+
) => Fetch<{ data: UserWithPerson[], count: number }>('/api/user/table', {
18+
state,
19+
include,
20+
}, 'POST'),
21+
authenticate: (login: string, password: string) => Fetch<UserWithPerson>('/api/user/authenticate', {
1222
login,
1323
password,
1424
}, 'POST'),

client/src/components/ActionsTable.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Paper, Stack, Table, TableBody, TableCell, TableContainer, TableHead, TableRow } from '@mui/material';
22
import moment from 'moment';
3-
import React, { useEffect } from 'react';
3+
import React, { useEffect, useMemo } from 'react';
44
import { useTranslation } from 'react-i18next';
5-
import RecordRoutes from '../api/RecordRoutes';
5+
import RecordRoutes, { RecordWithAuthorWithPerson } from '../api/RecordRoutes';
66
import useStateAsync from '../hooks/useStateAsync';
77
import Text from './Text';
88

@@ -18,11 +18,15 @@ const ActionsTable = ({
1818
}: ActionsTableProps) => {
1919
const { t } = useTranslation('actions');
2020

21-
const { data: records, reload: reloadRecords } = useStateAsync(
21+
const recordsListParams = useMemo(() => ({
22+
include: { author: { include: { person: true } } },
23+
}), []);
24+
const { data: _records, reload: reloadRecords } = useStateAsync(
2225
[],
2326
RecordRoutes.list,
24-
object,
27+
recordsListParams,
2528
);
29+
const records = _records as RecordWithAuthorWithPerson[];
2630

2731
// Reload table when new record is added and/or when reloadActions is updated
2832
useEffect(() => {

client/src/hooks/useAuth.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import React, { useCallback, useContext, useMemo, useState } from 'react';
2-
import UserRoutes, { User } from '../api/UserRoutes';
2+
import UserRoutes, { UserWithPerson } from '../api/UserRoutes';
33

44
interface AuthContextInterface {
5-
user: User,
5+
user: UserWithPerson,
66
authed: boolean,
7-
signin: (login: string, password: string) => Promise<User | null>,
7+
signin: (login: string, password: string) => Promise<UserWithPerson | null>,
88
signout: () => void,
9-
updateData: (data: User) => void,
9+
updateData: (data: UserWithPerson) => void,
1010
}
1111

12-
export const emptyUser: User = {
12+
export const emptyUser: UserWithPerson = {
1313
id: 0,
1414
login: '',
1515
admin: false,
@@ -49,7 +49,7 @@ interface AuthProviderProps {
4949
}
5050

5151
export const AuthProvider = ({ children }: AuthProviderProps) => {
52-
const [user, setUser] = useState<User>(emptyUser);
52+
const [user, setUser] = useState<UserWithPerson>(emptyUser);
5353
const [authed, setAuthed] = useState(false);
5454

5555
const signin = useCallback((
@@ -70,7 +70,7 @@ export const AuthProvider = ({ children }: AuthProviderProps) => {
7070
setUser(emptyUser);
7171
}, []);
7272

73-
const updateData = useCallback((data: User) => {
73+
const updateData = useCallback((data: UserWithPerson) => {
7474
setUser(data);
7575
}, []);
7676

client/src/views/HomeView.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { Card, CardContent } from '@mui/material';
22
import React from 'react';
3+
import { useTranslation } from 'react-i18next';
34
import Page from '../components/Page';
5+
import Text from '../components/Text';
46

5-
const HomeView = () => (
6-
<Page
7-
title="User list"
8-
>
9-
<Card>
10-
<CardContent>Hello !</CardContent>
11-
</Card>
12-
</Page>
13-
);
7+
const HomeView = () => {
8+
const { t } = useTranslation();
9+
10+
return (
11+
<Page title={t('home')}>
12+
<Card>
13+
<CardContent><Text>Hello !</Text></CardContent>
14+
</Card>
15+
</Page>
16+
);
17+
};
1418

1519
export default HomeView;

client/src/views/account/AccountView/ProfileDetails.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Box, Card, CardContent, CardHeader, Divider, Grid, TextField } from '@m
33
import React from 'react';
44
import { useWatch } from 'react-hook-form';
55
import { useTranslation } from 'react-i18next';
6-
import UserRoutes from '../../../api/UserRoutes';
6+
import UserRoutes, { UserWithPerson } from '../../../api/UserRoutes';
77
import { useAlert } from '../../../hooks/useAlert';
88
import { useAuth } from '../../../hooks/useAuth';
99
import useForm from '../../../hooks/useForm';
@@ -60,7 +60,7 @@ const ProfileDetails = ({ ...rest }) => {
6060
await UserRoutes.update(auth.user.id, {
6161
person: { update: processedData }
6262
}, { person: true }).then((newData) => {
63-
auth.updateData(newData);
63+
auth.updateData(newData as UserWithPerson);
6464
Alert.open('success', t('common:saved'));
6565
}).catch(catchError(Alert));
6666
Loader.close();

0 commit comments

Comments
 (0)