Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-Lyard committed Jul 14, 2024
2 parents 16259c4 + c041eff commit a7868db
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 42 deletions.
15 changes: 6 additions & 9 deletions client/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
async redirects() {
return [
{
source: '/',
destination: '/home',
permanent: true,
},
];
},
images: {
remotePatterns: [
{
Expand All @@ -17,6 +8,12 @@ const nextConfig = {
port:'',
pathname: '/uploads/**',
},
{
protocol: 'http',
hostname: 'localhost',
port:'4000',
pathname: '/uploads/**',
}
],
},
};
Expand Down
10 changes: 10 additions & 0 deletions client/src/app/[lang]/(user)/user/profile/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useErrorHandling } from '@/hooks/useErrorHandling';
import { HttpService } from '@/services';
import { IProfileResponse, IResponse } from '@/types/api';
import { FormEvent, useEffect, useState } from 'react';

export default function Profile({
params: { lang },
}: {
Expand Down Expand Up @@ -90,6 +91,7 @@ export default function Profile({
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-cyan-600 sm:max-w-md sm:text-sm sm:leading-6"
required={true}
defaultValue={username}
onChange={(e) => setUsername(e.target.value)}
/>
</div>
</div>
Expand Down Expand Up @@ -222,6 +224,14 @@ export default function Profile({
</div>
</div>

{errors.length > 0 &&
errors.map((error: string, index) => (
<p key={index} className="text-red-700">
{error}
</p>
))}
{message !== '' && <p className="text-green-700">{message}</p>}

<div className="mt-6 flex items-center justify-end gap-x-6">
<button
type="button"
Expand Down
49 changes: 30 additions & 19 deletions client/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,20 @@ export default function Navbar() {
handleNavChange(pathname);
handleUserLoggedIn();
}, [pathname, searchParams]);
const defaultLogo = 'user.png';
const [avatar, setAvatar] = useState<string>(
`${process.env.NEXT_PUBLIC_UPLOADS_URL}/${defaultLogo}`
);

async function handleUserLoggedIn() {
try {
const response = await http.service().get<IResponse>(`/users/me`);

if (response.data.isConnect === true) {
setIsLoggedIn(true);
setAvatar(
`${process.env.NEXT_PUBLIC_UPLOADS_URL}/${response.data.informations.avatar}`
);
} else {
setIsLoggedIn(false);
}
Expand All @@ -85,28 +92,32 @@ export default function Navbar() {

async function handleUserLogout() {
try {
await http.service().get<IResponse>(`/auth/logout`);
const response = await http.service().get<IResponse>(`/auth/logout`);
if (response.status === 'success') {
location.reload();
}
} catch (e: any) {}
}

async function handleProfile() {
try {
const response = await http
.service()
.get<IProfileResponse>(`/users/profile`);
setAvatar(response.data.user.avatar);
} catch (e: any) {}
}
// async function handleProfile() {
// try {
// const response = await http
// .service()
// .get<IProfileResponse>(`/users/profile`);

useEffect(() => {
handleProfile();
}, []);
// if (!response.data.user.avatar) {
// setAvatar(`${process.env.NEXT_PUBLIC_UPLOADS_URL}/${defaultLogo}`);
// } else {
// setAvatar(
// `${process.env.NEXT_PUBLIC_UPLOADS_URL}/${response.data.user.avatar}`
// );
// }
// } catch (e: any) {}
// }

const [avatar, setAvatar] = useState<string | null>(null);
const defaultLogo = '/relaxing-hippoquests.jpeg';
const avatarUrl = avatar
? `${process.env.NEXT_PUBLIC_UPLOADS_URL}/${avatar}`
: defaultLogo;
// useEffect(() => {
// handleProfile();
// }, []);

return (
<Disclosure as="nav" className="bg-gray-800">
Expand Down Expand Up @@ -197,7 +208,7 @@ export default function Navbar() {
<Image
width={32}
height={32}
src={avatarUrl}
src={avatar}
className="h-8 w-8 rounded-full"
alt=""
/>
Expand Down Expand Up @@ -229,7 +240,7 @@ export default function Navbar() {
<MenuItem key="logout">
{({ active }) => (
<Link
href="/home"
href=""
onClick={() => {
handleUserLogout();
}}
Expand Down
5 changes: 5 additions & 0 deletions client/src/types/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export interface IResponse {
message: string;
data: {
isConnect: boolean;
informations: {
role: string;
username: string;
avatar: string;
};
};
}

Expand Down
3 changes: 1 addition & 2 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ services:
- 4000:4000
volumes:
- ./server/src:/app/src
- prochainweb-images:/app/public/uploads
- ./server/public/uploads:/app/public/uploads

volumes:
prochainweb-database:
prochainweb-images:
Binary file added server/public/uploads/user.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions server/src/middleware/uploadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export const uploadFile = (req: Request, res: Response, next: NextFunction) => {
);
}

if (!req.file) {
/* if (!req.file) {
return next(new AppError(400, 'No file uploaded.'));
}
} */

// req.body.file = req.file.filename; // Store the filename in req.body for later use
next();
Expand Down
5 changes: 5 additions & 0 deletions server/src/types/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Express, Request } from 'express';

import { Multer } from 'multer';

export type File = Express.Multer.File;
1 change: 1 addition & 0 deletions server/src/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface IUser {
export interface IUserInformations {
role: RoleEnumType | null;
username: string;
avatar: string | null;
}
9 changes: 5 additions & 4 deletions server/src/user/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,19 @@ export const updateUserHandler = async (
const user = (await getUserInformations(req, next)) as IUser;

const { username, notification } = req.body;

const fileName = req.file!.filename;
console.log('filename', fileName);
console.log('username', username);
await updateUser({
user,
username,
notification: notification === 'true' ? true : false,
avatar: fileName,
avatar: req.file,
});

const message =
req.language === 'fr' ? 'Profil mis à jour' : 'Profile updated';
res.status(200).json({
status: 'success',
message,
});
} catch (err: any) {
next(err);
Expand Down
2 changes: 1 addition & 1 deletion server/src/user/dto/user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export interface IUserUpdateDto {
id: string;
username: string;
notification: boolean;
avatar: string;
avatar: string | null;
}
1 change: 1 addition & 0 deletions server/src/user/repository/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class UserRepository {
select: {
role: true,
username: true,
avatar: true
},
});
return userinfos;
Expand Down
3 changes: 2 additions & 1 deletion server/src/user/routes/user.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {
updateUserHandler,
} from '../controller/user.controller';
import { uploadFile } from '../../middleware/uploadFile';
import {readLanguage} from "../../middleware/readLanguage";

const router = express.Router();

router.get('/', authenticateUser, getUserHandler);

router.get('/me', getMeHandler);

router.post('/update', authenticateUser, uploadFile, updateUserHandler);
router.post('/update', readLanguage, authenticateUser, uploadFile, updateUserHandler);

router.get('/profile', authenticateUser, getProfileHandler);

Expand Down
12 changes: 8 additions & 4 deletions server/src/user/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import AppError from '../../utils/appError';
import { signJwt } from '../../utils/jwt';
import { IUserUpdateDto, UserDto } from '../dto/user.dto';
import { UserRepository } from '../repository/user.repository';

import { File } from '../../types/file';
export const createUser = async (user: UserDto) => {
return await UserRepository.createUser(user);
};
Expand Down Expand Up @@ -108,16 +108,20 @@ export async function updateUser({
user: IUser;
username: string;
notification: boolean;
avatar: string;
avatar: File | undefined;
}) {
const userUpdate: IUserUpdateDto = {
id: user.id,
username,
notification,
avatar,
avatar: null,
};
try {
await UserRepository.updateUser(userUpdate);
if (!avatar) {
userUpdate.avatar = user.avatar;
await UserRepository.updateUser(userUpdate);
}
// TODO: Logic if user change avatar
} catch (err: any) {
console.error(err);
throw new AppError(400, 'Erreur lors de la mise à jour du profil.');
Expand Down

0 comments on commit a7868db

Please sign in to comment.