diff --git a/src/client/app.js b/src/client/app.js index 74639193..5e6dd6d7 100644 --- a/src/client/app.js +++ b/src/client/app.js @@ -116,16 +116,6 @@ app.config(($locationProvider, $routeProvider) => { } } }, - { - name: '/gallery', - template: '', - title: 'Работы учащихся', - resolve: { - list: api => api.album - .list() - .then(i => i.data) - } - }, { name: '/awards', template: '', diff --git a/src/routes/albums.js b/src/routes/albums.js index 686f59b4..5fc8542b 100644 --- a/src/routes/albums.js +++ b/src/routes/albums.js @@ -1,29 +1,9 @@ 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 {c} = require('../core/db') - -async function findAlbum(id) { - return c('albums').findOne({id}) -} - -async function processAlbum(album) { - const url = `/album/${album.id}` - - const previewFromImage = imgs => imgs.length ? imgs[0] : null - const previewImageId = album.preview ? album.preview : previewFromImage(album.images) - if (previewImageId) { - const image = await c('images').findOne({_id: previewImageId}) - album.preview = image.data.medium - } - - return { - ...album, - url, - } -} function getMeta(album) { return { @@ -35,11 +15,9 @@ function getMeta(album) { function getAlbum() { return get('/album/:id', async (ctx, id) => { const path = getPathWithNoTrailingSlash(ctx.path) - const record = await findAlbum(id) - - if (record) { - const album = await processAlbum(record) + const album = await Album.findById(id) + if (album) { const Component = (
{ - let query = {}; - let albums = await c('albums') - .find(query) - .toArray(); - - ctx.body = await Promise.all(albums - .sort((a, b) => { - const da = a.date.getTime(); - const db = b.date.getTime(); - return db - da; - }) - .map(processAlbum) - ); - } - )); +function getMeta() { + return { + title: 'Галерея', + description: 'Галерея работ учащихся Шлиссельбургской Детской Художественной Школы' + } } -const processAlbum = async album => { - album.url = `/album/${album.id}`; +const GItem = ({album}) => ( +
+ +

{album.title}

+ {album.title}/ +
+
+) - const previewFromImage = imgs => imgs.length ? imgs[0] : null; - const previewImageId = album.preview ? album.preview : previewFromImage(album.images); - if (previewImageId) { - const image = await c('images').findOne({_id: previewImageId}); - album.preview = image.data.medium; - } +const ACollection = ({title, albums}) => ( +
+
{title}
+
{ + albums.map((album, index) => ( + + )) + }
+
+) - return album; -}; +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]) => ( + + )) + } +
+
+ ) + + ctx.type = 'text/html' + ctx.body = await render(path, Component, getMeta()) + } else { + ctx.status = 404 + } + }) +} + +exports.getGallery = getGallery diff --git a/src/routes/index.js b/src/routes/index.js index b81bfd25..7f77c529 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -7,7 +7,6 @@ import {index as indexFile} from '../config' import sitemap from './sitemap' import schedule from './schedule' -import gallery from './gallery' import documents from './documents' import teachers from './teachers' import pages from './pages' @@ -16,7 +15,6 @@ export function routes(store) { return compose([ sitemap(), schedule(), - gallery(), documents(store), teachers(), pages(), diff --git a/src/server.js b/src/server.js index 86e4ebbc..88dfa192 100644 --- a/src/server.js +++ b/src/server.js @@ -23,6 +23,7 @@ const error = require('./middlewares/error') const home = require('./routes/home') const article = require('./routes/articles') const albums = require('./routes/albums') +const gallery = require('./routes/gallery') const dirPublic = path.join(__dirname, '../public') const dirTemplates = path.join(__dirname, 'templates') @@ -53,6 +54,7 @@ export default function (store) { app.use(error.notFound(config.view404)) app.use(home.getHome(5)) + app.use(gallery.getGallery()) app.use(article.getArticles(5)) app.use(article.getArticle()) app.use(albums.getAlbum()) diff --git a/src/style/components/album-collection.scss b/src/style/components/album-collection.scss index 92321171..bcdcedd5 100644 --- a/src/style/components/album-collection.scss +++ b/src/style/components/album-collection.scss @@ -7,9 +7,12 @@ position: relative; justify-content: center; align-items:flex-start; + + margin: 0 0 $double-margin; } &__title{ - + text-align: center; + margin: $single-margin 0; } }