diff --git a/src/client/app.js b/src/client/app.js index a3017cc8..182be1af 100644 --- a/src/client/app.js +++ b/src/client/app.js @@ -83,48 +83,6 @@ app.config(($locationProvider, $routeProvider) => { })) let routers = [ - { - name: '/', - template: '', - title: 'Шлиссельбургская детская художественная школа' - }, - { - name: '/events/:page?', - template: '', - title: 'События Школы', - resolveAs: '$', - resolve: { - page: ($route, api) => { - let page = $route.current.params.page - if (!page) page = 1 - - return api.event - .list(page) - .then(i => i.data) - .then(i => { - let totalPages = Math.ceil(i.total / i.limit) - return { - data: i.data, - page: page, - totalPages: totalPages - } - }) - } - } - }, - { - name: '/article/:id', - template: '', - resolveAs: '$', - resolve: { - data: ($route, api) => { - let id = $route.current.params.id - if (!id) return null - return api.article.id(id) - .then(i => i.data) - } - } - }, { name: '/schedule/:period?/:semester?', template: '', diff --git a/src/components/ArticleList.js b/src/components/ArticleList.js index af3ebb56..303f75e4 100644 --- a/src/components/ArticleList.js +++ b/src/components/ArticleList.js @@ -3,7 +3,7 @@ const Paginator = require('./Paginator') const Article = require('./Article') const ArticleList = ({articles, nextPage, prevPage}) => ( -
+
{!prevPage ? null : ( )} diff --git a/src/models/menu.js b/src/models/menu.js index ebc9e1c7..ec855658 100644 --- a/src/models/menu.js +++ b/src/models/menu.js @@ -8,7 +8,7 @@ export default [ {url: '/articles/*', text: 'Статьи'}, {url: '/article/*', text: ''}, ], - }, , + }, { 'text': 'Расписание', 'url': '/schedule', diff --git a/src/routes/articles.js b/src/routes/articles.js index 9d267707..85c46cc0 100644 --- a/src/routes/articles.js +++ b/src/routes/articles.js @@ -47,6 +47,46 @@ async function findPinned(page) { .toArray() } +async function getArticleListComponent(path, page, pageSize) { + path = getPathWithNoTrailingSlash(path) + + const limit = pageSize + const skip = (page - 1) * pageSize + + const pinnedArticles = await findPinned(page) + const total = await totalArticles() + const totalPages = total / pageSize + + const id = i => i._id + const pinnedIds = pinnedArticles.map(id) + const articles = await findArticlesNin(pinnedIds, skip, limit, {date: -1}) + + const prevPage = page > 1 + ? page - 1 + : null + + const nextPage = page < totalPages + ? page + 1 + : null + + const content = [ + ...pinnedArticles.sort(sortArticleByDate), + ...articles + ] + .map(article => ({ + ...article, + url: articleUrl(article), + })) + + return ( + + ) +} + function getMeta(article) { return { title: article.title, @@ -55,45 +95,22 @@ function getMeta(article) { function getArticles(pageSize) { return get('/articles/:page?', async (ctx, page) => { - const path = getPathWithNoTrailingSlash(ctx.path) page = parseInt(page) || 1 - const limit = pageSize - const skip = (page - 1) * pageSize + if (page === 1) { + ctx.redirect('/') + } else { + const path = getPathWithNoTrailingSlash(ctx.path) - const pinnedArticles = await findPinned(page) - const total = await totalArticles() - const totalPages = total / pageSize + const articles = await getArticleListComponent(path, page, pageSize) + const Component = ( +
+ {articles} +
+ ) - const id = i => i._id - const pinnedIds = pinnedArticles.map(id) - const articles = await findArticlesNin(pinnedIds, skip, limit, {date: -1}) - - const prevPage = page > 1 - ? page - 1 - : null - - const nextPage = page < totalPages - ? page + 1 - : null - - const content = [ - ...pinnedArticles.sort(sortArticleByDate), - ...articles - ] - .map(article => ({ - ...article, - url: articleUrl(article), - })) - const Component = ( - - ) - - ctx.body = await render(path, Component, getMeta({title: `Статьи`})) + ctx.body = await render(path, Component, getMeta({title: `Статьи`})) + } }) } @@ -132,5 +149,6 @@ function getArticle() { })) } +exports.getArticleListComponent = getArticleListComponent exports.getArticles = getArticles exports.getArticle = getArticle diff --git a/src/routes/events.js b/src/routes/events.js deleted file mode 100644 index 9c350f21..00000000 --- a/src/routes/events.js +++ /dev/null @@ -1,36 +0,0 @@ -import route from 'koa-route'; -import {c} from '../core/db'; -import {json} from './'; -import {sortBy} from '../utils/sort'; - -let sortByDate = sortBy( - i => (new Date(i.date)) - .getTime() -); - -export default function () { - return route.get('/events/:page?', json( - async(ctx) => { - let limit = parseInt(ctx.query['limit']) || 10; - let skip = parseInt(ctx.query['skip']) || 0; - - let query = {}; - let documents = await c('events') - .find(query) - .sort({date: -1}) - .skip(skip) - .limit(limit) - .toArray(); - - let total = await c('events') - .find(query).count(); - - ctx.body = { - skip: skip, - limit: limit, - total: total, - data: documents - }; - } - )); -}; diff --git a/src/routes/home.js b/src/routes/home.js index 910beb8e..57b85017 100644 --- a/src/routes/home.js +++ b/src/routes/home.js @@ -2,6 +2,7 @@ const React = require('react') const Article = require('../components/Article') const {get} = require('koa-route') const {c} = require('../core/db') +const getArticleListComponent = require('./articles').getArticleListComponent const {render} = require('../lib/render') const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash const timestamp = require('../lib/date').timestamp @@ -34,17 +35,6 @@ async function findPinned(page) { .toArray() } -function getComponent(path, article) { - return ( -
- ) -} - function getMeta(article) { return { title: article.title, @@ -52,33 +42,19 @@ function getMeta(article) { } function getHome(pageSize) { - return get('/', async (ctx, page) => { - const path = getPathWithNoTrailingSlash(ctx.path) || '/' - page = parseInt(page) || 1 + return get('/', async ctx => { + const path = getPathWithNoTrailingSlash(ctx.path) + const articles = await getArticleListComponent(path, 1, pageSize) - const limit = pageSize - const skip = (page - 1) * pageSize - - const pinnedArticles = await findPinned(page) - - const id = i => i._id - const pinnedIds = pinnedArticles.map(id) - const articles = await findArticlesNin(pinnedIds, skip, limit, {date: -1}) - - const content = [ - ...pinnedArticles.sort(sortArticleByDate), - ...articles - ] - .map(article => getComponent(articleUrl(article), article)) const Component = ( -
-
-

МБУДО

-

Шлиссельбургская детская художественная школа

-
- - {content} +
+
+

МБУДО

+

Шлиссельбургская детская художественная школа

+ + {articles} +
) ctx.body = await render(path, Component, getMeta({title: 'Шлиссельбургская ДХШ'}), {showAuthor: true}) diff --git a/src/routes/index.js b/src/routes/index.js index 0f256536..b81bfd25 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -7,22 +7,18 @@ import {index as indexFile} from '../config' import sitemap from './sitemap' import schedule from './schedule' -import news from './news' import gallery from './gallery' import documents from './documents' import teachers from './teachers' -import events from './events' import pages from './pages' export function routes(store) { return compose([ sitemap(), schedule(), - news(), gallery(), documents(store), teachers(), - events(), pages(), ]) } diff --git a/src/routes/news.js b/src/routes/news.js deleted file mode 100644 index f4f75965..00000000 --- a/src/routes/news.js +++ /dev/null @@ -1,67 +0,0 @@ -import compose from 'koa-compose'; -import route from 'koa-route'; -import {accepts} from './'; -import {c} from '../core/db'; -import {sortBy} from '../utils/sort'; - -const timestamp = i => i.getTime(); -const sortNewsByDate = sortBy( - i => timestamp(new Date(i.date)) -); - -const redirect = async ctx => ctx.redirect('/'); - -export default function () { - return compose([ - news(), - pinned() - ]); -}; - -function news() { - return route.get('/news', accepts({ - 'text/html': redirect, - 'text/plain': redirect, - 'application/json': async ctx => { - const limit = parseInt(ctx.query['limit']) || 20; - const skip = parseInt(ctx.query['skip']) || 0; - - const now = new Date(); - const pinned = skip !== 0 ? [] : await c('timeline') - .find({until: {$gte: now}}) - .sort({date: -1}) - .toArray(); - - const id = i => i._id; - const pinnedIds = pinned.map(id); - - let posts = await c('timeline') - .find({ - _id: {$nin: pinnedIds} - }) - .sort({date: -1}) - .skip(skip) - .limit(limit) - .toArray(); - - ctx.body = [ - ...pinned.sort(sortNewsByDate), - ...posts - ]; - } - })); -} - -function pinned(){ - return route.get('/news/pinned', accepts({ - 'text/html': redirect, - 'text/plain': redirect, - 'application/json': async ctx => { - let now = new Date(); - ctx.body = await c('timeline') - .find({until: {$gte: now}}) - .toArray() - .then(i => i.sort(sortNewsByDate)); - } - })); -}