diff --git a/lib/utils.js b/lib/utils.js index 3152982..0bb14db 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -2,18 +2,32 @@ import path from "path"; import fs from "fs"; function getContentDirectory(name) { - return path.join(process.cwd(), "public", name) + return path.join(process.cwd(), "public", name); } - function getAllPhotos() { - const path = "img/Diaporama" - const contentDirectory = getContentDirectory(path); +const photoDir = "img/Diaporama"; + +function photoPath(fileName) { + return "/" + photoDir + "/" + fileName; +} + +function getAllPhotos() { + const contentDirectory = getContentDirectory(photoDir); const fileNames = fs.readdirSync(contentDirectory); console.log(`gACD: total pictures: ${fileNames.length}`); - return fileNames.map(f => path + "/" + f); + return fileNames.map((f, i) => { + return { + id: i + 1, // Photos are 1-indexed + path: photoPath(f), + }; + }); +} + +function getPhotoById(id) { + const contentDirectory = getContentDirectory(photoDir); + const fileNames = fs.readdirSync(contentDirectory); + return { id: id, path: photoPath(fileNames[id - 1]) }; // Photos are 1-indexed } -export { - getAllPhotos -}; +export { getAllPhotos, getPhotoById }; diff --git a/pages/index.js b/pages/index.js index 2ec5594..5a587de 100644 --- a/pages/index.js +++ b/pages/index.js @@ -5,10 +5,10 @@ import { Box, Container, Text, Wrap, WrapItem } from "@chakra-ui/react"; import Image from "next/image"; import { getAllPhotos } from "../lib/utils"; import { Img } from "@chakra-ui/react"; +import Link from "next/link"; -export async function getServerSideProps() { - const data = await getAllPhotos(); - console.log(data); +export async function getStaticProps() { + let data = await getAllPhotos(); return { props: { data, @@ -17,7 +17,6 @@ export async function getServerSideProps() { } export default function Home({ data }) { - console.log(data); const [photos, setPhotos] = useState(data); // TODO: Fallback with random flowers? fallbackSrc return ( @@ -27,7 +26,12 @@ export default function Home({ data }) { - me + me - {photos.map((path) => ( + {photos.map((photo) => ( - {/* + {/**/} - + + ))}