Refactor saved and extract SavedHead
This commit is contained in:
parent
73de22cbbc
commit
01af39fdfe
2 changed files with 131 additions and 78 deletions
25
components/SavedHead/index.tsx
Normal file
25
components/SavedHead/index.tsx
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
import React from 'react'
|
||||||
|
import Head from 'next/head'
|
||||||
|
import { useTranslation } from 'next-i18next'
|
||||||
|
|
||||||
|
const SavedHead = () => {
|
||||||
|
// Import translations
|
||||||
|
const { t } = useTranslation('common')
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Head>
|
||||||
|
<title>{t('page.titles.saved')}</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
|
||||||
|
<meta property="og:title" content={t('page.titles.saved')} />
|
||||||
|
<meta property="og:url" content="https://app.granblue.team/saved" />
|
||||||
|
<meta property="og:type" content="website" />
|
||||||
|
|
||||||
|
<meta name="twitter:card" content="summary_large_image" />
|
||||||
|
<meta property="twitter:domain" content="app.granblue.team" />
|
||||||
|
<meta name="twitter:title" content={t('page.titles.saved')} />
|
||||||
|
</Head>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default SavedHead
|
||||||
184
pages/saved.tsx
184
pages/saved.tsx
|
|
@ -1,11 +1,8 @@
|
||||||
import React, { useCallback, useEffect, useState } from 'react'
|
import React, { useCallback, useEffect, useState } from 'react'
|
||||||
import Head from 'next/head'
|
import InfiniteScroll from 'react-infinite-scroll-component'
|
||||||
|
|
||||||
import { queryTypes, useQueryState } from 'next-usequerystate'
|
import { queryTypes, useQueryState } from 'next-usequerystate'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import { useTranslation } from 'next-i18next'
|
import { useTranslation } from 'next-i18next'
|
||||||
import InfiniteScroll from 'react-infinite-scroll-component'
|
|
||||||
|
|
||||||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
|
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
|
||||||
import clonedeep from 'lodash.clonedeep'
|
import clonedeep from 'lodash.clonedeep'
|
||||||
|
|
||||||
|
|
@ -18,24 +15,35 @@ import useDidMountEffect from '~utils/useDidMountEffect'
|
||||||
import { appState } from '~utils/appState'
|
import { appState } from '~utils/appState'
|
||||||
import { elements, allElement } from '~data/elements'
|
import { elements, allElement } from '~data/elements'
|
||||||
import { emptyPaginationObject } from '~utils/emptyStates'
|
import { emptyPaginationObject } from '~utils/emptyStates'
|
||||||
import { printError } from '~utils/reportError'
|
|
||||||
|
|
||||||
|
import ErrorSection from '~components/ErrorSection'
|
||||||
import GridRep from '~components/GridRep'
|
import GridRep from '~components/GridRep'
|
||||||
import GridRepCollection from '~components/GridRepCollection'
|
import GridRepCollection from '~components/GridRepCollection'
|
||||||
import FilterBar from '~components/FilterBar'
|
import FilterBar from '~components/FilterBar'
|
||||||
|
import SavedHead from '~components/SavedHead'
|
||||||
|
|
||||||
|
import type { AxiosError } from 'axios'
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import type { FilterObject, PaginationObject } from '~types'
|
import type {
|
||||||
|
FilterObject,
|
||||||
|
PageContextObj,
|
||||||
|
PaginationObject,
|
||||||
|
ResponseStatus,
|
||||||
|
} from '~types'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
teams?: Party[]
|
context?: PageContextObj
|
||||||
meta: PaginationObject
|
|
||||||
raids: Raid[]
|
|
||||||
sortedRaids: Raid[][]
|
|
||||||
version: AppUpdate
|
version: AppUpdate
|
||||||
|
error: boolean
|
||||||
|
status?: ResponseStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
const SavedRoute: React.FC<Props> = (props: Props) => {
|
const SavedRoute: React.FC<Props> = ({
|
||||||
|
context,
|
||||||
|
version,
|
||||||
|
error,
|
||||||
|
status,
|
||||||
|
}: Props) => {
|
||||||
// Set up router
|
// Set up router
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
|
|
@ -97,11 +105,11 @@ const SavedRoute: React.FC<Props> = (props: Props) => {
|
||||||
|
|
||||||
// Set the initial parties from props
|
// Set the initial parties from props
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (props.teams) {
|
if (context && context.teams && context.pagination) {
|
||||||
setTotalPages(props.meta.totalPages)
|
setTotalPages(context.pagination.totalPages)
|
||||||
setRecordCount(props.meta.count)
|
setRecordCount(context.pagination.count)
|
||||||
replaceResults(props.meta.count, props.teams)
|
replaceResults(context.pagination.count, context.teams)
|
||||||
appState.version = props.version
|
appState.version = version
|
||||||
}
|
}
|
||||||
setCurrentPage(1)
|
setCurrentPage(1)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
@ -269,6 +277,16 @@ const SavedRoute: React.FC<Props> = (props: Props) => {
|
||||||
router.push(`/p/${shortcode}`)
|
router.push(`/p/${shortcode}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Methods: Page component rendering
|
||||||
|
function pageHead() {
|
||||||
|
if (context && context.user) return <SavedHead />
|
||||||
|
}
|
||||||
|
|
||||||
|
function pageError() {
|
||||||
|
if (status) return <ErrorSection status={status} />
|
||||||
|
else return <div />
|
||||||
|
}
|
||||||
|
|
||||||
function renderParties() {
|
function renderParties() {
|
||||||
return parties.map((party, i) => {
|
return parties.map((party, i) => {
|
||||||
return (
|
return (
|
||||||
|
|
@ -291,55 +309,45 @@ const SavedRoute: React.FC<Props> = (props: Props) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
if (context) {
|
||||||
<div id="Teams">
|
return (
|
||||||
<Head>
|
<div id="Teams">
|
||||||
<title>{t('page.titles.saved')}</title>
|
{pageHead()}
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<FilterBar
|
||||||
|
onFilter={receiveFilters}
|
||||||
<meta property="og:title" content={t('page.titles.saved')} />
|
scrolled={scrolled}
|
||||||
<meta property="og:url" content="https://app.granblue.team/saved" />
|
element={element}
|
||||||
<meta property="og:type" content="website" />
|
raidSlug={raidSlug ? raidSlug : undefined}
|
||||||
|
recency={recency}
|
||||||
<meta name="twitter:card" content="summary_large_image" />
|
|
||||||
<meta property="twitter:domain" content="app.granblue.team" />
|
|
||||||
<meta name="twitter:title" content={t('page.titles.saved')} />
|
|
||||||
</Head>
|
|
||||||
|
|
||||||
<FilterBar
|
|
||||||
onFilter={receiveFilters}
|
|
||||||
scrolled={scrolled}
|
|
||||||
element={element}
|
|
||||||
raidSlug={raidSlug ? raidSlug : undefined}
|
|
||||||
recency={recency}
|
|
||||||
>
|
|
||||||
<h1>{t('saved.title')}</h1>
|
|
||||||
</FilterBar>
|
|
||||||
|
|
||||||
<section>
|
|
||||||
<InfiniteScroll
|
|
||||||
dataLength={parties && parties.length > 0 ? parties.length : 0}
|
|
||||||
next={() => setCurrentPage(currentPage + 1)}
|
|
||||||
hasMore={totalPages > currentPage}
|
|
||||||
loader={
|
|
||||||
<div id="NotFound">
|
|
||||||
<h2>Loading...</h2>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
>
|
>
|
||||||
<GridRepCollection>{renderParties()}</GridRepCollection>
|
<h1>{t('saved.title')}</h1>
|
||||||
</InfiniteScroll>
|
</FilterBar>
|
||||||
|
|
||||||
{parties.length == 0 ? (
|
<section>
|
||||||
<div id="NotFound">
|
<InfiniteScroll
|
||||||
<h2>{t('saved.not_found')}</h2>
|
dataLength={parties && parties.length > 0 ? parties.length : 0}
|
||||||
</div>
|
next={() => setCurrentPage(currentPage + 1)}
|
||||||
) : (
|
hasMore={totalPages > currentPage}
|
||||||
''
|
loader={
|
||||||
)}
|
<div id="NotFound">
|
||||||
</section>
|
<h2>Loading...</h2>
|
||||||
</div>
|
</div>
|
||||||
)
|
}
|
||||||
|
>
|
||||||
|
<GridRepCollection>{renderParties()}</GridRepCollection>
|
||||||
|
</InfiniteScroll>
|
||||||
|
|
||||||
|
{parties.length == 0 ? (
|
||||||
|
<div id="NotFound">
|
||||||
|
<h2>{t('saved.not_found')}</h2>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
''
|
||||||
|
)}
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
} else return pageError()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getServerSidePaths = async () => {
|
export const getServerSidePaths = async () => {
|
||||||
|
|
@ -357,10 +365,10 @@ export const getServerSideProps = async ({ req, res, locale, query }: { req: Nex
|
||||||
// Set headers for server-side requests
|
// Set headers for server-side requests
|
||||||
setUserToken(req, res)
|
setUserToken(req, res)
|
||||||
|
|
||||||
try {
|
// Fetch latest version
|
||||||
// Fetch latest version
|
const version = await fetchLatestVersion()
|
||||||
const version = await fetchLatestVersion()
|
|
||||||
|
|
||||||
|
try {
|
||||||
// Fetch and organize raids
|
// Fetch and organize raids
|
||||||
let { raids, sortedRaids } = await api.endpoints.raids
|
let { raids, sortedRaids } = await api.endpoints.raids
|
||||||
.getAll()
|
.getAll()
|
||||||
|
|
@ -373,32 +381,52 @@ export const getServerSideProps = async ({ req, res, locale, query }: { req: Nex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up empty variables
|
// Set up empty variables
|
||||||
let teams: Party[] | null = null
|
let teams: Party[] | undefined = undefined
|
||||||
let meta: PaginationObject = emptyPaginationObject
|
let pagination: PaginationObject = emptyPaginationObject
|
||||||
|
|
||||||
// Fetch initial set of saved parties
|
// Fetch initial set of saved parties
|
||||||
const response = await api.savedTeams(params)
|
const response = await api.savedTeams(params)
|
||||||
|
|
||||||
// Assign values to pass to props
|
// Assign values to pass to props
|
||||||
teams = response.data.results
|
teams = response.data.results
|
||||||
meta.count = response.data.meta.count
|
pagination.count = response.data.meta.count
|
||||||
meta.totalPages = response.data.meta.total_pages
|
pagination.totalPages = response.data.meta.total_pages
|
||||||
meta.perPage = response.data.meta.per_page
|
pagination.perPage = response.data.meta.per_page
|
||||||
|
|
||||||
|
// Consolidate data into context object
|
||||||
|
const context: PageContextObj = {
|
||||||
|
teams: teams,
|
||||||
|
raids: raids,
|
||||||
|
sortedRaids: sortedRaids,
|
||||||
|
pagination: pagination,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pass to the page component as props
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
teams: teams,
|
context: context,
|
||||||
meta: meta,
|
|
||||||
raids: raids,
|
|
||||||
sortedRaids: sortedRaids,
|
|
||||||
version: version,
|
version: version,
|
||||||
|
error: false,
|
||||||
...(await serverSideTranslations(locale, ['common', 'roadmap'])),
|
...(await serverSideTranslations(locale, ['common', 'roadmap'])),
|
||||||
// Will be passed to the page component as props
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
printError(error, 'axios')
|
// Extract the underlying Axios error
|
||||||
|
const axiosError = error as AxiosError
|
||||||
|
const response = axiosError.response
|
||||||
|
|
||||||
|
// Pass to the page component as props
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
context: null,
|
||||||
|
error: true,
|
||||||
|
status: {
|
||||||
|
code: response?.status,
|
||||||
|
text: response?.statusText,
|
||||||
|
},
|
||||||
|
...(await serverSideTranslations(locale, ['common', 'roadmap'])),
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SavedRoute
|
export default SavedRoute
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue