/album is Server side renderable
This commit is contained in:
1 parent
c2a457eea7
commit
b829885b00
5 files changed
+70
-29
No files matched your search
@@ -126,19 +126,6 @@ app.config(($locationProvider, $routeProvider) => {
|
||||
.then(i => i.data)
|
||||
}
|
||||
},
|
||||
{
|
||||
name: '/album/:id',
|
||||
template: '<page-album album="$resolve.album"></page-album>',
|
||||
resolve: {
|
||||
album: ($route, api) => {
|
||||
let id = $route.current.params.id
|
||||
|
||||
return api.album
|
||||
.fetch(id)
|
||||
.then(i => i.data)
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
name: '/awards',
|
||||
template: '<page-awards items="$resolve.awards"></page-awards>',
|
||||
|
||||
+6
-3
@@ -45,9 +45,12 @@ export default [
|
||||
'color': 'pink'
|
||||
},
|
||||
{
|
||||
'text': 'Галерея',
|
||||
'url': '/gallery',
|
||||
'color': 'red'
|
||||
text: 'Галерея',
|
||||
url: '/gallery',
|
||||
color: 'red',
|
||||
items: [
|
||||
{url: '/album/*', text: ''},
|
||||
]
|
||||
},
|
||||
{
|
||||
'text': 'Документы',
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
const React = require('react')
|
||||
const Article = require('../components/Article')
|
||||
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 {
|
||||
title: album.title,
|
||||
description: album.title,
|
||||
}
|
||||
}
|
||||
|
||||
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 Component = (
|
||||
<div className="content content_thin">
|
||||
<Article
|
||||
title={album.title}
|
||||
date={album.date}
|
||||
data={album.post}
|
||||
shareable={true}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
|
||||
ctx.type = 'text/html'
|
||||
ctx.body = await render(path, Component, getMeta(album), {commentsEnabled: true})
|
||||
} else {
|
||||
ctx.status = 404
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
exports.getAlbum = getAlbum
|
||||
@@ -6,7 +6,6 @@ import {json} from './';
|
||||
export default function () {
|
||||
return compose([
|
||||
gallery(),
|
||||
album()
|
||||
]);
|
||||
};
|
||||
|
||||
@@ -30,18 +29,6 @@ function gallery() {
|
||||
));
|
||||
}
|
||||
|
||||
function album() {
|
||||
return route.get('/album/:id', json(
|
||||
async(ctx, id) => {
|
||||
let record = await c('albums').findOne({
|
||||
id: id
|
||||
});
|
||||
record = await processAlbum(record);
|
||||
ctx.body = record;
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
const processAlbum = async album => {
|
||||
album.url = `/album/${album.id}`;
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ const config = require('./config')
|
||||
const error = require('./middlewares/error')
|
||||
const home = require('./routes/home')
|
||||
const article = require('./routes/articles')
|
||||
const albums = require('./routes/albums')
|
||||
|
||||
const dirPublic = path.join(__dirname, '../public')
|
||||
const dirTemplates = path.join(__dirname, 'templates')
|
||||
@@ -54,6 +55,7 @@ export default function (store) {
|
||||
app.use(home.getHome(5))
|
||||
app.use(article.getArticles(5))
|
||||
app.use(article.getArticle())
|
||||
app.use(albums.getAlbum())
|
||||
app.use(routes(store))
|
||||
|
||||
return Server(app.callback())
|
||||
|
||||
Reference in new issue
Block a user