const React = require('react')
const Article = require('../components/Article')
const Album = require('../core/Album')
const {render} = require('../lib/render')
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
const {get} = require('koa-route')
const previewUrl = preview => preview
? preview.url
: null
function getMeta() {
return {
title: 'Галерея',
description: 'Галерея работ учащихся Шлиссельбургской Детской Художественной Школы'
}
}
const GItem = ({album}) => (
)
const ACollection = ({title, albums}) => (
{title}
{
albums.map((album, index) => (
))
}
)
function splitBy(key) {
return items => {
return items.reduce((acc, item) => {
const name = key(item)
const items = acc.get(name) || []
return acc.set(key(item), [...items, item])
}, new Map())
}
}
function getGallery() {
return get('/gallery', async ctx => {
const path = getPathWithNoTrailingSlash(ctx.path)
const query = {}
const albums = await Album.find(query, {sort: {date: -1}})
if (albums.length) {
const meta = getMeta()
const albumsByYear = splitBy(a => a.date.getFullYear())(albums)
const Component = (
{
[...albumsByYear.entries()].map(([year, albums], index) => (
))
}
)
ctx.type = 'text/html'
ctx.body = await render(path, Component, getMeta())
} else {
ctx.status = 404
}
})
}
exports.getGallery = getGallery