From cf17d0628f6926768ac869224202ce419ffa4d7c Mon Sep 17 00:00:00 2001 From: Tatiana_Garcia <151873298+TatiGV@users.noreply.github.com> Date: Tue, 3 Sep 2024 00:05:47 +0200 Subject: [PATCH] Implement getUserName in app, api and cor, and modified styles #99 --- .../project/api/handlers/getUserHandler.js | 4 +- .../api/handlers/getUserNameHandler.js | 13 ++++++ .../project/api/handlers/index.js | 5 ++- staff/tatiana-garcia/project/api/index.js | 5 ++- .../project/app/logic/getUserName.js | 33 +++++++++++++++ .../tatiana-garcia/project/app/logic/index.js | 4 +- .../project/app/view/contact/index.jsx | 5 ++- .../project/app/view/home/Header.jsx | 33 ++++++++++++++- .../project/app/view/home/index.jsx | 41 +++++++++---------- .../project/app/view/login/index.jsx | 6 +-- .../app/view/petsitters/petsitterDetails.jsx | 16 +++----- .../project/app/view/register/index.jsx | 2 +- .../app/view/registerPetssiterUser/index.jsx | 14 +++---- .../project/app/view/settings/index.jsx | 2 +- .../app/view/settings/updatePetsitterUser.jsx | 4 +- .../project/cor/logic/getUserName.js | 17 ++++++++ .../tatiana-garcia/project/cor/logic/index.js | 4 +- 17 files changed, 150 insertions(+), 58 deletions(-) create mode 100644 staff/tatiana-garcia/project/api/handlers/getUserNameHandler.js create mode 100644 staff/tatiana-garcia/project/app/logic/getUserName.js create mode 100644 staff/tatiana-garcia/project/cor/logic/getUserName.js diff --git a/staff/tatiana-garcia/project/api/handlers/getUserHandler.js b/staff/tatiana-garcia/project/api/handlers/getUserHandler.js index 156eb345b..2b3d60162 100644 --- a/staff/tatiana-garcia/project/api/handlers/getUserHandler.js +++ b/staff/tatiana-garcia/project/api/handlers/getUserHandler.js @@ -3,10 +3,8 @@ import { logic } from '../../cor/index.js' export default (req, res, next) => { const { userId } = req - const { targetUserId } = req.params - try { - logic.getUser(userId, targetUserId) + logic.getUser(userId) .then(user => res.json(user)) .catch(error => next(error)) } catch (error) { diff --git a/staff/tatiana-garcia/project/api/handlers/getUserNameHandler.js b/staff/tatiana-garcia/project/api/handlers/getUserNameHandler.js new file mode 100644 index 000000000..7c98b44f3 --- /dev/null +++ b/staff/tatiana-garcia/project/api/handlers/getUserNameHandler.js @@ -0,0 +1,13 @@ +import { logic } from '../../cor/index.js' + +export default (req, res, next) => { + const { userId } = req + + try { + logic.getUserName(userId) + .then(name => res.json(name)) + .catch(error => next(error)) + } catch (error) { + next(error) + } +} \ No newline at end of file diff --git a/staff/tatiana-garcia/project/api/handlers/index.js b/staff/tatiana-garcia/project/api/handlers/index.js index 17b154003..6e6d5e22e 100644 --- a/staff/tatiana-garcia/project/api/handlers/index.js +++ b/staff/tatiana-garcia/project/api/handlers/index.js @@ -10,6 +10,8 @@ import addReviewHandler from './addReviewHandler.js' import getPetsitterReviewHandler from './getPetsitterReviewHandler.js' import deletePetsitterReview from './deletePetsitterReview.js' import updatePetsitterUserHandler from './updatePetsitterUserHandler.js' +import getUserNameHandler from './getUserNameHandler.js' + export { registerUserHandler, @@ -23,5 +25,6 @@ export { addReviewHandler, getPetsitterReviewHandler, deletePetsitterReview, - updatePetsitterUserHandler + updatePetsitterUserHandler, + getUserNameHandler } \ No newline at end of file diff --git a/staff/tatiana-garcia/project/api/index.js b/staff/tatiana-garcia/project/api/index.js index c4f2c60a3..4c48f89d6 100644 --- a/staff/tatiana-garcia/project/api/index.js +++ b/staff/tatiana-garcia/project/api/index.js @@ -9,6 +9,7 @@ import { registerUserHandler, authenticateUserHandler, getUserHandler, + getUserNameHandler, updateUserHandler, registerPetsitterUserHandler, getAllPetsittersHandler, @@ -35,7 +36,9 @@ mongoose.connect(process.env.MONGODB_URI) api.post('/users/auth', jsonBodyParser, authenticateUserHandler) - api.get('/users/:targetUserId', jwtVerifier, getUserHandler) + api.get('/users/:userId', jwtVerifier, getUserHandler) + + api.get('/users/:userId/name', jwtVerifier, getUserNameHandler) api.get('/petsitters', getAllPetsittersHandler) diff --git a/staff/tatiana-garcia/project/app/logic/getUserName.js b/staff/tatiana-garcia/project/app/logic/getUserName.js new file mode 100644 index 000000000..69a8572ab --- /dev/null +++ b/staff/tatiana-garcia/project/app/logic/getUserName.js @@ -0,0 +1,33 @@ +import { errors } from '../../com/index.js' + +const { SystemError } = errors + +import extractPayLoadFromToken from '../util/extractPayLoadFromToken' + +export default () => { + const { sub: userId } = extractPayLoadFromToken(sessionStorage.token) + + return fetch(`${import.meta.env.VITE_API_URL}/users/${userId}/name`, { + method: 'GET', + headers: { + Authorization: `Bearer ${sessionStorage.token}` + } + }) + .catch(error => { throw new SystemError(error.message) }) + .then(response => { + const { status } = response + + if (status === 200) + return response.json() + .then(name => name) + + return response.json() + .then(body => { + const { error, message } = body + + const constructor = errors[error] + + throw new constructor(message) + }) + }) +} \ No newline at end of file diff --git a/staff/tatiana-garcia/project/app/logic/index.js b/staff/tatiana-garcia/project/app/logic/index.js index d76ef1313..e89b2cf5b 100644 --- a/staff/tatiana-garcia/project/app/logic/index.js +++ b/staff/tatiana-garcia/project/app/logic/index.js @@ -14,6 +14,7 @@ import addReview from './addReview' import getPetsitterReview from './getPetsitterReview' import deletePetsitterReview from './deletePetsitterReview' import updatePetsitterUser from './updatePetsitterUser' +import getUserName from './getUserName' const logic = { registerUser, @@ -31,7 +32,8 @@ const logic = { addReview, getPetsitterReview, deletePetsitterReview, - updatePetsitterUser + updatePetsitterUser, + getUserName } export default logic \ No newline at end of file diff --git a/staff/tatiana-garcia/project/app/view/contact/index.jsx b/staff/tatiana-garcia/project/app/view/contact/index.jsx index 5f5eeb671..12b7c2f0d 100644 --- a/staff/tatiana-garcia/project/app/view/contact/index.jsx +++ b/staff/tatiana-garcia/project/app/view/contact/index.jsx @@ -16,8 +16,9 @@ export default function Contact({ onRegisterPetsitterUserClick }) { return <>
- - ¿Necesitas ayuda? Contáctanos + Contáctanos + + ¿Necesitas ayuda? Puedes enviarnos tus dudas o mejoras sobre la aplicación al siguiente correo y te contactaremos lo antes posible. diff --git a/staff/tatiana-garcia/project/app/view/home/Header.jsx b/staff/tatiana-garcia/project/app/view/home/Header.jsx index c015b7d51..baebc05c4 100644 --- a/staff/tatiana-garcia/project/app/view/home/Header.jsx +++ b/staff/tatiana-garcia/project/app/view/home/Header.jsx @@ -1,9 +1,30 @@ -import React from 'react' +import { useState, useEffect } from 'react' + +import logic from '../../logic' import Container from '../library/Container.jsx' import Heading from '../library/Heading.jsx' +import Paragraph from '../library/Paragraph.jsx' export default function Header() { + const [name, setName] = useState(null) + const [userRole, setUserRole] = useState(null) + + useEffect(() => { + if (logic.isUserLoggedIn()) { + logic.getUserName() + .then(name => { + setName(name) + const role = logic.getUserRole() + setUserRole(role) + }) + .catch(error => { + console.error(error) + alert(error.message) + }) + } + }, []) + return
logo exoticus @@ -11,6 +32,14 @@ export default function Header() { Exoticus - + + { + name ? ( + Hola, {name}! + ) : ( + Hola! + ) + } +
} \ No newline at end of file diff --git a/staff/tatiana-garcia/project/app/view/home/index.jsx b/staff/tatiana-garcia/project/app/view/home/index.jsx index 4ec27e770..80c22734a 100644 --- a/staff/tatiana-garcia/project/app/view/home/index.jsx +++ b/staff/tatiana-garcia/project/app/view/home/index.jsx @@ -8,32 +8,31 @@ import Image from '../library/Image' export default function Home({ }) { return <>
-
- - animales exoticos +
+ animales exoticos - - ¿Cómo funciona? - - Exotics es una aplicación donde podrás contactar con las guarderías de exóticos más cercanas para que cuiden de tus mascotas cuando lo necesites. - + + ¿Cómo funciona? + + Exotics es una aplicación donde podrás contactar con las guarderías de exóticos más cercanas para que cuiden de tus mascotas cuando lo necesites. + - 🔍Busca - - Entra en la aplicación y busca al cuidador o la guardería más cercana a ti. - + 🔍Busca + + Entra en la aplicación y busca al cuidador o la guardería más cercana a ti. + - 📧Contacta y reserva - - Contacta con el cuidador o la guardería que más se adecue a tus necesidades y reserva. - + 📧Contacta y reserva + + Contacta con el cuidador o la guardería que más se adecue a tus necesidades y reserva. + - 🐾Disfruta - - Tu mascota estará cuidada mientras tú disfrutas de tus vacaciones. - - + 🐾Disfruta + + Tu mascota estará cuidada mientras tú disfrutas de tus vacaciones. + +