2020-11-19 17:11:23 +03:00
|
|
|
import React, { useState } from 'react'
|
|
|
|
import { Link, useHistory } from 'react-router-dom'
|
|
|
|
|
|
|
|
import useAuth from '@wasp/auth/useAuth.js'
|
|
|
|
import logout from '@wasp/auth/logout.js'
|
|
|
|
import updateUser from '@wasp/actions/updateUser'
|
|
|
|
import getUser from '@wasp/queries/getUser'
|
2020-11-20 20:22:49 +03:00
|
|
|
import getArticlesByUser from '@wasp/queries/getArticlesByUser'
|
2020-11-19 17:11:23 +03:00
|
|
|
import { useQuery } from '@wasp/queries'
|
|
|
|
|
2020-11-19 17:32:37 +03:00
|
|
|
import Navbar from './Navbar'
|
|
|
|
import smileyImageUrl from './smiley.jpg'
|
|
|
|
|
2020-11-19 17:11:23 +03:00
|
|
|
const UserProfilePage = (props) => {
|
|
|
|
const history = useHistory()
|
|
|
|
|
|
|
|
const username = props.match.params.username
|
|
|
|
const { data: user, error: userError } = useQuery(getUser, { username })
|
|
|
|
|
2020-11-19 17:32:37 +03:00
|
|
|
// NOTE: use-query will retry multiple times in case of error, making user
|
|
|
|
// wait relatively long before giving up on for example 404 and returning error,
|
|
|
|
// while on the other hand we would probably like it to give up on 404 immediatelly!
|
|
|
|
// Should we modify react-query default settings to not do any retrying?
|
2020-11-19 17:11:23 +03:00
|
|
|
if (userError) {
|
|
|
|
console.log(userError)
|
|
|
|
history.push("/")
|
|
|
|
}
|
|
|
|
|
2020-11-19 17:32:37 +03:00
|
|
|
// TODO: List My Articles
|
|
|
|
// TODO: List Favorited Articles
|
2020-11-19 17:11:23 +03:00
|
|
|
|
|
|
|
return user ? (
|
|
|
|
<div>
|
2020-11-19 17:32:37 +03:00
|
|
|
<Navbar />
|
|
|
|
|
|
|
|
<img src={user.profilePictureUrl || smileyImageUrl} />
|
2020-11-19 17:11:23 +03:00
|
|
|
<p> { user.username } </p>
|
|
|
|
<p> { user.bio } </p>
|
|
|
|
<div>
|
2020-11-20 20:22:49 +03:00
|
|
|
{ /* TODO: Show this link only if user is logged in. */ }
|
2020-11-19 17:11:23 +03:00
|
|
|
<Link to='/settings'>Edit Profile Settings</Link>
|
|
|
|
</div>
|
2020-11-20 20:22:49 +03:00
|
|
|
|
|
|
|
<Articles user={user} />
|
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
}
|
|
|
|
|
|
|
|
const Articles = (props) => {
|
|
|
|
// TODO: Should I have pagination here, probably I should?
|
|
|
|
|
|
|
|
const { data: articles } = useQuery(getArticlesByUser, { username: props.user.username })
|
|
|
|
|
|
|
|
return articles ? (
|
|
|
|
<div>
|
|
|
|
{ articles.map(article => <Article article={article} key={article.id} />) }
|
2020-11-19 17:11:23 +03:00
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
}
|
|
|
|
|
2020-11-20 20:22:49 +03:00
|
|
|
const Article = (props) => {
|
|
|
|
const article = props.article
|
|
|
|
return (
|
|
|
|
<div>
|
2020-11-24 23:10:26 +03:00
|
|
|
<Link to={`/article/${article.slug}`}>
|
2020-11-20 20:22:49 +03:00
|
|
|
<h2> { article.title } </h2>
|
|
|
|
</Link>
|
|
|
|
<p> { article.description } </p>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-19 17:11:23 +03:00
|
|
|
export default UserProfilePage
|