import * as express from 'express' ; import * as Next from 'next' ; import * as categories from '../../constants/categories' ; import { findCategory , getLength } from '../utils' ; export const renderHomepage = ( app ) => ( req , res ) => { const { search } = req . query ; const searchQuery = search && { search : search . replace ( '+' , ' ' ) } ; const defaultQuery = { ... searchQuery , sort : 'top' } ; return app . render ( req , res , '/' , defaultQuery ) ; } ; export const renderOneLevel = ( app ) => ( req , res , next ) => { const { cat } = req . params ; const findIn = findCategory ( categories , cat ) ; const query = { category : findIn ( 'categories' ) , sort : findIn ( 'sorts' ) , type : findIn ( 'types' ) , } ; const size = getLength ( query ) ; if ( ! size ) return next ( ) ; return app . render ( req , res , '/' , query ) ; } ; export const renderTwoLevel = ( app ) => ( req , res , next ) => { const { first , second } = req . params ; if ( first === '_next' ) return next ( ) ; const findFirstIn = findCategory ( categories , first ) ; const findSecondIn = findCategory ( categories , second ) ; const query = { category : findSecondIn ( 'categories' ) , sort : findSecondIn ( 'sorts' ) , type : findFirstIn ( 'types' ) , } ; if ( ! query . type || ( ! query . sort && ! query . category ) ) return next ( ) ; return app . render ( req , res , '/' , query ) ; } ; export const renderThreeLevel = ( app ) => ( req , res , next ) => { const { category , sort , type } = req . params ; const query = { category : findCategory ( categories , category ) ( 'categories' ) , sort : findCategory ( categories , sort ) ( 'sorts' ) , type : findCategory ( categories , type ) ( 'types' ) , } ; const size = getLength ( query ) ; if ( size !== 3 ) return next ( ) ; return app . render ( req , res , '/' , query ) ; } ; export const renderSingle = ( app ) => ( req , res ) => app . render ( req , res , '/single' , req . params ) ;