forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement getUserName in app, api and cor, and modified styles b00tc4…
- Loading branch information
Showing
17 changed files
with
150 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
staff/tatiana-garcia/project/api/handlers/getUserNameHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,45 @@ | ||
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 <header className='bg-teal-100 fixed left-0 top-0 w-full flex items-center gap-2 p-2'> | ||
<Container className='flex-shrink'> | ||
<img src='../../../logoExoticus.png' alt='logo exoticus' className='h-[3rem] w-auto' /> | ||
</Container> | ||
<Container> | ||
<Heading className=' text-xl font-bold text-black italic m-1'>Exoticus</ Heading> | ||
</Container> | ||
|
||
<Container className='font-extrabold text-right flex flex-col justify-end text-sm'> | ||
{ | ||
name ? ( | ||
<Paragraph>Hola, {name}!</Paragraph> | ||
) : ( | ||
<Paragraph>Hola!</Paragraph> | ||
) | ||
} | ||
</Container> | ||
</header> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { User } from '../data/models.js' | ||
|
||
import { validate, errors } from '../../com/index.js' | ||
|
||
const { NotFoundError, SystemError } = errors | ||
|
||
export default (userId) => { | ||
validate.id(userId, 'userId') | ||
|
||
return User.findById(userId).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) throw new NotFoundError('user not found') | ||
|
||
return user.name | ||
}) | ||
} |
Oops, something went wrong.