From ed9dae3760b2fb0b48a6f995ea169ae564502258 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 15 Oct 2019 02:12:56 +0300 Subject: [PATCH] documents page fixed --- pages/{documents.jsx => documents.tsx} | 49 +++++++++++++++-------- pages/index.tsx | 2 +- src/components/DocumentList.js | 18 --------- src/components/DocumentList/index.tsx | 22 ++++++++++ src/components/DocumentListItem.js | 23 ----------- src/components/DocumentListItem/index.tsx | 30 ++++++++++++++ src/core/{Document.js => Document.ts} | 28 +++++++++---- src/core/db.ts | 2 +- src/lib/file.js | 27 ------------- src/lib/file.ts | 23 +++++++++++ src/{next-lib.js => next-lib.ts} | 10 +++-- src/types.ts | 9 +++++ src/utils/{common.js => common.ts} | 18 +++++---- 13 files changed, 156 insertions(+), 105 deletions(-) rename pages/{documents.jsx => documents.tsx} (74%) delete mode 100644 src/components/DocumentList.js create mode 100644 src/components/DocumentList/index.tsx delete mode 100644 src/components/DocumentListItem.js create mode 100644 src/components/DocumentListItem/index.tsx rename src/core/{Document.js => Document.ts} (68%) delete mode 100644 src/lib/file.js create mode 100644 src/lib/file.ts rename src/{next-lib.js => next-lib.ts} (81%) create mode 100644 src/types.ts rename src/utils/{common.js => common.ts} (82%) diff --git a/pages/documents.jsx b/pages/documents.tsx similarity index 74% rename from pages/documents.jsx rename to pages/documents.tsx index 618a6c9c..97e82696 100644 --- a/pages/documents.jsx +++ b/pages/documents.tsx @@ -5,12 +5,19 @@ import { Meta } from '../src/components/Meta' import menuModel from '../src/models/menu' import { buildMenu } from '../src/lib/menu' 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 { 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 .map(fn => fn) .reverse() @@ -23,7 +30,7 @@ function getSorted(documents) { const subtract = (a, b) => a - b - const uniqueCategories = unique(i => i.category) + const uniqueCategories = unique(i => i.category) const priority = { 'Основные документы': [ @@ -53,13 +60,16 @@ function getSorted(documents) { 'Документы для поступления', ] - const sortBy = fn => (a, b) => [a, b] - .map(fn) - .reduce(subtract) + function sortBy(fn: (value: T) => number): (a: T, b: T) => number { + return (a, b) => [a, b].map(fn).reduce(subtract) + } - const getPriorityFn = (m, list) => value => list.includes(value) - ? list.indexOf(value) - : m + function getPriorityFn(m: number, list: T[]): (value: T) => number { + return (value: T) => { + const has = list.includes(value) + return has ? list.indexOf(value) : m + } + } const safeSelect = (d, store, key) => key in store ? 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 = props => ( (
- {props.collections.map(({ name, items }, index) => ( + {props.collections.map((collection, index) => ( ))}
@@ -109,8 +126,8 @@ const Page = (props) => ( Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = '/documents' - const res = await requestGet(createApiUrl(ctx.req, '/api/files'), {}) - const files = res.items || [] + const res = await requestGet>(createApiUrl(ctx.req, '/api/files'), { items: [] }) + const files = res.items const title = 'Документы' const collections = getSorted(files) diff --git a/pages/index.tsx b/pages/index.tsx index df3e7fa4..af7215cc 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -36,7 +36,7 @@ Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = '/' const page = 1 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 nextPage = res.nextPage const prevPage = res.prevPage diff --git a/src/components/DocumentList.js b/src/components/DocumentList.js deleted file mode 100644 index bc33069d..00000000 --- a/src/components/DocumentList.js +++ /dev/null @@ -1,18 +0,0 @@ -const React = require('react') -const Document = require('./DocumentListItem') - -const DocumentList = ({documents, name}) => ( -
-
-

{name}

-
- -
- {documents.map((d, i) => ( - - ))} -
-
-) - -module.exports = DocumentList diff --git a/src/components/DocumentList/index.tsx b/src/components/DocumentList/index.tsx new file mode 100644 index 00000000..541e1d12 --- /dev/null +++ b/src/components/DocumentList/index.tsx @@ -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 = props => ( +
+
+

{props.name}

+
+ +
+ {props.documents.map((d, i) => ( + + ))} +
+
+) diff --git a/src/components/DocumentListItem.js b/src/components/DocumentListItem.js deleted file mode 100644 index 21ef96d2..00000000 --- a/src/components/DocumentListItem.js +++ /dev/null @@ -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}) => ( - -) - -module.exports = Document diff --git a/src/components/DocumentListItem/index.tsx b/src/components/DocumentListItem/index.tsx new file mode 100644 index 00000000..04090a36 --- /dev/null +++ b/src/components/DocumentListItem/index.tsx @@ -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 = props => ( + +) diff --git a/src/core/Document.js b/src/core/Document.ts similarity index 68% rename from src/core/Document.js rename to src/core/Document.ts index 27e9e558..6c19aae1 100644 --- a/src/core/Document.js +++ b/src/core/Document.ts @@ -1,12 +1,12 @@ -const Data = require('./Data') -const Image = require('./Image') -const { find, findOne } = require('../lib/store') +import Data from './Data' +import Image from './Image' +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 = {}) { - const items = await find(store(), query, options) + const items = await find(store(), query, options as any) return Promise.all(items.map(processDocument)) } @@ -18,6 +18,17 @@ class Document { 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) { this.id = data.id this.hash = data.hash @@ -37,6 +48,9 @@ class Document { } class DocumentFile { + public name: any + public size: any + constructor(data) { this.name = data.name this.size = data.size @@ -52,5 +66,3 @@ const processDocument = async data => { preview, }) } - -module.exports = Document diff --git a/src/core/db.ts b/src/core/db.ts index 7770ed1a..4c4b5acb 100644 --- a/src/core/db.ts +++ b/src/core/db.ts @@ -41,7 +41,7 @@ function init(db) { Data.setStore('Album', db.collection('albums')) Data.setStore('Image', db.collection('images')) Data.setStore('Teacher', db.collection('collective')) - Data.setStore(Document, db.collection('documents')) + Data.setStore('Document', db.collection('documents')) Data.setStore('Settings', db.collection('settings')) return db diff --git a/src/lib/file.js b/src/lib/file.js deleted file mode 100644 index 4079f9b8..00000000 --- a/src/lib/file.js +++ /dev/null @@ -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 diff --git a/src/lib/file.ts b/src/lib/file.ts new file mode 100644 index 00000000..504cc26d --- /dev/null +++ b/src/lib/file.ts @@ -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]}` +} diff --git a/src/next-lib.js b/src/next-lib.ts similarity index 81% rename from src/next-lib.js rename to src/next-lib.ts index 3694918b..626adb36 100644 --- a/src/next-lib.js +++ b/src/next-lib.ts @@ -1,8 +1,12 @@ import axios from 'axios' -export async function requestGet(url, defaultResponse) { +export interface IResponseItems { + items: T[] +} + +export async function requestGet(url: string, defaultResponse: T): Promise { try { - const res = await axios.get(url) + const res = await axios.get(url) return res.data } catch (e) { @@ -19,7 +23,7 @@ export async function requestGet(url, defaultResponse) { export function wrapInitialProps(fn) { return async ctx => { if (process.env.NODE_ENV === 'production' && process.browser) { - return __NEXT_DATA__.props.pageProps; + return window['__NEXT_DATA__'].props.pageProps; } return fn(ctx) diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 00000000..cd3580e1 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,9 @@ +export interface IDocument { + category: string + fileName: string + fileSize: number + fileUrl: string + imageUrl: string + title: string + url: string +} diff --git a/src/utils/common.js b/src/utils/common.ts similarity index 82% rename from src/utils/common.js rename to src/utils/common.ts index ee15c886..a2ec4754 100644 --- a/src/utils/common.js +++ b/src/utils/common.ts @@ -40,7 +40,7 @@ export function indexEquals(withNumber) { * @param map */ 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 * @returns {*} */ -export function choise(scope, selector){ +export function choise(scope, selector) { return scope.reduce(selector, null); } -export function unique(fn) { - return list => [...new Set(list.map(fn))]; +export function unique(fn: (value: T) => U): (list: T[]) => U[] { + return list => Array.from( + new Set(list.map(fn)) + ) } export function branch(fn) { let keys = unique(fn); return list => keys(list) - .reduce((dict, key) => { + .reduce((dict: any, key: any) => { dict[key] = list.filter( 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; return dict; } @@ -88,7 +90,7 @@ export function map(v, fn) { * @param selector * @returns {*} */ -export function createMap(selector, list){ +export function createMap(selector, list) { return list.reduce((map, i) => map.set(selector(i), i), new Map()); } @@ -99,6 +101,6 @@ export function createMap(selector, list){ * @param {function} fn * @returns {function(*=): null} */ -export function nullSafe(fn){ +export function nullSafe(fn) { return i => i ? fn(i) : null; }