documents page fixed

This commit is contained in:
tmshv committed 2019-10-15 02:12:56 +03:00
1 parent eed3a3c007
commit ed9dae3760
13 files changed
+156 -105

No files matched your search

+33 -16
View File
@@ -5,12 +5,19 @@ import { Meta } from '../src/components/Meta'
import menuModel from '../src/models/menu' import menuModel from '../src/models/menu'
import { buildMenu } from '../src/lib/menu' import { buildMenu } from '../src/lib/menu'
import { meta } from '../src/lib/meta' import { meta } from '../src/lib/meta'
import { IDocument } from '../src/types'
import DocumentList from '../src/components/DocumentList' import { DocumentList } from '../src/components/DocumentList'
import { unique } from '../src/utils/common' import { unique } from '../src/utils/common'
import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib' import { createApiUrl, requestGet, wrapInitialProps, IResponseItems } from '../src/next-lib'
import { NextPage } from 'next'
function getSorted(documents) { interface IDocumentCategory {
name: string
items: IDocument[]
}
function getSorted(documents: IDocument[]): IDocumentCategory[] {
const compose = (...fns) => value => fns const compose = (...fns) => value => fns
.map(fn => fn) .map(fn => fn)
.reverse() .reverse()
@@ -23,7 +30,7 @@ function getSorted(documents) {
const subtract = (a, b) => a - b const subtract = (a, b) => a - b
const uniqueCategories = unique(i => i.category) const uniqueCategories = unique<IDocument, string>(i => i.category)
const priority = { const priority = {
'Основные документы': [ 'Основные документы': [
@@ -53,13 +60,16 @@ function getSorted(documents) {
'Документы для поступления', 'Документы для поступления',
] ]
const sortBy = fn => (a, b) => [a, b] function sortBy<T>(fn: (value: T) => number): (a: T, b: T) => number {
.map(fn) return (a, b) => [a, b].map(fn).reduce(subtract)
.reduce(subtract) }
const getPriorityFn = (m, list) => value => list.includes(value) function getPriorityFn<T>(m: number, list: T[]): (value: T) => number {
? list.indexOf(value) return (value: T) => {
: m const has = list.includes(value)
return has ? list.indexOf(value) : m
}
}
const safeSelect = (d, store, key) => key in store const safeSelect = (d, store, key) => key in store
? store[key] ? store[key]
@@ -84,7 +94,14 @@ function getSorted(documents) {
}], []) }], [])
} }
const Page = (props) => ( interface IProps {
collections: IDocumentCategory[]
pageUrl: string
title: string
meta: any
}
const Page: NextPage<IProps> = props => (
<App <App
menu={buildMenu(props.pageUrl, menuModel)} menu={buildMenu(props.pageUrl, menuModel)}
showAuthor={true} showAuthor={true}
@@ -96,11 +113,11 @@ const Page = (props) => (
</Head> </Head>
<div className="content content_thin"> <div className="content content_thin">
{props.collections.map(({ name, items }, index) => ( {props.collections.map((collection, index) => (
<DocumentList <DocumentList
key={index} key={index}
name={name} name={collection.name}
documents={items} documents={collection.items}
/> />
))} ))}
</div> </div>
@@ -109,8 +126,8 @@ const Page = (props) => (
Page.getInitialProps = wrapInitialProps(async (ctx) => { Page.getInitialProps = wrapInitialProps(async (ctx) => {
const pageUrl = '/documents' const pageUrl = '/documents'
const res = await requestGet(createApiUrl(ctx.req, '/api/files'), {}) const res = await requestGet<IResponseItems<IDocument>>(createApiUrl(ctx.req, '/api/files'), { items: [] })
const files = res.items || [] const files = res.items
const title = 'Документы' const title = 'Документы'
const collections = getSorted(files) const collections = getSorted(files)
+1 -1
View File
@@ -36,7 +36,7 @@ Page.getInitialProps = wrapInitialProps(async (ctx) => {
const pageUrl = '/' const pageUrl = '/'
const page = 1 const page = 1
const pageSize = ctx.query.pageSize || 15 const pageSize = ctx.query.pageSize || 15
const res = await requestGet(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`), {}) const res = await requestGet(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`), {}) as any
const articles = res.items || [] const articles = res.items || []
const nextPage = res.nextPage const nextPage = res.nextPage
const prevPage = res.prevPage const prevPage = res.prevPage
-18
View File
@@ -1,18 +0,0 @@
const React = require('react')
const Document = require('./DocumentListItem')
const DocumentList = ({documents, name}) => (
<div className="documents-container">
<div className="documents-container__head">
<h2>{name}</h2>
</div>
<div className="documents-container__body">
{documents.map((d, i) => (
<Document key={i} {...d}/>
))}
</div>
</div>
)
module.exports = DocumentList
+22
View File
@@ -0,0 +1,22 @@
import * as React from 'react'
import { IDocument } from '../../types'
import { DocumentListItem } from '../DocumentListItem'
export interface IDocumentListProps {
documents: IDocument[]
name: string
}
export const DocumentList: React.FC<IDocumentListProps> = props => (
<div className="documents-container">
<div className="documents-container__head">
<h2>{props.name}</h2>
</div>
<div className="documents-container__body">
{props.documents.map((d, i) => (
<DocumentListItem key={i} {...d} />
))}
</div>
</div>
)
-23
View File
@@ -1,23 +0,0 @@
const React = require('react')
const {ext, size} = require('../lib/file')
const {titleCase} = require('../lib/string')
const Document = ({title, url, imageUrl, fileUrl, fileName, fileSize}) => (
<div className="document-row">
<a href={url} className="invisible">
<div className="document-row__image">
<img src={imageUrl} alt={title}/>
</div>
</a>
<div className="document-row__file">
<a href={url}>{titleCase(title)}</a>
</div>
<div className="document-row__file-info">
<a href={fileUrl} target="_blank">{ext(fileName)} ({size(fileSize)})</a>
</div>
</div>
)
module.exports = Document
+30
View File
@@ -0,0 +1,30 @@
import * as React from 'react'
import { ext, size } from '../../lib/file'
import { titleCase } from '../../lib/string'
export interface IDocumentListItemProps {
title: string
url: string
imageUrl: string
fileUrl: string
fileName: string
fileSize: number
}
export const DocumentListItem: React.FC<IDocumentListItemProps> = props => (
<div className="document-row">
<a href={props.url} className="invisible">
<div className="document-row__image">
<img src={props.imageUrl} alt={props.title} />
</div>
</a>
<div className="document-row__file">
<a href={props.url}>{titleCase(props.title)}</a>
</div>
<div className="document-row__file-info">
<a href={props.fileUrl} target="_blank">{ext(props.fileName)} ({size(props.fileSize)})</a>
</div>
</div>
)
+20 -8
View File
@@ -1,12 +1,12 @@
const Data = require('./Data') import Data from './Data'
const Image = require('./Image') import Image from './Image'
const { find, findOne } = require('../lib/store') import { find, findOne } from '../lib/store'
const store = () => Data.getStore(Document) const store = () => Data.getStore('Document')
class Document { export default class Document {
static async find(query, options = {}) { static async find(query, options = {}) {
const items = await find(store(), query, options) const items = await find(store(), query, options as any)
return Promise.all(items.map(processDocument)) return Promise.all(items.map(processDocument))
} }
@@ -18,6 +18,17 @@ class Document {
return processDocument(data) return processDocument(data)
} }
public id: any
public hash: any
public file: any
public fileInfo: any
public type: any
public title: any
public category: any
public url: any
public preview: any
public viewUrl: any
constructor(data) { constructor(data) {
this.id = data.id this.id = data.id
this.hash = data.hash this.hash = data.hash
@@ -37,6 +48,9 @@ class Document {
} }
class DocumentFile { class DocumentFile {
public name: any
public size: any
constructor(data) { constructor(data) {
this.name = data.name this.name = data.name
this.size = data.size this.size = data.size
@@ -52,5 +66,3 @@ const processDocument = async data => {
preview, preview,
}) })
} }
module.exports = Document
+1 -1
View File
@@ -41,7 +41,7 @@ function init(db) {
Data.setStore('Album', db.collection('albums')) Data.setStore('Album', db.collection('albums'))
Data.setStore('Image', db.collection('images')) Data.setStore('Image', db.collection('images'))
Data.setStore('Teacher', db.collection('collective')) Data.setStore('Teacher', db.collection('collective'))
Data.setStore(Document, db.collection('documents')) Data.setStore('Document', db.collection('documents'))
Data.setStore('Settings', db.collection('settings')) Data.setStore('Settings', db.collection('settings'))
return db return db
-27
View File
@@ -1,27 +0,0 @@
function ext(filname) {
let getExt = name => /\.(\w+)$/.exec(name)[1]
let defaultExt = ext => ext.toUpperCase()
const exts = ['png', 'pdf', 'jpg', 'jpeg', 'gif', 'doc']
const extd = ['ПНГ', 'ПДФ', 'ЖПГ', 'ЖПГ', 'ГИФ', 'ДОК']
let ext = getExt(filname)
const i = exts.indexOf(ext)
return i > -1
? extd[i]
: defaultExt(ext)
}
function size(bytes, precision = 1) {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return ''
const units = ['байт', 'КБ', 'МБ', 'ГБ', 'ТБ', 'ПБ']
const number = Math.floor(Math.log(bytes) / Math.log(1024))
const size = (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision)
return `${size} ${units[number]}`
}
exports.ext = ext
exports.size = size
+23
View File
@@ -0,0 +1,23 @@
const getExt = (name: string) => /\.(\w+)$/.exec(name)[1]
const defaultExt = (ext: string) => ext.toUpperCase()
const exts = ['png', 'pdf', 'jpg', 'jpeg', 'gif', 'doc']
const extd = ['ПНГ', 'ПДФ', 'ЖПГ', 'ЖПГ', 'ГИФ', 'ДОК']
const units = ['байт', 'КБ', 'МБ', 'ГБ', 'ТБ', 'ПБ']
export function ext(filname: string): string {
const ext = getExt(filname)
const i = exts.indexOf(ext)
return i > -1
? extd[i]
: defaultExt(ext)
}
export function size(bytes: any, precision = 1): string {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return ''
const number = Math.floor(Math.log(bytes) / Math.log(1024))
const size = (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision)
return `${size} ${units[number]}`
}
+7 -3
View File
@@ -1,8 +1,12 @@
import axios from 'axios' import axios from 'axios'
export async function requestGet(url, defaultResponse) { export interface IResponseItems<T> {
items: T[]
}
export async function requestGet<T>(url: string, defaultResponse: T): Promise<T> {
try { try {
const res = await axios.get(url) const res = await axios.get<T>(url)
return res.data return res.data
} catch (e) { } catch (e) {
@@ -19,7 +23,7 @@ export async function requestGet(url, defaultResponse) {
export function wrapInitialProps(fn) { export function wrapInitialProps(fn) {
return async ctx => { return async ctx => {
if (process.env.NODE_ENV === 'production' && process.browser) { if (process.env.NODE_ENV === 'production' && process.browser) {
return __NEXT_DATA__.props.pageProps; return window['__NEXT_DATA__'].props.pageProps;
} }
return fn(ctx) return fn(ctx)
+9
View File
@@ -0,0 +1,9 @@
export interface IDocument {
category: string
fileName: string
fileSize: number
fileUrl: string
imageUrl: string
title: string
url: string
}
+10 -8
View File
@@ -40,7 +40,7 @@ export function indexEquals(withNumber) {
* @param map * @param map
*/ */
export function select(byCondition, map = i => i) { export function select(byCondition, map = i => i) {
return (result, item) => byCondition(item) ? map(item): map(result) return (result, item) => byCondition(item) ? map(item) : map(result)
} }
/** /**
@@ -49,19 +49,21 @@ export function select(byCondition, map = i => i) {
* @param selector * @param selector
* @returns {*} * @returns {*}
*/ */
export function choise(scope, selector){ export function choise(scope, selector) {
return scope.reduce(selector, null); return scope.reduce(selector, null);
} }
export function unique(fn) { export function unique<T, U>(fn: (value: T) => U): (list: T[]) => U[] {
return list => [...new Set(list.map(fn))]; return list => Array.from(
new Set(list.map(fn))
)
} }
export function branch(fn) { export function branch(fn) {
let keys = unique(fn); let keys = unique(fn);
return list => keys(list) return list => keys(list)
.reduce((dict, key) => { .reduce((dict: any, key: any) => {
dict[key] = list.filter( dict[key] = list.filter(
i => fn(i) === key i => fn(i) === key
); );
@@ -71,7 +73,7 @@ export function branch(fn) {
} }
export function assign(dict, key, value){ export function assign(dict, key, value) {
dict[key] = value; dict[key] = value;
return dict; return dict;
} }
@@ -88,7 +90,7 @@ export function map(v, fn) {
* @param selector * @param selector
* @returns {*} * @returns {*}
*/ */
export function createMap(selector, list){ export function createMap(selector, list) {
return list.reduce((map, i) => map.set(selector(i), i), new Map()); return list.reduce((map, i) => map.set(selector(i), i), new Map());
} }
@@ -99,6 +101,6 @@ export function createMap(selector, list){
* @param {function} fn * @param {function} fn
* @returns {function(*=): null} * @returns {function(*=): null}
*/ */
export function nullSafe(fn){ export function nullSafe(fn) {
return i => i ? fn(i) : null; return i => i ? fn(i) : null;
} }