2020-11-28 00:52:38 +03:00
|
|
|
import React, { useState } from 'react'
|
2020-10-30 22:18:17 +03:00
|
|
|
import { Link } from 'react-router-dom'
|
2020-11-26 18:35:38 +03:00
|
|
|
import _ from 'lodash'
|
2020-10-30 18:28:50 +03:00
|
|
|
|
2020-11-17 13:49:10 +03:00
|
|
|
import useAuth from '@wasp/auth/useAuth.js'
|
2020-11-26 18:35:38 +03:00
|
|
|
import getTags from '@wasp/queries/getTags'
|
2020-11-27 20:49:54 +03:00
|
|
|
import getFollowedArticles from '@wasp/queries/getFollowedArticles'
|
|
|
|
import getAllArticles from '@wasp/queries/getAllArticles'
|
2020-11-26 18:35:38 +03:00
|
|
|
import { useQuery } from '@wasp/queries'
|
2020-11-17 13:49:10 +03:00
|
|
|
|
2020-11-19 17:32:37 +03:00
|
|
|
import Navbar from './Navbar'
|
2020-11-27 20:49:54 +03:00
|
|
|
import ArticleList from './ArticleList'
|
2020-11-19 17:32:37 +03:00
|
|
|
|
2020-10-30 18:28:50 +03:00
|
|
|
const MainPage = () => {
|
2020-11-27 20:49:54 +03:00
|
|
|
const { data: me } = useAuth()
|
2020-11-17 13:49:10 +03:00
|
|
|
|
2020-11-27 20:49:54 +03:00
|
|
|
const { data: followedArticles } = useQuery(getFollowedArticles)
|
2020-11-28 00:52:38 +03:00
|
|
|
|
|
|
|
const allArticlesPageSize = 1
|
|
|
|
// TODO: Make the page idx persistent in the URL as query param.
|
|
|
|
const [allArticlesPageIdx, setAllArticlesPageIdx] = useState(0)
|
|
|
|
const { data: allArticlesData } = useQuery(
|
|
|
|
getAllArticles,
|
|
|
|
{
|
|
|
|
skip: allArticlesPageIdx * allArticlesPageSize,
|
|
|
|
take: allArticlesPageSize
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const allArticles = allArticlesData?.articles
|
|
|
|
const allArticlesCount = allArticlesData?.count
|
|
|
|
const allArticlesPageCount = Math.trunc(allArticlesCount / allArticlesPageSize)
|
|
|
|
console.log(allArticles, allArticlesCount, allArticlesPageCount)
|
2020-11-26 18:35:38 +03:00
|
|
|
|
2020-10-30 22:18:17 +03:00
|
|
|
return (
|
|
|
|
<div>
|
2020-11-19 17:32:37 +03:00
|
|
|
<Navbar />
|
|
|
|
|
2020-11-26 18:35:38 +03:00
|
|
|
<Tags />
|
|
|
|
|
2020-11-27 20:49:54 +03:00
|
|
|
{ me && (
|
|
|
|
<div>
|
|
|
|
<h1> Your Feed </h1>
|
|
|
|
<ArticleList articles={followedArticles || []} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<h1> Global Feed </h1>
|
|
|
|
<ArticleList articles={allArticles || []} />
|
2020-11-28 00:52:38 +03:00
|
|
|
{ allArticlesPageCount > 0 && (
|
|
|
|
<div>
|
|
|
|
{ allArticlesPageIdx > 0 && (
|
|
|
|
<>
|
|
|
|
<button> 1 </button>
|
|
|
|
<button> < </button>
|
|
|
|
</>
|
|
|
|
) }
|
|
|
|
{ /* TODO: Make the current page number an input which user can change. */ }
|
|
|
|
{ allArticlesPageIdx + 1 }
|
|
|
|
{ allArticlesPageIdx < allArticlesPageCount - 1 && (
|
|
|
|
<>
|
|
|
|
<button> > </button>
|
|
|
|
<button> { allArticlesPageCount } </button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
) }
|
2020-11-27 20:49:54 +03:00
|
|
|
</div>
|
|
|
|
|
2020-10-30 22:18:17 +03:00
|
|
|
</div>
|
|
|
|
)
|
2020-10-30 18:28:50 +03:00
|
|
|
}
|
|
|
|
|
2020-11-26 18:35:38 +03:00
|
|
|
const Tags = () => {
|
|
|
|
const { data: tags } = useQuery(getTags)
|
|
|
|
|
|
|
|
if (!tags) return null
|
|
|
|
|
|
|
|
const popularTags = _.take(_.sortBy(tags, [t => -1 * t.numArticles]), 10)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
Popular tags: { popularTags.map(tag => (
|
|
|
|
<div>
|
|
|
|
{ tag.name } ({ tag.numArticles })
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:28:50 +03:00
|
|
|
export default MainPage
|