| import { map, mapValues } from 'lodash'; |
| import { TIMEZONES_REQUEST } from 'calypso/state/action-types'; |
| import { registerHandlers } from 'calypso/state/data-layer/handler-registry'; |
| import { http } from 'calypso/state/data-layer/wpcom-http/actions'; |
| import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils'; |
| import { timezonesReceive } from 'calypso/state/timezones/actions'; |
|
|
| const noop = () => {}; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const timezonePairsToMap = ( pairs ) => |
| Object.fromEntries( map( pairs, ( { label, value } ) => [ value, label ] ) ); |
|
|
| |
| |
| |
| |
| export const fromApi = ( { manual_utc_offsets, timezones, timezones_by_continent } ) => ( { |
| rawOffsets: timezonePairsToMap( manual_utc_offsets ), |
| labels: timezonePairsToMap( timezones ), |
| byContinents: mapValues( timezones_by_continent, ( zones ) => |
| map( zones, ( { value } ) => value ) |
| ), |
| } ); |
|
|
| |
| |
| |
| export const fetchTimezones = ( action ) => |
| http( |
| { |
| method: 'GET', |
| path: '/timezones', |
| apiNamespace: 'wpcom/v2', |
| }, |
| action |
| ); |
|
|
| export const addTimezones = ( action, data ) => timezonesReceive( data ); |
|
|
| registerHandlers( 'state/data-layer/wpcom/timezones/index.js', { |
| [ TIMEZONES_REQUEST ]: [ |
| dispatchRequest( { |
| fetch: fetchTimezones, |
| onSuccess: addTimezones, |
| onError: noop, |
| fromApi, |
| } ), |
| ], |
| } ); |
|
|