File size: 1,996 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import config from '@automattic/calypso-config';
import { withStorageKey } from '@automattic/state-utils';
import { isEqual } from 'lodash';
import {
DOCUMENT_HEAD_LINK_SET,
DOCUMENT_HEAD_META_SET,
DOCUMENT_HEAD_TITLE_SET,
DOCUMENT_HEAD_UNREAD_COUNT_SET,
ROUTE_SET,
} from 'calypso/state/action-types';
import { combineReducers, withSchemaValidation } from 'calypso/state/utils';
import { titleSchema, unreadCountSchema, linkSchema, metaSchema } from './schema';
/**
* Constants
*/
export const DEFAULT_META_STATE = config( 'meta' );
export const title = withSchemaValidation( titleSchema, ( state = '', action ) => {
switch ( action.type ) {
case DOCUMENT_HEAD_TITLE_SET:
return action.title;
}
return state;
} );
export const unreadCount = withSchemaValidation( unreadCountSchema, ( state = 0, action ) => {
switch ( action.type ) {
case DOCUMENT_HEAD_UNREAD_COUNT_SET:
return action.count;
case ROUTE_SET:
return 0;
}
return state;
} );
export const meta = withSchemaValidation( metaSchema, ( state = DEFAULT_META_STATE, action ) => {
switch ( action.type ) {
case DOCUMENT_HEAD_META_SET:
return action.meta;
}
return state;
} );
export const link = withSchemaValidation( linkSchema, ( state = [], action ) => {
switch ( action.type ) {
case DOCUMENT_HEAD_LINK_SET: {
if ( ! action.link ) {
return state;
}
// Append action.link to the state array and prevent duplicate objects.
// Works with action.link being a single link object or an array of link objects.
const links = Array.isArray( action.link ) ? action.link : [ action.link ];
return links.reduce( ( accuState, newLink ) => {
const isNew = ! accuState.some( ( accuLink ) => isEqual( accuLink, newLink ) );
return isNew ? [ ...accuState, newLink ] : accuState;
}, state );
}
}
return state;
} );
const combinedReducer = combineReducers( {
link,
meta,
title,
unreadCount,
} );
export default withStorageKey( 'documentHead', combinedReducer );
|