diff --git a/api/server/plugins/walk/lib/index.js b/api/server/plugins/walk/lib/index.js index 664c285c2..0904f577b 100644 --- a/api/server/plugins/walk/lib/index.js +++ b/api/server/plugins/walk/lib/index.js @@ -1,5 +1,5 @@ const routes = require('../../../lib/routes'); -const files = require('../../../../../app/src/lib/filesystem'); +const files = require('../../../../../app/src/lib/filesystems'); const validation = require('../../../lib/validation'); const routeWalkPath = { diff --git a/app/.eslintrc.js b/app/.eslintrc.js index 5b5bc5368..3267ce9bd 100644 --- a/app/.eslintrc.js +++ b/app/.eslintrc.js @@ -6,6 +6,7 @@ module.exports = { semi: [2, 'never'], // remove semicolons 'react/prop-types': 'off', 'react/react-in-jsx-scope': 'off', // Next.js magically includes + 'react/jsx-one-expression-per-line': 'off', // too vertical }, env: { jest: true, diff --git a/app/__tests__/__snapshots__/snapshot.jsx.snap b/app/__tests__/__snapshots__/snapshot.jsx.snap index 325b22eec..ab784faf5 100644 --- a/app/__tests__/__snapshots__/snapshot.jsx.snap +++ b/app/__tests__/__snapshots__/snapshot.jsx.snap @@ -5,6 +5,7 @@ exports[`renders homepage unchanged 1`] = ` className="container" >
+

diff --git a/app/jest.config.js b/app/jest.config.js index 1e1bada46..1d7ad51b0 100644 --- a/app/jest.config.js +++ b/app/jest.config.js @@ -5,6 +5,7 @@ module.exports = { '!**/node_modules/**', ], setupFilesAfterEnv: ['/setupTests.js'], + testEnvironment: 'jsdom', testMatch: ['**/__tests__/**/*.[jt]s?(x)', '!**/__fixtures__/**'], testPathIgnorePatterns: ['/node_modules/', '/.next/'], transform: { diff --git a/app/pages/api/admin/filesystem/__tests__/index.test.js b/app/pages/api/admin/filesystems/__tests__/index.test.js similarity index 100% rename from app/pages/api/admin/filesystem/__tests__/index.test.js rename to app/pages/api/admin/filesystems/__tests__/index.test.js diff --git a/app/pages/api/admin/filesystem/index.js b/app/pages/api/admin/filesystems/index.js similarity index 88% rename from app/pages/api/admin/filesystem/index.js rename to app/pages/api/admin/filesystems/index.js index 9277a4864..f967c6633 100644 --- a/app/pages/api/admin/filesystem/index.js +++ b/app/pages/api/admin/filesystems/index.js @@ -1,4 +1,4 @@ -import local from '../../../../src/lib/filesystem' +import local from '../../../../src/lib/filesystems' const errorSchema = (message) => ({ files: [], error: { message } }) diff --git a/app/pages/api/galleries/__tests__/index.test.js b/app/pages/api/galleries/__tests__/index.test.js index 41e38ae97..9af0f8894 100644 --- a/app/pages/api/galleries/__tests__/index.test.js +++ b/app/pages/api/galleries/__tests__/index.test.js @@ -25,6 +25,7 @@ describe('Galleries API', () => { const result = await response.json() expect(response.status).toBe(405) + expect(result.error.message.toLowerCase()).toContain('not allowed') expect(result.galleries.length).toBe(0) expect(result.galleries.includes('demo')).toBeFalsy() diff --git a/app/pages/api/galleries/index.js b/app/pages/api/galleries/index.js index 762fd7274..323f5d62f 100644 --- a/app/pages/api/galleries/index.js +++ b/app/pages/api/galleries/index.js @@ -1,14 +1,12 @@ import local from '../../../src/lib/galleries' -const errorSchema = (message) => ({ galleries: [], error: { message } }) - export default async function handler({ method }, res) { switch (method) { case 'GET': { - const out = await local.get(errorSchema) + const out = await local.get(true) return res.status(out.status).json(out.body) } default: - return res.status(405).json(errorSchema(`Method ${method} Not Allowed`)) + return res.status(405).json(local.errorSchema(`Method ${method} Not Allowed`)) } } diff --git a/app/pages/index.jsx b/app/pages/index.jsx index 238e37113..ffd5d8ab6 100644 --- a/app/pages/index.jsx +++ b/app/pages/index.jsx @@ -1,7 +1,15 @@ import Head from 'next/head' + import styles from './index.module.css' +import { get as getGalleries } from '../src/lib/galleries' + +export async function getStaticProps() { + return { + props: await getGalleries(), + } +} -const Home = () => ( +const Home = ({ galleries }) => (
Create Next App @@ -9,6 +17,7 @@ const Home = () => (
+

Galleries {JSON.stringify(galleries)}

Welcome to {' '} diff --git a/app/src/lib/filesystem.js b/app/src/lib/filesystems.js similarity index 100% rename from app/src/lib/filesystem.js rename to app/src/lib/filesystems.js diff --git a/app/src/lib/galleries.js b/app/src/lib/galleries.js index f42ddb353..efb6a37a6 100644 --- a/app/src/lib/galleries.js +++ b/app/src/lib/galleries.js @@ -2,19 +2,40 @@ const fsCallback = require('fs') const fs = fsCallback.promises -async function get(errorSchema = (msg) => msg) { +const errorSchema = (message) => { + const out = { galleries: [] } + if (!message) return out + return { ...out, error: { message } } +} + +/** + * Get Galleries from local filesystem + * @param {boolean} returnEnvelope will enable a return value with HTTP status code and body + * @returns {Promise} galleries + */ +async function get(returnEnvelope = false) { try { const hasPrefix = (content) => content.isDirectory() const namesOnly = (content) => content.name const contents = await fs.readdir('../public/galleries', { withFileTypes: true }) + const galleries = contents.filter(hasPrefix).map(namesOnly) - return { body: { galleries: contents.filter(hasPrefix).map(namesOnly) }, status: 200 } + if (returnEnvelope) { + return { body: { galleries }, status: 200 } + } + + return { galleries } } catch (e) { - return { body: errorSchema('No galleries are found'), status: 404 } + if (returnEnvelope) { + return { body: errorSchema('No galleries are found'), status: 404 } + } + + return errorSchema() } } module.exports = { get, + errorSchema, }