File size: 1,960 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 77 78 |
import { combineReducers } from '@wordpress/data';
import { DataStatus } from './constants';
import { stringifyDomainQueryObject } from './utils';
import type { Action } from './actions';
import type { DomainCategory, DomainSuggestionState, DomainAvailabilities } from './types';
import type { Reducer } from 'redux';
const initialDomainSuggestionState: DomainSuggestionState = {
state: DataStatus.Uninitialized,
data: {},
errorMessage: null,
lastUpdated: -Infinity,
pendingSince: undefined,
};
export const domainSuggestions: Reducer< DomainSuggestionState, Action > = (
state = initialDomainSuggestionState,
action
) => {
if ( action.type === 'FETCH_DOMAIN_SUGGESTIONS' ) {
return {
...state,
state: DataStatus.Pending,
errorMessage: null,
pendingSince: action.timeStamp,
};
}
if ( action.type === 'RECEIVE_DOMAIN_SUGGESTIONS_SUCCESS' ) {
return {
...state,
state: DataStatus.Success,
data: {
...state.data,
[ stringifyDomainQueryObject( action.queryObject ) ]: action.suggestions,
},
errorMessage: null,
lastUpdated: action.timeStamp,
pendingSince: undefined,
};
}
if ( action.type === 'RECEIVE_DOMAIN_SUGGESTIONS_ERROR' ) {
return {
...state,
state: DataStatus.Failure,
errorMessage: action.errorMessage,
lastUpdated: action.timeStamp,
pendingSince: undefined,
};
}
return state;
};
const categories: Reducer< DomainCategory[], Action > = ( state = [], action ) => {
if ( action.type === 'RECEIVE_CATEGORIES' ) {
return action.categories;
}
return state;
};
const availability: Reducer< DomainAvailabilities, Action > = ( state = {}, action ) => {
if ( action.type === 'RECEIVE_DOMAIN_AVAILABILITY' ) {
return {
...state,
[ action.domainName ]: action.availability,
};
}
return state;
};
const reducer = combineReducers( { categories, domainSuggestions, availability } );
export type State = ReturnType< typeof reducer >;
export default reducer;
|