import { NextLinkType } from '@weco/common/model/next-link-type' ; import { parseCsv } from '@weco/common/utils/csv' ; import { isNotUndefined } from '../../utils/array' ; type Params = { [ key ] : any ; } ; export type UrlParams = { [ key ] : string ; } ; export function serialiseUrl ( params ) { return Object . keys ( params ) . reduce ( ( acc , key ) => { const val = params [ key ] ; if ( Array . isArray ( val ) ) { if ( val . length === 0 ) { return { ... acc , } ; } else { return { ... acc , [ key ] : val . join ( ',' ) , } ; } } if ( val === null || val === undefined || val === '' ) { return { ... acc } ; } if ( key === 'page' && val === 1 ) { return { ... acc } ; } return { ... acc , [ key ] : val . toString ( ) , } ; } , { } ) ; } function stringToCsv ( s ) { return s ? s . split ( ',' ) : [ ] ; } function maybeString ( s ) { return s || undefined ; } function defaultTo1 ( v ) { return typeof v === 'number' ? v : isNotUndefined ( v ) ? parseInt ( v , 10 ) : 1 ; } function defaultToEmptyString ( s ) { return s || '' ; } function quotedCsv ( v ) { return v ? parseCsv ( v ) : [ ] ; } type NextRoute < T > = { fromQuery : ( q : UrlParams ) => T ; toLink : ( t : T ) => NextLinkType ; toQuery : ( t : T ) => UrlParams ; } ; export type WorksRouteProps = { query : string ; page : number ; workType : string [ ] ; itemsLocationsLocationType : string [ ] ; availabilities : string [ ] ; sort ? : string ; sortOrder ? : string ; productionDatesFrom ? : string ; productionDatesTo ? : string ; imagesColor ? : string ; search ? : string ; source ? : string ; languages ? : string [ ] ; subjectsLabel ? : string [ ] ; genresLabel ? : string [ ] ; contributorsAgentLabel ? : string [ ] ; } ; export const WorksRoute = { fromQuery ( q ) { return { query : defaultToEmptyString ( q . query ) , page : defaultTo1 ( q . page ) , workType : stringToCsv ( q . workType ) , itemsLocationsLocationType : stringToCsv ( q [ 'items.locations.locationType' ] ) , availabilities : stringToCsv ( q [ 'availabilities' ] ) , sort : maybeString ( q . sort ) , sortOrder : maybeString ( q . sortOrder ) , productionDatesFrom : maybeString ( q [ 'production.dates.from' ] ) , productionDatesTo : maybeString ( q [ 'production.dates.to' ] ) , imagesColor : maybeString ( q [ 'images.color' ] ) , search : maybeString ( q . search ) , source : maybeString ( q . source ) , languages : stringToCsv ( q . languages ) , subjectsLabel : quotedCsv ( q [ 'subjects.label' ] ) , genresLabel : quotedCsv ( q [ 'genres.label' ] ) , contributorsAgentLabel : quotedCsv ( q [ 'contributors.agent.label' ] ) , } ; } , toLink ( params ) { const pathname = '/works' ; const { source , ... paramsWithoutSource } = params ; return { href : { pathname , query : WorksRoute . toQuery ( params ) , } , as : { pathname , query : WorksRoute . toQuery ( paramsWithoutSource ) , } , } ; } , toQuery ( params ) { return serialiseUrl ( { query : params . query , page : params . page , workType : params . workType , 'items.locations.locationType' : params . itemsLocationsLocationType , availabilities : params . availabilities , sort : params . sort , sortOrder : params . sortOrder , 'production.dates.from' : params . productionDatesFrom , 'production.dates.to' : params . productionDatesTo , 'images.color' : params . imagesColor , search : params . search , source : params . source , 'contributors.agent.label' : params . contributorsAgentLabel , } ) ; } , } ; export type WorkRouteProps = { id : string ; } ; export const WorkRoute = { fromQuery ( q ) { return { id : defaultToEmptyString ( q . id ) , } ; } , toLink ( params ) { const { id } = params ; return { href : { pathname : '/work' , query : { id } , } , as : { pathname : `/works/${ id }` , query : { } , } , } ; } , toQuery ( params ) { return serialiseUrl ( { id : params . id } ) ; } , } ; export type ItemRouteProps = { workId : string ; langCode : string ; canvas : number ; manifest ? : number ; sierraId ? : string ; isOverview ? : boolean ; page ? : number ; pageSize ? : number ; } ; export const ItemRoute = { fromQuery ( q ) { const { workId , langCode = 'eng' , canvas , sierraId , manifest , isOverview , page , pageSize , } = q ; return { workId : defaultToEmptyString ( workId ) , langCode , sierraId : maybeString ( sierraId ) , pageSize : pageSize ? parseInt ( pageSize , 10 ) : 4 , canvas : defaultTo1 ( canvas ) , manifest : defaultTo1 ( manifest ) , isOverview : Boolean ( isOverview ) , page : defaultTo1 ( page ) , } ; } , toLink ( params ) { const { workId } = params ; return { href : { pathname : `/item` , query : ItemRoute . toQuery ( params ) , } , as : { pathname : `/works/${ workId }/items` , query : ItemRoute . toQuery ( params ) , } , } ; } , toQuery ( params ) { return serialiseUrl ( params ) ; } , } ; export type ImageRouteProps = { id : string ; workId : string ; langCode : string ; } ; export const ImageRoute = { fromQuery ( q ) { const { workId , langCode = 'eng' , id } = q ; return { workId : defaultToEmptyString ( workId ) , langCode , id : defaultToEmptyString ( id ) , } ; } , toLink ( params ) { const { workId } = params ; return { href : { pathname : '/image' , query : ImageRoute . toQuery ( params ) , } , as : { pathname : `/works/${ workId }/images` , query : ImageRoute . toQuery ( params ) , } , } ; } , toQuery ( params ) { return serialiseUrl ( params ) ; } , } ; export const worksLink = ( params , source ) => WorksRoute . toLink ( { ... params , source } ) ; export const itemLink = ItemRoute . toLink ; export const imageLink = ImageRoute . toLink ;