Skip to content

Commit

Permalink
create changing header for login home and register in app b00tc4mp#101
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasOrtsBaguena committed Aug 22, 2024
1 parent 938e3e5 commit 46ff525
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 169 deletions.
70 changes: 36 additions & 34 deletions staff/lucas-orts/project/app/view/App.jsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
import { useState, useEffect } from 'react'
import { Routes, Route, useNavigate, Navigate } from 'react-router-dom'

import { Routes, Route, useNavigate } from 'react-router-dom'
import Header from './home/Header'
import Home from './home'
import Login from './login'
import Register from './register'
import Home from './home'
import Alert from './common/Alert'
import logic from '../logic'

import Alert from './common/Alert'
import { Context } from './context'

import logic from '../logic'

const App = () => {
export default function App() {
const navigate = useNavigate()

const [theme, setTheme] = useState(localStorage.theme)
const [isAuthenticated, setIsAuthenticated] = useState(logic.isUserLoggedIn())
const [alertMessage, setAlertMessage] = useState(null)

useEffect(() => {
document.documentElement.className = theme
localStorage.theme = theme
}, [theme])

const handleLogin = () => {
const handleHomeClick = () => {
navigate('/')
}

const handleRegisterClick = () => {
navigate('/register')
}

const handleRegister = () => {
navigate('/login')
}

const handleLoginClick = () => {
navigate('/login')
}

const handleLogout = () => {
setIsAuthenticated(false)
navigate('/') // Redirigir a Home después de logout
}

const handleLogin = () => {
setIsAuthenticated(true)
navigate('/')
}

const handleAlertAccept = () => setAlertMessage(null)
const handleRegisterClick = () => {
navigate('/register')
}

return <Context.Provider value={{ theme, setTheme, alert: setAlertMessage }}>
<Routes>
<Route path="/login" element={<Login onLogin={handleLogin} onRegisterClick={handleRegisterClick} />} />
<Route path="/register" element={<Register onRegister={handleRegister} onLoginClick={handleLoginClick} />} />
<Route path="/*" element={<Home onLogout={handleLogout} />} />
</Routes>
const handleAlertAccept = () => setAlertMessage(null)

{alertMessage && <Alert message={alertMessage} onAccept={handleAlertAccept} />}
</Context.Provider>
return (
<Context.Provider value={{ alert: setAlertMessage }}>
<>
<Header
onHomeClick={handleHomeClick}
onLogout={handleLogout}
onLoginClick={handleLoginClick} // Aquí pasamos la función correctamente
isAuthenticated={isAuthenticated}
/>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/login" element={<Login onLogin={handleLogin} onRegisterClick={handleRegisterClick} />} />
<Route path="/register" element={<Register onRegister={() => navigate('/login')} onLoginClick={handleLoginClick} />} />
</Routes>
{alertMessage && <Alert message={alertMessage} onAccept={handleAlertAccept} />}
</>
</Context.Provider>
)
}

export default App
16 changes: 9 additions & 7 deletions staff/lucas-orts/project/app/view/home/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { useState, useEffect } from 'react'
import Button from '../library/Button'
import Paragraph from '../library/Paragraph'
import Container from '../library/Container'
import logic from '../../logic' // Asegúrate de importar la lógica
import logic from '../../logic'

export default function Header({ onHomeClick, onLogout, isAuthenticated }) {
export default function Header({ onHomeClick, onLogout, onLoginClick, isAuthenticated }) {
const [name, setName] = useState(null)

useEffect(() => {
Expand All @@ -21,16 +21,18 @@ export default function Header({ onHomeClick, onLogout, isAuthenticated }) {
alert(error.message)
}
} else {
setName(null) // Clear the name when the user is not authenticated
setName(null)
}
}, [isAuthenticated])

const handleLogout = () => {
try {
logic.logoutUser() // Lógica para cerrar sesión
onLogout() // Llama al callback para actualizar el estado en Home
logic.logoutUser()

onLogout()
} catch (error) {
console.error(error)

alert(error.message)
}
}
Expand All @@ -40,7 +42,7 @@ export default function Header({ onHomeClick, onLogout, isAuthenticated }) {
<Container>
<Button onClick={onHomeClick}>Home</Button>
{!isAuthenticated ? (
<Button>Login</Button>
<Button onClick={onLoginClick}>Login</Button> // Esto debe llamar a onLoginClick
) : (
<>
<Paragraph>{name}</Paragraph>
Expand All @@ -50,4 +52,4 @@ export default function Header({ onHomeClick, onLogout, isAuthenticated }) {
</Container>
</header>
)
}
}
4 changes: 4 additions & 0 deletions staff/lucas-orts/project/app/view/home/Profile.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Container from '../library/Container'
import logic from '../../logic'

export default function Profile() { }
39 changes: 6 additions & 33 deletions staff/lucas-orts/project/app/view/home/index.jsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
import { useState } from 'react'
import { Routes, Route, useNavigate } from 'react-router-dom'

import Header from './Header'
// import Footer from './Footer'
import { Routes, Route } from 'react-router-dom'
import Hello from './Hello'
import isUserLoggedIn from '../../logic/isUserLoggedIn'

export default function Home() {
const navigate = useNavigate()
const [isAuthenticated, setIsAuthenticated] = useState(isUserLoggedIn())

const handleHomeClick = () => {
navigate('/')
}

const handleLogout = () => {
setIsAuthenticated(false)
}

return (
<>
<Header
onHomeClick={handleHomeClick}
onLogout={handleLogout}
isAuthenticated={isAuthenticated}
/>

<main className="flex flex-col items-center gap-4 mt-16 mb-12 dark:bg-black">
<Routes>

<Route path="/hello/:to" element={<Hello />} />
</Routes>
</main>

{/* <Footer/> */}
</>
<main className='flex flex-col items-center gap-4 mt-16 mb-12 dark:bg-black'>
<Routes>
<Route path='/hello/:to' element={<Hello />} />
</Routes>
</main>
)
}
106 changes: 49 additions & 57 deletions staff/lucas-orts/project/app/view/register/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ import Button from '../library/Button'
import useContext from '../context'

export default function Register({ onRegister, onLoginClick }) {
console.debug('Register -> call')

const { alert } = useContext()

const handleRegisterSubmit = event => {
console.debug('Register -> handleRegisterSubmit')

event.preventDefault()

const form = event.target
Expand All @@ -40,70 +36,66 @@ export default function Register({ onRegister, onLoginClick }) {

try {
logic.registerUser(name, surname, email, phone, address, password, passwordRepeat)
.then(() => onRegister())
.then(() => onRegister()) // Redirige después del registro
.catch(error => {
console.error(error)

alert(message)
alert(error.message)
})
} catch (error) {
console.error(error)

alert(error.message)
}
}

const handleLoginClick = event => {
console.debug('Register -> handleLoginClick')

event.preventDefault()

onLoginClick()
onLoginClick() // Redirige a login cuando se hace clic en el enlace de Login
}

return <main className="flex flex-col items-center gap-4 bg-white dark:bg-black h-screen dark:text-white">
<Heading className="font-scatters">Register</Heading>

<Form onSubmit={handleRegisterSubmit} className="flex-col">
<Container className="flex-col items-start">
<Label htmlFor="name-input">Name</Label>
<Input type="text" id="name-input" name="name" placeholder="name" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="surname-input">Surname</Label>
<Input type="text" id="surname-input" name="surname" placeholder="surname" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="email-input">E-mail</Label>
<Input type="email" id="email-input" name="email" placeholder="email" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="phone-input">Phone</Label>
<Input type="text" id="phone-input" name="phone" placeholder="phone" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="address-input">Address</Label>
<Input type="text" id="address-input" name="address" placeholder="address" />
</Container>


<Container className="flex-col items-start">
<Label htmlFor="password-input">Password</Label>
<Input type="password" id="password-input" name="password" placeholder="password" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="password-repeat-input">Repeat Password</Label>
<Input type="password" id="password-repeat-input" name="password-repeat" placeholder="repeat password" />
</Container>

<Button type="submit">Register</Button>
</Form>

<Link onClick={handleLoginClick}>Login</Link>
</main>
return (
<main className="flex flex-col items-center gap-4 bg-white dark:bg-black h-screen dark:text-white">
<Heading className="font-scatters">Register</Heading>

<Form onSubmit={handleRegisterSubmit} className="flex-col">
<Container className="flex-col items-start">
<Label htmlFor="name-input">Name</Label>
<Input type="text" id="name-input" name="name-input" placeholder="name" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="surname-input">Surname</Label>
<Input type="text" id="surname-input" name="surname-input" placeholder="surname" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="email-input">E-mail</Label>
<Input type="email" id="email-input" name="email-input" placeholder="email" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="phone-input">Phone</Label>
<Input type="text" id="phone-input" name="phone-input" placeholder="phone" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="address-input">Address</Label>
<Input type="text" id="address-input" name="address-input" placeholder="address" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="password-input">Password</Label>
<Input type="password" id="password-input" name="password-input" placeholder="password" />
</Container>

<Container className="flex-col items-start">
<Label htmlFor="password-repeat-input">Repeat Password</Label>
<Input type="password" id="password-repeat-input" name="password-repeat-input" placeholder="repeat password" />
</Container>

<Button type="submit">Register</Button>
</Form>

<Link onClick={handleLoginClick}>Login</Link>
</main>
)
}
4 changes: 2 additions & 2 deletions staff/lucas-orts/project/cor/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ const product = new Schema({
type: {
type: String
},
minprize: {
minprice: {
type: Number,
required: true
},
maxprize: {
maxprice: {
type: Number,
required: true
},
Expand Down
10 changes: 5 additions & 5 deletions staff/lucas-orts/project/cor/logic/createProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { validate, errors } from 'com'

const { NotFoundError, SystemError } = errors

export default (userId, name, type, minprize, maxprize, image) => {
export default (userId, name, type, minprice, maxprice, image) => {
validate.string(userId, 'userId')
validate.string(name, 'name')
validate.string(type, 'type')
validate.number(minprize, 'minprize')
validate.number(maxprize, 'maxprize')
validate.number(minprice, 'minprice')
validate.number(maxprice, 'maxprice')
validate.url(image, 'image')
return User.findById(userId).lean()
.catch(error => { throw new SystemError(error.message) })
Expand All @@ -20,8 +20,8 @@ export default (userId, name, type, minprize, maxprize, image) => {
farmer: userId,
name,
type,
minprize,
maxprize,
minprice,
maxprice,
image
})
.catch(error => { throw new SystemError(error.message) })
Expand Down
Loading

0 comments on commit 46ff525

Please sign in to comment.