/news, /events deleted, Articles update

This commit is contained in:
tmshv committed 2017-05-22 13:30:05 +03:00
1 parent 7bc34f9710
commit e21425fd52
8 files changed
+66 -221

No files matched your search

-42
View File
@@ -83,48 +83,6 @@ app.config(($locationProvider, $routeProvider) => {
})) }))
let routers = [ let routers = [
{
name: '/',
template: '<page-school></page-school>',
title: 'Шлиссельбургская детская художественная школа'
},
{
name: '/events/:page?',
template: '<page-events page="{{$.page.page}}" total-pages="{{$.page.totalPages}}" posts="$.page.data"></page-events>',
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: '<page-article article="$.data"></page-article>',
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?', name: '/schedule/:period?/:semester?',
template: '<page-schedule schedules="$resolve.list"></page-schedule>', template: '<page-schedule schedules="$resolve.list"></page-schedule>',
+1 -1
View File
@@ -3,7 +3,7 @@ const Paginator = require('./Paginator')
const Article = require('./Article') const Article = require('./Article')
const ArticleList = ({articles, nextPage, prevPage}) => ( const ArticleList = ({articles, nextPage, prevPage}) => (
<div className="content content_thin"> <div>
{!prevPage ? null : ( {!prevPage ? null : (
<Paginator url={`/articles/${prevPage}`} type="top"/> <Paginator url={`/articles/${prevPage}`} type="top"/>
)} )}
+1 -1
View File
@@ -8,7 +8,7 @@ export default [
{url: '/articles/*', text: 'Статьи'}, {url: '/articles/*', text: 'Статьи'},
{url: '/article/*', text: ''}, {url: '/article/*', text: ''},
], ],
}, , },
{ {
'text': 'Расписание', 'text': 'Расписание',
'url': '/schedule', 'url': '/schedule',
+53 -35
View File
@@ -47,6 +47,46 @@ async function findPinned(page) {
.toArray() .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 (
<ArticleList
articles={content}
prevPage={prevPage}
nextPage={nextPage}
/>
)
}
function getMeta(article) { function getMeta(article) {
return { return {
title: article.title, title: article.title,
@@ -55,45 +95,22 @@ function getMeta(article) {
function getArticles(pageSize) { function getArticles(pageSize) {
return get('/articles/:page?', async (ctx, page) => { return get('/articles/:page?', async (ctx, page) => {
const path = getPathWithNoTrailingSlash(ctx.path)
page = parseInt(page) || 1 page = parseInt(page) || 1
const limit = pageSize if (page === 1) {
const skip = (page - 1) * pageSize ctx.redirect('/')
} else {
const path = getPathWithNoTrailingSlash(ctx.path)
const pinnedArticles = await findPinned(page) const articles = await getArticleListComponent(path, page, pageSize)
const total = await totalArticles() const Component = (
const totalPages = total / pageSize <div className="content content_thin">
{articles}
</div>
)
const id = i => i._id ctx.body = await render(path, Component, getMeta({title: `Статьи`}))
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 = (
<ArticleList
articles={content}
prevPage={prevPage}
nextPage={nextPage}
/>
)
ctx.body = await render(path, Component, getMeta({title: `Статьи`}))
}) })
} }
@@ -132,5 +149,6 @@ function getArticle() {
})) }))
} }
exports.getArticleListComponent = getArticleListComponent
exports.getArticles = getArticles exports.getArticles = getArticles
exports.getArticle = getArticle exports.getArticle = getArticle
-36
View File
@@ -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
};
}
));
};
+11 -35
View File
@@ -2,6 +2,7 @@ const React = require('react')
const Article = require('../components/Article') const Article = require('../components/Article')
const {get} = require('koa-route') const {get} = require('koa-route')
const {c} = require('../core/db') const {c} = require('../core/db')
const getArticleListComponent = require('./articles').getArticleListComponent
const {render} = require('../lib/render') const {render} = require('../lib/render')
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
const timestamp = require('../lib/date').timestamp const timestamp = require('../lib/date').timestamp
@@ -34,17 +35,6 @@ async function findPinned(page) {
.toArray() .toArray()
} }
function getComponent(path, article) {
return (
<Article
url={path}
title={article.title}
date={article.date}
data={article.post}
/>
)
}
function getMeta(article) { function getMeta(article) {
return { return {
title: article.title, title: article.title,
@@ -52,33 +42,19 @@ function getMeta(article) {
} }
function getHome(pageSize) { function getHome(pageSize) {
return get('/', async (ctx, page) => { return get('/', async ctx => {
const path = getPathWithNoTrailingSlash(ctx.path) || '/' const path = getPathWithNoTrailingSlash(ctx.path)
page = parseInt(page) || 1 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 = ( const Component = (
<div className="content content_thin"> <div className="content content_thin">
<div className="hudozka-title"> <div className="hudozka-title">
<p>МБУДО</p> <p>МБУДО</p>
<h1>Шлиссельбургская детская художественная&nbsp;школа</h1> <h1>Шлиссельбургская детская художественная&nbsp;школа</h1>
</div>
{content}
</div> </div>
{articles}
</div>
) )
ctx.body = await render(path, Component, getMeta({title: 'Шлиссельбургская ДХШ'}), {showAuthor: true}) ctx.body = await render(path, Component, getMeta({title: 'Шлиссельбургская ДХШ'}), {showAuthor: true})
-4
View File
@@ -7,22 +7,18 @@ import {index as indexFile} from '../config'
import sitemap from './sitemap' import sitemap from './sitemap'
import schedule from './schedule' import schedule from './schedule'
import news from './news'
import gallery from './gallery' import gallery from './gallery'
import documents from './documents' import documents from './documents'
import teachers from './teachers' import teachers from './teachers'
import events from './events'
import pages from './pages' import pages from './pages'
export function routes(store) { export function routes(store) {
return compose([ return compose([
sitemap(), sitemap(),
schedule(), schedule(),
news(),
gallery(), gallery(),
documents(store), documents(store),
teachers(), teachers(),
events(),
pages(), pages(),
]) ])
} }
-67
View File
@@ -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));
}
}));
}