diff --git a/containers/api/package-lock.json b/containers/api/package-lock.json index 5821b426..e925e7b2 100644 --- a/containers/api/package-lock.json +++ b/containers/api/package-lock.json @@ -15,6 +15,7 @@ "@nestjs/passport": "^9.0.3", "@nestjs/platform-express": "^9.0.0", "@nestjs/typeorm": "^9.0.1", + "@types/multer": "^1.4.7", "axios": "^1.4.0", "base32-decode": "^1.0.0", "base32-encode": "^2.0.0", @@ -2132,7 +2133,6 @@ "version": "1.19.2", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz", "integrity": "sha512-ALYone6pm6QmwZoAgeyNksccT9Q4AWZQ6PvfwR37GT6r6FWUPguq6sUmNGSMV2Wr761oQoBxwGGa6DR5o1DC9g==", - "dev": true, "dependencies": { "@types/connect": "*", "@types/node": "*" @@ -2141,14 +2141,12 @@ "node_modules/@types/body-parser/node_modules/@types/node": { "version": "18.16.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.3.tgz", - "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==", - "dev": true + "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==" }, "node_modules/@types/connect": { "version": "3.4.35", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz", "integrity": "sha512-cdeYyv4KWoEgpBISTxWvqYsVy444DOqehiF3fM3ne10AmJ62RSyNkUnxMJXHQWRQQX2eR94m5y1IZyDwBjV9FQ==", - "dev": true, "dependencies": { "@types/node": "*" } @@ -2156,8 +2154,7 @@ "node_modules/@types/connect/node_modules/@types/node": { "version": "18.16.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.3.tgz", - "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==", - "dev": true + "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==" }, "node_modules/@types/cookiejar": { "version": "2.1.2", @@ -2192,7 +2189,6 @@ "version": "4.17.17", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.17.tgz", "integrity": "sha512-Q4FmmuLGBG58btUnfS1c1r/NQdlp3DMfGDGig8WhfpA2YRUtEkxAjkZb0yvplJGYdF1fsQ81iMDcH24sSCNC/Q==", - "dev": true, "dependencies": { "@types/body-parser": "*", "@types/express-serve-static-core": "^4.17.33", @@ -2204,7 +2200,6 @@ "version": "4.17.34", "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.34.tgz", "integrity": "sha512-fvr49XlCGoUj2Pp730AItckfjat4WNb0lb3kfrLWffd+RLeoGAMsq7UOy04PAPtoL01uKwcp6u8nhzpgpDYr3w==", - "dev": true, "dependencies": { "@types/node": "*", "@types/qs": "*", @@ -2215,8 +2210,7 @@ "node_modules/@types/express-serve-static-core/node_modules/@types/node": { "version": "18.16.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.3.tgz", - "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==", - "dev": true + "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==" }, "node_modules/@types/graceful-fs": { "version": "4.1.6", @@ -2283,8 +2277,15 @@ "node_modules/@types/mime": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", - "integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==", - "dev": true + "integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==" + }, + "node_modules/@types/multer": { + "version": "1.4.7", + "resolved": "https://registry.npmjs.org/@types/multer/-/multer-1.4.7.tgz", + "integrity": "sha512-/SNsDidUFCvqqcWDwxv2feww/yqhNeTRL5CVoL3jU4Goc4kKEL10T7Eye65ZqPNi4HRx8sAEX59pV1aEH7drNA==", + "dependencies": { + "@types/express": "*" + } }, "node_modules/@types/node": { "version": "18.15.11", @@ -2306,14 +2307,12 @@ "node_modules/@types/qs": { "version": "6.9.7", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz", - "integrity": "sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==", - "dev": true + "integrity": "sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==" }, "node_modules/@types/range-parser": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.4.tgz", - "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==", - "dev": true + "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==" }, "node_modules/@types/semver": { "version": "7.3.13", @@ -2325,7 +2324,6 @@ "version": "0.17.1", "resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.1.tgz", "integrity": "sha512-Cwo8LE/0rnvX7kIIa3QHCkcuF21c05Ayb0ZfxPiv0W8VRiZiNW/WuRupHKpqqGVGf7SUA44QSOUKaEd9lIrd/Q==", - "dev": true, "dependencies": { "@types/mime": "^1", "@types/node": "*" @@ -2334,14 +2332,12 @@ "node_modules/@types/send/node_modules/@types/node": { "version": "18.16.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.3.tgz", - "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==", - "dev": true + "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==" }, "node_modules/@types/serve-static": { "version": "1.15.1", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.1.tgz", "integrity": "sha512-NUo5XNiAdULrJENtJXZZ3fHtfMolzZwczzBbnAeBbqBwG+LaG6YaJtuwzwGSQZ2wsCrxjEhNNjAkKigy3n8teQ==", - "dev": true, "dependencies": { "@types/mime": "*", "@types/node": "*" @@ -2350,8 +2346,7 @@ "node_modules/@types/serve-static/node_modules/@types/node": { "version": "18.16.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.3.tgz", - "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==", - "dev": true + "integrity": "sha512-OPs5WnnT1xkCBiuQrZA4+YAV4HEJejmHneyraIaxsbev5yCEr6KMwINNFP9wQeFIw8FWcoTqF3vQsa5CDaI+8Q==" }, "node_modules/@types/stack-utils": { "version": "2.0.1", diff --git a/containers/api/package.json b/containers/api/package.json index 28893ce0..74c414b2 100644 --- a/containers/api/package.json +++ b/containers/api/package.json @@ -26,6 +26,7 @@ "@nestjs/passport": "^9.0.3", "@nestjs/platform-express": "^9.0.0", "@nestjs/typeorm": "^9.0.1", + "@types/multer": "^1.4.7", "axios": "^1.4.0", "base32-decode": "^1.0.0", "base32-encode": "^2.0.0", diff --git a/containers/api/src/app.controller.ts b/containers/api/src/app.controller.ts index 8edc5334..f8a698da 100644 --- a/containers/api/src/app.controller.ts +++ b/containers/api/src/app.controller.ts @@ -1,4 +1,5 @@ -import { Controller, Request, Req, Get, Post, UseGuards, Redirect, Res, Body } from '@nestjs/common'; +import { Controller, Request, Req, Get, Post, UseGuards, Redirect, Res, Body, UploadedFile, UseInterceptors} from '@nestjs/common'; +import { FileInterceptor } from '@nestjs/platform-express'; import { JwtAuthGuard } from './auth/jwt-auth.guard'; import { AuthService } from './auth/auth.service'; @@ -51,10 +52,29 @@ export class AppController { return await this.userService.findOne(data.username); } + @UseGuards(JwtAuthGuard) @Get('/friends') async getFriends(@Request() req) { // return await this.userService.getFriends(req.user.username); - return await this.userService.getFriends("apommier"); + return await this.userService.getFriends(req.user.username); + } + + @UseGuards(JwtAuthGuard) + @Post('/friend') + async newFriend(@Request() req, @Body() data: any) { + // return await this.userService.getFriends(req.user.username); + console.log(`user= ${req.user.username}`) + const user = await this.userService.findOne(req.user.username) + return await this.userService.addFriend(user, data.username); + } + + @UseGuards(JwtAuthGuard) + @Post('/status') + async setStatus(@Request() req, @Body() data: any) { + const user = await this.userService.findOne(req.user.username); + + user.status = data.status; + await this.userService.save(user); } @UseGuards(JwtAuthGuard) @@ -62,12 +82,38 @@ export class AppController { async setNickname(@Request() req, @Body() data: any) { // let user = req.user // user.nickname = data.nickname + console.log(`user= ${req.user.username}`) let user = await this.userService.findOne(req.user.username) user.nickname = data.nickname; // return await this.userService.getFriends(req.user.username); return await this.userService.save(user); } + @UseGuards(JwtAuthGuard) + @Post('/picture') + @UseInterceptors(FileInterceptor('photo')) + async setProfilPicture(@Request() req, @UploadedFile() file: Express.Multer.File) { + let user = await this.userService.findOne(req.user.username) + if (!file) + user.photo = null; + else + user.photo = file.buffer; + return await this.userService.save(user); + } + + @UseGuards(JwtAuthGuard) + @Post('/getPicture') + async getProfilPicture(@Body() data: any) { + // console.log(`dataaaaa= ${data.username}`) + return await this.userService.getPic(data.username) + + // return user.photo + // const photoData = user.photo; + // Buffer.from(user.photo, 'binary').buffer; + // const arrayBuffer = ArrayBuffer.from(photoData, 'binary'); + // return await this.userService.save(user); + } + //======================================================================================================== //======================================================================================================== // Pong @@ -125,6 +171,13 @@ export class AppController { return user.rank; } +// @UseGuards(JwtAuthGuard) + @Get('/ranking') + async getRanking() + { + return await this.userService.getRanking(); + } + @UseGuards(JwtAuthGuard) @Post('/history') async getHistory(@Body() data: any) diff --git a/containers/api/src/auth/login42.ts b/containers/api/src/auth/login42.ts index a8b34010..4d0adea4 100644 --- a/containers/api/src/auth/login42.ts +++ b/containers/api/src/auth/login42.ts @@ -69,6 +69,7 @@ export class loginClass { doubleAuth: 0, friendRequest: null, friends: null, + photo: null, }; await this.usersService.create(user); } diff --git a/containers/api/src/model/user.entity.ts b/containers/api/src/model/user.entity.ts index b11fb3bd..c0413098 100644 --- a/containers/api/src/model/user.entity.ts +++ b/containers/api/src/model/user.entity.ts @@ -26,6 +26,9 @@ export class User { @Column({ nullable: true }) username: string; + @Column({ type: 'bytea', nullable: true }) + photo: Buffer; + @Column({ nullable: true }) password: string; diff --git a/containers/api/src/users/users.service.ts b/containers/api/src/users/users.service.ts index 43b5cb41..f557c6b1 100644 --- a/containers/api/src/users/users.service.ts +++ b/containers/api/src/users/users.service.ts @@ -65,6 +65,32 @@ export class UsersService { // }); } } + + async addFriend(user: User, username: string) { + user.friends = user.friends || []; + user.friends.push(username); + this.save(user); + } + + async getRanking() { + return await this.userRepository.query("SELECT * FROM \"User\" ORDER BY rank DESC;"); + } + + async getPic( username: string) { + // const user = await this.findOne(username); + let result = await this.userRepository.query("select encode(photo, 'base64') FROM public.\"User\" WHERE username = $1;", [username]); + // console.log(`result= ${result}`) + // console.log(`result= ${result.text}`) + // console.log(`encode= ${result.encode}`) + // console.log(`encode= ${result.string}`) + if (result.length > 0) { + const encodedPhoto = result[0].encode; + console.log(`pic!!! =`) + return encodedPhoto; + } + console.log(`no pic`) + return undefined + } } diff --git a/containers/react/src/components/App.jsx b/containers/react/src/components/App.jsx index 9f01f9b3..204d2a28 100644 --- a/containers/react/src/components/App.jsx +++ b/containers/react/src/components/App.jsx @@ -17,6 +17,7 @@ import SuccessToken from '../script/tokenSuccess' import DoubleAuth from "../pages/2fa.js"; import Game from "../pages/Game.jsx"; import Social from "../components/Social/Social.jsx"; +import Logout from "../components/Profile/Logout.jsx"; function AnimatedRoute () { const location = useLocation(); @@ -38,6 +39,7 @@ function AnimatedRoute () { {/* }/> */} }/> + }/> }/> diff --git a/containers/react/src/components/Game/Rank.jsx b/containers/react/src/components/Game/Rank.jsx new file mode 100644 index 00000000..8e167a18 --- /dev/null +++ b/containers/react/src/components/Game/Rank.jsx @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Rank.jsx :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/09 08:49:24 by apommier #+# #+# */ +/* Updated: 2023/06/09 08:55:22 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// import React from "react" +import React, { useState, useEffect, useRef } from "react"; +// import {Rank} from '../../DataBase/DataRank.js' +import DefaultPicture from '../../assets/profile.jpg' +import api from '../../script/axiosApi'; + +function Rank({user, index}){ + + const [profilePicture, setProfilePicture] = useState(''); + + useEffect(() => { + const fetchProfilePicture = async () => { + try { + // const user = await api.get("/profile"); + const pic = await api.post("/getPicture", {username: user.username}) + // console.log(`user naem profile pic222= ${currentUser.username}`) + // console.log(` profile pic222= ${pic.data}`) + setProfilePicture(pic.data); + } catch (error) { + console.error('Error fetching profile picture:', error); + } + }; + + fetchProfilePicture(); + }) + + return ( +
+
+

{index + 1}

+

{user.rank}: {user.nickname} + {profilePicture ? ( + + ) : ( + Default Profile Picture + )} + {/* */} +

+
+

{user.opponent}

+
+ ) +} + +export default Rank \ No newline at end of file diff --git a/containers/react/src/components/Game/Ranking.jsx b/containers/react/src/components/Game/Ranking.jsx index 25a5bd06..8cce52d5 100644 --- a/containers/react/src/components/Game/Ranking.jsx +++ b/containers/react/src/components/Game/Ranking.jsx @@ -1,25 +1,59 @@ -import React from "react" -import {Rank} from '../../DataBase/DataRank.js' +// import React from "react" +import React, { useState, useEffect, useRef } from "react"; +import Rank from './Rank.jsx' import defaultpic from '../../assets/profile.jpg' +import api from '../../script/axiosApi'; function Ranking(){ + + const [isLoading, setIsLoading] = useState(true); + const [ranking, setRanking] = useState([]); + + useEffect(()=> { + + const getRanking = async ()=>{ + try{ + // const tmpFriends = await api.get("/friends") + const Ranking = await api.get("/ranking") + setRanking(Ranking.data); + console.log(`ranking= ${Ranking.data}`) + // setFriends(tmpFriends.data); + // return tmpUser; + // console.log(`user= ${tmpUser.data.username}`); + setIsLoading(false) + + } + catch(err){ + console.log(err); + } + }; + getRanking(); + + }, []) + + console.log(`ranking after= ${ranking}`) + return (
-

Ranking

+ {isLoading ? ( + <> + ) : ( + //

Ranking

- {Rank.map((item, index) => { - return ( -
-
  • -
    -

    {item.rank}: {item.name}

    -
    - {/*

    {item.openent}

    */} -
  • -
    - ) - })} + {ranking.map((user, index) => ( + + // return ( + //
    + //
    + //

    {index + 1}

    + //

    {user.rank}: {user.nickname}

    + //
    + //

    {user.opponent}

    + //
    + // ) + ))}
    + )}
    ) } diff --git a/containers/react/src/components/Header.jsx b/containers/react/src/components/Header.jsx index ebe6453e..15c06b7d 100644 --- a/containers/react/src/components/Header.jsx +++ b/containers/react/src/components/Header.jsx @@ -1,4 +1,4 @@ -import React, {useState} from 'react'; +import React, {useState, useEffect} from 'react'; import {AiOutlineMenuUnfold} from 'react-icons/ai'; // import * as AiIcons from 'react-icons/ai'; import {Link} from 'react-router-dom'; @@ -10,7 +10,7 @@ import Modal from './Sidebar/Modal'; // import AnimatePresence from import '../styles/Header.css'; - +import api from '../script/axiosApi'; function Header() { // const [sidebar, setSidebar] = useState(false); @@ -18,55 +18,30 @@ function Header() { const [modalOpen, setModalOpen] = useState(false); const close = () => setModalOpen(false); const open = () => setModalOpen(true); + + const [profilePicture, setProfilePicture] = useState(''); + + useEffect(() => { + const fetchProfilePicture = async () => { + try { + const user = await api.get("/profile"); + const pic = await api.post("/getPicture", {username: user.data.username}) + setProfilePicture(pic.data); + // console.log(`profile pic222= ${pic.data}`) + } catch (error) { + console.error('Error fetching profile picture:', error); + } + }; + + fetchProfilePicture(); + }, []); + + // console.log(`profile pic= ${profilePicture}`) + + // photo.toString('base64') return (
    - {/*
    - - - - onClick={() => (modalOpen ? close() : open())}> - - - -
    - - profile - -
    */} - {/* - - -
    - - profile - -
    */} - {/*
    */} - - {/* */} - (modalOpen ? close() : open())}> @@ -75,7 +50,17 @@ function Header() {
    - profile +
    + + {profilePicture ? ( + // Profile Picture + + // Profile Picture + ) : ( + Default Profile Picture + )} +
    +
    - {/*
    */} +
    profile @@ -260,7 +260,6 @@ function Chats(){
    - {/*
    */} {conversations.map(c=> (
    setCurrentChat(c)}> @@ -276,25 +275,29 @@ function Chats(){ { currentChat ? ( <> -
    - {messages.map(m=>( - - ))} - {/* */} -
    - setNewMessage(e.target.value)} - value={newMessages} - /> -
    - +
    + {messages.map(m=>( + + ))} + {/* */} +
    + setNewMessage(e.target.value)} + value={newMessages} + /> +
    + +
    -
    ) : (Open a conversation)} -
    + + ) : ( + Open a conversation)} +
    + //
    ); diff --git a/containers/react/src/components/Messages/Message.jsx b/containers/react/src/components/Messages/Message.jsx index 846c60f0..cd10f4e1 100644 --- a/containers/react/src/components/Messages/Message.jsx +++ b/containers/react/src/components/Messages/Message.jsx @@ -6,15 +6,16 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/01 18:24:46 by apommier #+# #+# */ -/* Updated: 2023/06/01 18:33:43 by apommier ### ########.fr */ +/* Updated: 2023/06/09 09:00:06 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ -import React from "react" +import { useEffect, useState, useRef } from "react"; +import api from '../../script/axiosApi'; import styled from "styled-components" -import DefaultPic from '../../assets/profile.jpg' -import { useRef } from "react"; -import { useEffect } from "react"; +import DefaultPicture from '../../assets/profile.jpg' +// import { useRef } from "react"; +// import { useEffect } from "react"; import '../../styles/Messages.css' const MeStyleP = styled.p` @@ -26,17 +27,39 @@ const MeStyleP = styled.p` ` function MessageMe({message, own}){ + + const [profilePicture, setProfilePicture] = useState(''); const scrollRef = useRef(); useEffect(() => { - scrollRef.current?.scrollIntoView({ behavior: "smooth"}) -}, []) + scrollRef.current?.scrollIntoView({ behavior: "smooth"}) + const fetchProfilePicture = async () => { + try { + // const user = await api.get("/profile"); + const pic = await api.post("/getPicture", {username: message.sender}) + // console.log(`user naem profile pic222= ${currentUser.username}`) + // console.log(` profile pic222= ${pic.data}`) + setProfilePicture(pic.data); + } catch (error) { + console.error('Error fetching profile picture:', error); + } + }; + fetchProfilePicture(); + }, []) + return (
    - profile + {/* profile + */} + {profilePicture ? ( + + ) : ( + Default Profile Picture + )}
    -
    {message.sender}
    + {/*
    {message.senderNickname}
    */} +
    {message.sender}
    {message.text}
    diff --git a/containers/react/src/components/Profile/EditName.jsx b/containers/react/src/components/Profile/EditName.jsx index 97f5690e..4111f064 100644 --- a/containers/react/src/components/Profile/EditName.jsx +++ b/containers/react/src/components/Profile/EditName.jsx @@ -62,6 +62,7 @@ const ModalEdit = ( handleClose ) => {
    + ) } diff --git a/containers/react/src/components/Profile/Logout.jsx b/containers/react/src/components/Profile/Logout.jsx new file mode 100644 index 00000000..dd422844 --- /dev/null +++ b/containers/react/src/components/Profile/Logout.jsx @@ -0,0 +1,15 @@ + + + +function Logout(){ + + localStorage.clear(); + const path = `http://localhost/`; + // history(path, { replace: true }); + // window.location.replace(path); + // window.history.pushState({}, '', path); + window.history.pushState({}, null, path); + window.location.reload(false); +} + +export default Logout; \ No newline at end of file diff --git a/containers/react/src/components/Sidebar/SidebarData.js b/containers/react/src/components/Sidebar/SidebarData.js index dae5dfaf..afbf3d52 100644 --- a/containers/react/src/components/Sidebar/SidebarData.js +++ b/containers/react/src/components/Sidebar/SidebarData.js @@ -39,7 +39,7 @@ export const SidebarData = [ }, { title: 'Log out', - path: '/team', + path: '/logout', icon: , cName: 'nav-text' }, diff --git a/containers/react/src/components/Social/Friend.jsx b/containers/react/src/components/Social/Friend.jsx new file mode 100644 index 00000000..8630f8c1 --- /dev/null +++ b/containers/react/src/components/Social/Friend.jsx @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Friend.jsx :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/09 08:18:58 by apommier #+# #+# */ +/* Updated: 2023/06/09 08:35:21 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +import { useEffect, useState } from "react"; +import api from '../../script/axiosApi'; +import DefaultPicture from '../../assets/profile.jpg' +import styled from "styled-components"; + +import { RxCircle } from "react-icons/rx"; +import { CgFontSpacing } from "react-icons/cg"; + +const UserChat = styled.div ` + padding: 5px; + display: flex; + align-items: center; + gap: 5px; + color: white; + cursor: pointer; + + &:hover{ + background-color: #3e3c61; + } +` + +const SideP = styled.p` + font-size: 14px; + color: lightgray; + margin-left: 15px; +` + +export default function Friend({currentUser}) +{ + const [profilePicture, setProfilePicture] = useState(''); + + useEffect(() => { + const fetchProfilePicture = async () => { + try { + // const user = await api.get("/profile"); + const pic = await api.post("/getPicture", {username: currentUser.username}) + // console.log(`user naem profile pic222= ${currentUser.username}`) + // console.log(` profile pic222= ${pic.data}`) + setProfilePicture(pic.data); + } catch (error) { + console.error('Error fetching profile picture:', error); + } + }; + + fetchProfilePicture(); + }) + + function getStatus(friend) + { + let status = friend.status + console.log(`status= ${status}`) + let statusColor; + + if (status === 0) + statusColor = 'grey'; + else if (status === 1) + statusColor = 'green'; + else if (status === 2) + statusColor = 'blue'; + return statusColor; + } + + const handleSpectate = (user) => { + //socket connection and add to party with one with username + console.log(`spectate hehe`) + console.log(`user= ${user}`) + }; + + const handleButtonClick = (user) => { + let path = `http://localhost/profile/${user.username}`; + // history(path, { replace: true }); + // window.location.replace(path); + window.history.pushState({}, null, path); + window.location.reload(false); + }; + + return ( + + {profilePicture ? ( + + ) : ( + Default Profile Picture + )} +
    + handleButtonClick(currentUser)}>{currentUser.nickname} + + + {getStatus(currentUser) !== 'blue' ? ( + <> + ) : ( + + )} +
    +
    + ) +} + + diff --git a/containers/react/src/components/Social/Social.jsx b/containers/react/src/components/Social/Social.jsx index 1d6a5036..b4119b35 100644 --- a/containers/react/src/components/Social/Social.jsx +++ b/containers/react/src/components/Social/Social.jsx @@ -1,32 +1,21 @@ -import DefaultPic from '../../assets/profile.jpg' +import DefaultPicture from '../../assets/profile.jpg' import api from '../../script/axiosApi'; import React, { useState, useEffect, useRef } from "react"; import styled from "styled-components"; + +import Friend from './Friend.jsx'; + import { ImBlocked } from 'react-icons/im'; import { MdOutlineGroupAdd } from 'react-icons/md'; + // import React from "react"; import { useNavigate } from "react-router-dom"; -const UserChat = styled.div ` - padding: 5px; - display: flex; - align-items: center; - gap: 5px; - color: white; - cursor: pointer; - &:hover{ - background-color: #3e3c61; - } -` -const SideP = styled.p` - font-size: 14px; - color: lightgray; - margin-left: 15px; -` + const TouchDiv = styled.div` margin-left: 10px; margin-right: 4px; @@ -45,6 +34,7 @@ function Social (){ const [friends, setFriends] = useState([]); const [isLoading, setIsLoading] = useState(true); const [user, setUser] = useState(null); + const [profilePicture, setProfilePicture] = useState(''); useEffect(()=> { @@ -52,6 +42,8 @@ function Social (){ try{ const tmpFriends = await api.get("/friends") const tmpUser = await api.get("/profile") + const pic = await api.post("/getPicture", {username: tmpUser.data.username}) + setProfilePicture(pic.data); setUser(tmpUser.data); setFriends(tmpFriends.data); // return tmpUser; @@ -67,18 +59,35 @@ function Social (){ }, []) - const handleButtonClick = (user) => { - let path = `http://localhost/profile/${user.username}`; - // history(path, { replace: true }); - // window.location.replace(path); - window.history.pushState({}, null, path); - window.location.reload(false); - }; + + + + + // const { status } = this.props; + // let statusColor = ''; + // // let statusIcon = RxCircle; + // let status = 0 + + // if (status === 0) { + // // statusIcon = faCircle; + // statusColor = 'green'; + // } else if (status === 1) { + // // statusIcon = faCircle; + // statusColor = 'red'; + // } else if (status === 2) { + // // statusIcon = faCircle; + // statusColor = 'blue'; + // } return (
    - profile + {/* profile */} + {profilePicture ? ( + + ) : ( + Default Profile Picture + )} {isLoading ? (

    Loading...

    @@ -97,15 +106,27 @@ function Social (){
    {friends.map(c=> ( -
    handleButtonClick(c)}> - - User -
    - {c.nickname} - Desc? -
    -
    -
    + + // + // {profilePicture ? ( + // + // ) : ( + // Default Profile Picture + // )} + //
    + // handleButtonClick(c)}>{c.nickname} + // + // + // {getStatus(c) !== 'blue' ? ( + // <> + // ) : ( + // + // )} + //
    + //
    + + //
    + ))} ) diff --git a/containers/react/src/pages/Home.jsx b/containers/react/src/pages/Home.jsx index 1da40cd4..ad99f226 100644 --- a/containers/react/src/pages/Home.jsx +++ b/containers/react/src/pages/Home.jsx @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Home.jsx :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/09 08:19:04 by apommier #+# #+# */ +/* Updated: 2023/06/09 08:19:05 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + // import { React, useState } from "react"; import '../styles/Profile.css' // import '../styles/App.css' @@ -34,18 +46,63 @@ function Profile () { const [user, setUser] = useState(null); const [isLoading, setIsLoading] = useState(true); const [modalOpen, setModalOpen] = useState(false); + const [mine, setMine] = useState(false); const close = () => setModalOpen(false); const open = () => setModalOpen(true); const { username } = useParams(); + const [selectedPhoto, setSelectedPhoto] = useState(null); + const [profilePicture, setProfilePicture] = useState(''); + + const handleFileChange = (event) => { + // const file = event.target.files[0]; + setSelectedPhoto(event.target.files[0]); + // try{ + // api.post("/picture", {picture: URL.createObjectURL(file)}) + // } + // catch(err){ + // console.log(err); + // } + }; + + const handleUpload = async () => { + const formData = new FormData(); + formData.append('photo', selectedPhoto); + try { + await api.post('/picture', formData); + console.log('File uploaded successfully'); + window.location.reload(false); + } catch (error) { + console.error('Error uploading file:', error); + } + }; + useEffect(()=> { const getUser = async ()=>{ console.log(`username= ${username}`) + // const pic + let pic try{ - const tmpUser = await api.post("/user", {username: username}) - setUser(tmpUser.data); - console.log(`user= ${tmpUser.data.username}`) + const me = await api.get("/profile") + if (!username) + { + setMine(true); + setUser(me.data); + console.log(`mine= true = ${mine}`) + pic = await api.post("/getPicture", {username: me.data.username}) //good one? + // username = me.data.username + } + else + { + const tmpUser = await api.post("/user", {username: username}) + setUser(tmpUser.data); + pic = await api.post("/getPicture", {username: username}) //good one? + + } + // const pic = await api.get("/picture")//pic du user + setProfilePicture(pic.data); + // console.log(`user= ${tmpUser.data.username}`) setIsLoading(false) } catch(err){ @@ -57,7 +114,13 @@ function Profile () { return (
    - Profile pic + {/* Profile pic + */} + {profilePicture ? ( + + ) : ( + Default Profile Picture + )} {isLoading ? (

    Loading...

    @@ -65,11 +128,28 @@ function Profile () {

    {user.nickname}

    )}
    + + + + {mine ? ( +
    (modalOpen ? close() : open())}> {modalOpen === true ? : } + +
    + + +
    +
    + ) : ( + <> + )} + + + null}> diff --git a/containers/react/src/pages/canvas.js b/containers/react/src/pages/canvas.js index e04cf5af..ec45cf8c 100644 --- a/containers/react/src/pages/canvas.js +++ b/containers/react/src/pages/canvas.js @@ -120,6 +120,7 @@ export function drawCanvas() { let response = await api.get('/profile'); const myName = response.data.username; response = await api.get('/rank'); + await api.post('/status', {status: 2}); opRank = response.data console.log(`rank= ${opRank}`); console.log(`myname= ${myName}`); @@ -316,11 +317,13 @@ function draw(timestamp) if (myScore === maxScore) { api.post('/win', data); + api.post('/status', {status: 1}); console.log("send win"); } else { api.post('/loss', data); + api.post('/status', {status: 1}); console.log("send loose"); } window.location.replace("http://localhost/pong");