app moved to next.js

This commit is contained in:
tmshv committed 2019-08-01 14:44:25 +03:00
1 parent dcca513aea
commit 43af2996bf
48 files changed
+1147 -1027

No files matched your search

-17
View File
@@ -1,17 +0,0 @@
const {renderAlbum} = require('../render/albums')
const {get} = require('koa-route')
function getAlbum() {
return get('/album/:id', async (ctx, id) => {
const body = await renderAlbum(id)
if (body) {
ctx.type = 'text/html'
ctx.body = body
} else {
ctx.status = 404
}
})
}
exports.getAlbum = getAlbum
-46
View File
@@ -1,46 +0,0 @@
const {get} = require('koa-route')
const Article = require('../components/Article')
const {renderArticles, renderArticle} = require('../render/articles')
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
const accepts = require('./index').accepts
function getArticles(pageSize) {
return get('/articles/:page?', async (ctx, page) => {
page = parseInt(page) || 1
if (page === 1) {
ctx.redirect('/')
} else {
const path = getPathWithNoTrailingSlash(ctx.path)
ctx.body = await renderArticles(path, page, pageSize)
}
})
}
function getArticle() {
return get('/article/:id', accepts({
'text/html': async (ctx, id) => {
const body = await renderArticle(id)
if (body) {
ctx.type = 'text/html'
ctx.body = body
} else {
ctx.status = 404
}
},
'application/json': async (ctx, id) => {
const article = await Article.findById(id)
if (article) {
ctx.body = article
} else {
ctx.status = 404
}
}
}))
}
exports.getArticles = getArticles
exports.getArticle = getArticle
-126
View File
@@ -1,126 +0,0 @@
const React = require('react')
const Share = require('../components/Share')
const DocumentComponent = require('../components/Document')
const Document = require('../core/Document')
const ImageArtifactType = require('../core/ImageArtifactType')
const {render} = require('../lib/render')
const {prioritySort} = require('../lib/sort')
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
const {get} = require('koa-route')
const {splitBy} = require('../lib/array')
const {unique} = require('../utils/common')
const {bakeDocument} = require('./documents')
const sizes = [
ImageArtifactType.MEDIUM,
ImageArtifactType.BIG,
ImageArtifactType.ORIGIN,
]
function getMeta(document) {
return {
title: document.title,
description: document.title,
}
}
function getDocument() {
return get('/document/:id', async (ctx, id) => {
const path = getPathWithNoTrailingSlash(ctx.path)
const document = await Document.findById(id)
const data = document
? await bakeDocument(document)
: null
if (data) {
const Component = (
<div className="content content_wide">
<DocumentComponent {...data}/>
<Share/>
</div>
)
ctx.type = 'text/html'
ctx.body = await render(path, Component, getMeta(document), {menuPadding: true})
} else {
ctx.status = 404
}
})
}
function getSorted(documents) {
const compose = (...fns) => value => fns
.map(fn => fn)
.reverse()
.reduce((acc, fn) => fn(acc), value)
const pass = fn => value => {
fn(value)
return value
}
const subtract = (a, b) => a - b
const uniqueCategories = unique(i => i.category)
const priority = {
'Основные документы': [
'https://static.shlisselburg.org/art/uploads/ustav-2015.pdf',
'https://static.shlisselburg.org/art/uploads/obrazovatelnaia-litsenziia-2015.pdf',
'https://static.shlisselburg.org/art/uploads/svidetelstvo-na-pomeshchenie.pdf',
'https://static.shlisselburg.org/art/uploads/egriul.pdf',
'https://static.shlisselburg.org/art/uploads/inn.pdf',
'https://static.shlisselburg.org/art/uploads/reshenie-o-sozdanii-shkoly.pdf',
'https://static.shlisselburg.org/art/uploads/kopiia-resheniia-o-naznachenii-rukovoditelia.pdf',
'https://static.shlisselburg.org/art/uploads/postanovlenie-o-sozdanii-munitsipalnogo-obrazovatelnogo-uchrezhdeniia-kultury-detskaia-khudozhestvennaia-shkola.pdf',
],
'Учебные программы': [
'https://static.shlisselburg.org/art/uploads/uchebnaia-programma-zhivopis.pdf',
]
}
const CATEGORY_PRIORITY = [
'Основные документы',
'Планы ФХД',
'Самообследование деятельности',
'Инструкции',
'Образование',
'Учебные программы',
'Платные образовательные услуги',
'Дополнительные общеразвивающие программы',
'Документы для поступления',
]
const sortBy = fn => (a, b) => [a, b]
.map(fn)
.reduce(subtract)
const getPriorityFn = (m, list) => value => list.includes(value)
? list.indexOf(value)
: m
const safeSelect = (d, store, key) => key in store
? store[key]
: d
const categoryPriority = getPriorityFn(10000, CATEGORY_PRIORITY)
const documentPriority = ({category, fileUrl}) => compose(
getPriorityFn.bind(null, 10000),
safeSelect.bind(null, [], priority)
)(category)(fileUrl)
const documentsOf = category => documents.filter(i => i.category === category)
const sorted = documents => documents.sort(sortBy(documentPriority))
const collectDocumentsByCategory = compose(sorted, documentsOf)
return uniqueCategories(documents)
.sort(sortBy(categoryPriority))
.reduce((acc, name) => [...acc, {
name,
items: collectDocumentsByCategory(name)
}], [])
}
exports.getDocument = getDocument
-159
View File
@@ -1,159 +0,0 @@
const React = require('react')
const Article = require('../components/Article')
const DocumentList = require('../components/DocumentList')
const Document = require('../core/Document')
const ImageArtifactType = require('../core/ImageArtifactType')
const {render} = require('../lib/render')
const {prioritySort} = require('../lib/sort')
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
const {get} = require('koa-route')
const {splitBy} = require('../lib/array')
const {unique} = require('../utils/common')
const sizes = [
ImageArtifactType.MEDIUM,
ImageArtifactType.BIG,
ImageArtifactType.ORIGIN,
]
function getDocumentsMeta() {
return {
title: 'Документы',
description: 'Документы Шлиссельбургской ДХШ',
}
}
function getMeta(teacher) {
return {
title: teacher.name,
description: teacher.name,
}
}
async function bakeDocument(document) {
const preview = document.preview
if (!preview) return null
const image = preview.findArtifact(sizes)
if (!image) return null
const category = (document.category || '')
.replace('Documents/', '')
.replace('Pages/', '')
.replace('Albums/', '')
.replace('Articles /', '')
return {
category,
title: document.title,
fileName: document.fileInfo.name,
fileSize: document.fileInfo.size,
fileUrl: document.url,
imageUrl: image.url,
url: document.viewUrl,
}
}
function getDocuments() {
return get('/documents', async ctx => {
const path = getPathWithNoTrailingSlash(ctx.path)
let documents = await Document.find({hidden: false})
if (documents) {
documents = await Promise.all(documents.map(bakeDocument))
documents = documents.filter(Boolean)
const collections = getSorted(documents)
const Component = (
<div className="content content_thin">
{collections.map(({name, items}, index) => (
<DocumentList key={index} name={name} documents={items}/>
))}
</div>
)
ctx.type = 'text/html'
ctx.body = await render(path, Component, getDocumentsMeta(), {menuPadding: true})
} else {
ctx.status = 404
}
})
}
function getSorted(documents) {
const compose = (...fns) => value => fns
.map(fn => fn)
.reverse()
.reduce((acc, fn) => fn(acc), value)
const pass = fn => value => {
fn(value)
return value
}
const subtract = (a, b) => a - b
const uniqueCategories = unique(i => i.category)
const priority = {
'Основные документы': [
'https://static.shlisselburg.org/art/uploads/ustav-2015.pdf',
'https://static.shlisselburg.org/art/uploads/obrazovatelnaia-litsenziia-2015.pdf',
'https://static.shlisselburg.org/art/uploads/svidetelstvo-na-pomeshchenie.pdf',
'https://static.shlisselburg.org/art/uploads/egriul.pdf',
'https://static.shlisselburg.org/art/uploads/inn.pdf',
'https://static.shlisselburg.org/art/uploads/reshenie-o-sozdanii-shkoly.pdf',
'https://static.shlisselburg.org/art/uploads/kopiia-resheniia-o-naznachenii-rukovoditelia.pdf',
'https://static.shlisselburg.org/art/uploads/postanovlenie-o-sozdanii-munitsipalnogo-obrazovatelnogo-uchrezhdeniia-kultury-detskaia-khudozhestvennaia-shkola.pdf',
],
'Учебные программы': [
'https://static.shlisselburg.org/art/uploads/uchebnaia-programma-zhivopis.pdf',
]
}
const CATEGORY_PRIORITY = [
'Основные документы',
'Планы ФХД',
'Самообследование деятельности',
'Инструкции',
'Образование',
'Учебные программы',
'Платные образовательные услуги',
'Дополнительные общеразвивающие программы',
'Документы для поступления',
]
const sortBy = fn => (a, b) => [a, b]
.map(fn)
.reduce(subtract)
const getPriorityFn = (m, list) => value => list.includes(value)
? list.indexOf(value)
: m
const safeSelect = (d, store, key) => key in store
? store[key]
: d
const categoryPriority = getPriorityFn(10000, CATEGORY_PRIORITY)
const documentPriority = ({category, fileUrl}) => compose(
getPriorityFn.bind(null, 10000),
safeSelect.bind(null, [], priority)
)(category)(fileUrl)
const documentsOf = category => documents.filter(i => i.category === category)
const sorted = documents => documents.sort(sortBy(documentPriority))
const collectDocumentsByCategory = compose(sorted, documentsOf)
return uniqueCategories(documents)
.sort(sortBy(categoryPriority))
.reduce((acc, name) => [...acc, {
name,
items: collectDocumentsByCategory(name)
}], [])
}
exports.bakeDocument = bakeDocument
exports.getDocuments = getDocuments
-90
View File
@@ -1,90 +0,0 @@
const React = require('react')
const Article = require('../components/Article')
const Album = require('../core/Album')
const {render} = require('../lib/render')
const {splitBy} = require('../lib/array')
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
const {get} = require('koa-route')
function getMeta() {
return {
title: 'Галерея',
description: 'Галерея работ учащихся Шлиссельбургской Детской Художественной Школы'
}
}
// {data.set.map((s, i) => (
// <source
// key={i}
// src={i}
// />
// ))}
// const m = 0.5
const m = 1
const AlbumImage = ({data, alt}) => (
<picture>
<img
className="opa"
alt={alt}
src={data.src}
width={data.width * m}
height={data.height * m}
srcSet={(data.set || []).map(({url, density}) => `${url} ${density}x`)}
/>
</picture>
)
const GItem = ({album}) => (
<div className="gallery-item">
<a className="invisible" href={album.url}>
<AlbumImage data={album.preview} alt={album.title}/>
</a>
</div>
)
const ACollection = ({title, albums}) => (
<div className="album-collection">
<div className="album-collection__title">{title}</div>
<div className="album-collection__body">{
albums.map((album, index) => (
<GItem key={index} album={album}/>
))
}</div>
</div>
)
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 = (
<div className="content content_wide">
<Article title={meta.title}>
{
[...albumsByYear.entries()].map(([year, albums], index) => (
<ACollection
key={index}
title={year}
albums={albums}
/>
))
}
</Article>
</div>
)
ctx.type = 'text/html'
ctx.body = await render(path, Component, getMeta(), {menuPadding: true})
} else {
ctx.status = 404
}
})
}
exports.getGallery = getGallery
-12
View File
@@ -1,12 +0,0 @@
const {get} = require('koa-route')
const {renderHome} = require('../render/home')
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
function getHome(pageSize) {
return get('/', async ctx => {
const path = getPathWithNoTrailingSlash(ctx.path)
ctx.body = await renderHome(path, pageSize)
})
}
exports.getHome = getHome
-67
View File
@@ -1,67 +0,0 @@
import {readFile} from 'async-file'
import handlebars from 'handlebars'
import {map, assign} from '../utils/common'
import {index as indexFile} from '../config'
export function queryObject() {
let toTrue = map(true, i => i === 'true')
let toFalse = map(false, i => i === 'false')
const processors = [toTrue, toFalse]
return async(ctx, next) => {
ctx.query = Object
.keys(ctx.query)
.reduce((query, key) => assign(
query,
key,
processors
.reduce((value, fn) => fn(value), query[key])
), ctx.query)
await next()
}
}
export function accepts(routes) {
return async function (ctx) {
const types = Object
.keys(routes)
.filter(type => ctx.request.accepts(type))
if (types.length) {
let fn = routes[types[0]]
await fn.apply(ctx, arguments)
} else {
ctx.status = 406
}
}
}
export function index(fn) {
return async function (ctx) {
let test = true
if (fn) test = await fn.apply(ctx, arguments)
if (!test) ctx.status = 404
const data = await getInitialData()
const source = await readFile(indexFile, 'utf-8')
const template = handlebars.compile(source)
ctx.type = 'text/html'
ctx.body = template({data: JSON.stringify(data)})
}
}
export function json(fn) {
return accepts({
'text/html': index(),
'text/plain': index(),
'application/json': fn
})
}
async function getInitialData() {
return {pages: []}
}
-16
View File
@@ -1,16 +0,0 @@
const {renderPage} = require('../render/page')
const {get} = require('koa-route')
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
module.exports = function (handleNotFound) {
return get('*', async ctx => {
const path = getPathWithNoTrailingSlash(ctx.path)
const body = await renderPage(path)
if (body) {
ctx.type = 'text/html'
ctx.body = body
} else {
await handleNotFound(ctx)
}
})
}
-34
View File
@@ -1,34 +0,0 @@
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
const {renderPerson, renderCollective} = require('../render/persons')
const {get} = require('koa-route')
function getCollective(order) {
return get('/collective', async ctx => {
const path = getPathWithNoTrailingSlash(ctx.path)
const body = await renderCollective(path, order)
if (body) {
ctx.type = 'text/html'
ctx.body = body
} else {
ctx.status = 404
}
})
}
function getTeacher() {
return get('/teacher/:id', async (ctx, id) => {
const body = await renderPerson(id)
if (body) {
ctx.type = 'text/html'
ctx.body = body
} else {
ctx.status = 404
}
})
}
exports.getCollective = getCollective
exports.getTeacher = getTeacher