From be82458c0b345220691363862e6135f9b1440c8e Mon Sep 17 00:00:00 2001 From: Ian Mancini Date: Fri, 11 Dec 2020 01:42:38 -0300 Subject: [PATCH] Add /notas/[slug] page --- src/lib/api.ts | 16 +++++- src/pages/notas/[slug].tsx | 64 ++++++++++++++++++++++++ src/pages/{notas.tsx => notas/index.tsx} | 34 +++++++++---- src/pages/work/[slug].tsx | 4 +- 4 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 src/pages/notas/[slug].tsx rename src/pages/{notas.tsx => notas/index.tsx} (70%) diff --git a/src/lib/api.ts b/src/lib/api.ts index 2e8f97a..1d28f89 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -120,6 +120,16 @@ export async function getWorkBySlug(slug: string) { }) } +export async function getNotaBySlug(slug: string) { + return client.getItems('notas', { + filter: { + slug: { + eq: slug, + }, + }, + }) +} + export interface IAbout { texto_about: string texto_team: string @@ -211,8 +221,10 @@ export async function getAllNotasWithImages(): Promise { return notasWithImages } -export async function getNotaWithImage(id: number): Promise { - const nota = await getNotaById(id) +export async function getNotaWithImage(slug: string): Promise { + const _nota = await getNotaBySlug(slug) + const nota = _nota.data[0] + const notaWithImage = await extractUrlAndDownloadImage(nota, 'imagen', notasImagesDir) return notaWithImage } diff --git a/src/pages/notas/[slug].tsx b/src/pages/notas/[slug].tsx new file mode 100644 index 0000000..6d461a9 --- /dev/null +++ b/src/pages/notas/[slug].tsx @@ -0,0 +1,64 @@ +import { Spacer, Stack, Heading, Text, Box, Flex } from '@chakra-ui/react' + +import ResponsiveImage from 'components/ResponsiveImage' +import SEO from 'components/SEO' + +import { getAllNotasWithImages, getNotaWithImage, INotaWithImage, login } from 'lib/api' +import ReactMarkdown from 'react-markdown' + +const NotePage: React.FC<{ data: INotaWithImage }> = ({ data }) => { + console.log(data) + return ( + <> + + + {data.titulo} + + {data.tiempo_lectura} min de lectura + + + {data.tags.map((tag) => ( + {tag} + ))} + + + {data.cuerpo} + + + + + ) +} + +export default NotePage + +export async function getStaticPaths() { + await login() + const notas = await getAllNotasWithImages() + + return { + paths: notas.map((nota) => `/notas/${nota.slug}`), + fallback: false, + } +} + +export async function getStaticProps({ params }) { + await login() + + const notaWithImage = await getNotaWithImage(params.slug) + + return { props: { data: notaWithImage } } +} diff --git a/src/pages/notas.tsx b/src/pages/notas/index.tsx similarity index 70% rename from src/pages/notas.tsx rename to src/pages/notas/index.tsx index 0a4d58f..510517a 100644 --- a/src/pages/notas.tsx +++ b/src/pages/notas/index.tsx @@ -1,16 +1,16 @@ import { Link as ChakraLink, Stack, Heading, Text, Box, Flex } from '@chakra-ui/react' import Link from 'next/link' -import ResponsiveImage from '../components/ResponsiveImage' -import SEO from '../components/SEO' +import ResponsiveImage from 'components/ResponsiveImage' +import SEO from 'components/SEO' -import { getAbout, getAllNotasWithImages, INotaWithImage, login } from '../lib/api' +import { getAbout, getAllNotasWithImages, INotaWithImage, login } from 'lib/api' const Notas: React.FC<{ notas: INotaWithImage[] }> = ({ notas }) => { return ( <> - + Notas y pensamientos @@ -34,26 +34,38 @@ const Notas: React.FC<{ notas: INotaWithImage[] }> = ({ notas }) => { alt={`Imagen de ${nota.titulo}`} url={`notas/${nota.imagen_file}`} /> - + {nota.titulo} {nota.tiempo_lectura} min de lectura - - {nota.tags.map((tag) => ( - {tag} - ))} - + + {nota.tags.map((tag) => `${tag} `)} + + diff --git a/src/pages/work/[slug].tsx b/src/pages/work/[slug].tsx index 237070a..1b3d83c 100644 --- a/src/pages/work/[slug].tsx +++ b/src/pages/work/[slug].tsx @@ -5,7 +5,7 @@ import SEO from 'components/SEO' import { getAllWorks, getWorkWithImages, IWorkWithImages, login } from 'lib/api' -const Work: React.FC<{ data: IWorkWithImages }> = ({ data }) => { +const WorkPage: React.FC<{ data: IWorkWithImages }> = ({ data }) => { return ( <> @@ -84,7 +84,7 @@ const Work: React.FC<{ data: IWorkWithImages }> = ({ data }) => { ) } -export default Work +export default WorkPage export async function getStaticPaths() { await login()