From 2ca613e9169ab523120d3d01ab75c8504079a451 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Sat, 15 Feb 2020 05:28:06 +0300 Subject: [PATCH] album page update --- pages/album/[album].jsx | 43 -------------------- pages/album/[slug].tsx | 87 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 43 deletions(-) delete mode 100644 pages/album/[album].jsx create mode 100644 pages/album/[slug].tsx diff --git a/pages/album/[album].jsx b/pages/album/[album].jsx deleted file mode 100644 index f61c4797..00000000 --- a/pages/album/[album].jsx +++ /dev/null @@ -1,43 +0,0 @@ -import React from 'react' -import Head from 'next/head' -import { App } from '../../src/components/App' -import Article from '../../src/components/Article' -import Html from '../../src/components/Html' -import menuModel from '../../src/models/menu' -import { buildMenu } from '../../src/lib/menu' -import { createApiUrl, requestGet, wrapInitialProps } from '../../src/next-lib' - -const Page = (props) => ( - - - {props.album.title} - -
- -
-
-) - -Page.getInitialProps = wrapInitialProps(async (ctx) => { - const pageUrl = '/gallery' - const id = ctx.query.album - const album = await requestGet(createApiUrl(`/api/albums/${id}`), {}) - - return { - album, - pageUrl, - } -}) - -export default Page \ No newline at end of file diff --git a/pages/album/[slug].tsx b/pages/album/[slug].tsx new file mode 100644 index 00000000..92ac727b --- /dev/null +++ b/pages/album/[slug].tsx @@ -0,0 +1,87 @@ +import Head from 'next/head' +import { App } from 'src/components/App' +import Article from 'src/components/Article' +import Html from 'src/components/Html' +import menuModel from 'src/models/menu' +import { buildMenu } from 'src/lib/menu' +import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' +import { NextPage } from 'next' + +type Props = { + pageUrl: string + data: any //(album) +} + +const Page: NextPage = props => { + if (!props?.data) { + console.log('kek error', props) + + return null + } + + return ( + + + {props.data.title} + + +
+ +
+
+ ) +} + +export const unstable_getStaticProps = async (ctx: any) => { + const pageUrl = '/gallery' + // const id = ctx.query.slug + const id = ctx.params.slug + const data = await requestGet(createApiUrl(`/api/albums/${id}`), null) + + console.log('call album unstable_getStaticProps', data) + if (!data) { + throw new Error(`kek ${id}`) + } + + return { + props: { + data, + pageUrl, + } + } +} + +export const unstable_getStaticPaths = async () => { + console.log('call album unstable_getStaticPaths') + + const urls = await requestGet>(createApiUrl(`/api/albums/urls`), null) + if (!urls) { + return null + } + + return { + paths: urls.items + .map(x => x.replace('/album/', '')) + .map(slug => { + return { + params: { + slug, + }, + } + }), + } +} + +export default Page