File size: 6,481 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import config from '@automattic/calypso-config';
import moment from 'moment/moment';
import makeJsonSchemaParser from 'calypso/lib/make-json-schema-parser';
import { JITM_DISMISS, JITM_FETCH } 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 { setJetpackConnectionMaybeUnhealthy } from 'calypso/state/jetpack-connection-health/actions';
import { clearJITM, insertJITM } from 'calypso/state/jitm/actions';
import getEnvStatsFeatureSupportChecksMemoized from 'calypso/state/sites/selectors/get-env-stats-feature-supports';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import schema from './schema.json';
const noop = () => {};
// Clean this up when we release a major version of Odyssey Stats e.g. v1.1.
const isRunningInLegacyJetpackSite =
config.isEnabled( 'is_running_in_jetpack_site' ) &&
! getEnvStatsFeatureSupportChecksMemoized( config( 'intial_state' ), config( 'blog_id' ) )
.supportsWpcomV3Jitm;
const jitmSchema = ! isRunningInLegacyJetpackSite
? schema
: {
$schema: 'http://json-schema.org/draft-04/schema#',
properties: {
data: {
type: 'array',
items: schema.items,
},
},
};
/**
* Existing libraries do not escape decimal encoded entities that php encodes, this handles that.
* @param {string} str The string to decode
* @returns {string} The decoded string
*/
const unescapeDecimalEntities = ( str ) =>
str.replace( /&#(\d+);/g, ( _, entity ) => String.fromCharCode( entity ) );
/**
* Given an object from the api, prepare it to be consumed by the ui by transforming the shape of the data
* @param {Object} jitms The response object from the jitms endpoint
* @returns {Object} The transformed data to display
*/
export const transformApiRequest = ( jitms ) => {
// Different shape of data between Calypso and Jetpack.
if ( isRunningInLegacyJetpackSite && jitms && jitms.data ) {
jitms = jitms.data;
}
return jitms.map( ( jitm ) => ( {
message: unescapeDecimalEntities( jitm.content.message || '' ),
description: unescapeDecimalEntities( jitm.content.description || '' ),
classes: unescapeDecimalEntities( jitm.content.classes || '' ),
icon: unescapeDecimalEntities( jitm.content.icon || '' ),
iconPath: unescapeDecimalEntities( jitm.content.iconPath || '' ),
featureClass: jitm.feature_class,
CTA: {
message: unescapeDecimalEntities( jitm.CTA.message ),
link: unescapeDecimalEntities( jitm.CTA.link || jitm.url ),
target: unescapeDecimalEntities(
jitm.CTA.target || '' === jitm.CTA.target ? jitm.CTA.target : '_blank'
),
},
tracks: jitm.tracks,
action: jitm.action,
template: jitm.template,
id: jitm.id,
isDismissible: jitm.is_dismissible,
messageExpiration: jitm.message_expiration ? moment( jitm.message_expiration ) : null,
title: unescapeDecimalEntities( jitm.content.title || '' ),
disclaimer: jitm.content.disclaimer.map( unescapeDecimalEntities ),
} ) );
};
/**
* Processes the current state and determines if it should fire a jitm request
* @param {Object} action The fetch action
* @returns {Object} The HTTP fetch action
*/
export const doFetchJITM = ( action ) =>
http(
{
method: 'GET',
apiNamespace: 'wpcom/v3',
path: config.isEnabled( 'is_running_in_jetpack_site' )
? '/jitm'
: `/sites/${ action.siteId }/jitm`,
query: {
message_path: action.messagePath,
query: action.searchQuery,
locale: action.locale,
},
isLocalApiCall: true, // required to use the wpcom/v3 namespace
},
{ ...action }
);
/**
* Dismisses a JITM
* @param {Object} action The dismissal action
* @returns {Object} The HTTP fetch action
*/
export const doDismissJITM = ( action ) =>
http(
{
method: 'POST',
apiNamespace: 'wpcom/v3',
path: config.isEnabled( 'is_running_in_jetpack_site' )
? '/jitm'
: `/sites/${ action.siteId }/jitm`,
body: {
feature_class: action.featureClass,
id: action.id,
},
isLocalApiCall: true, // required to use the wpcom/v3 namespace
},
action
);
/**
* Called when the http layer receives a valid jitm
* @param {Object} action action object
* @param {number} action.siteId The site id
* @param {string} action.messagePath The jitm message path (ex: calypso:comments:admin_notices)
* @param {Array} jitms The jitms
* @returns {Function} a handler for the request
*/
export const receiveJITM = ( action, jitms ) => ( dispatch, getState ) => {
const siteId = action.siteId || action.site_id || getSelectedSiteId( getState() );
dispatch( insertJITM( siteId, action.messagePath, jitms ) );
};
/**
* Called when a jitm fails for any network related reason
* @param {Object} action action object
* @param {number} action.siteId The site id
* @param {string} action.messagePath The jitm message path (ex: calypso:comments:admin_notices)
* @returns {Function} a handler for the failed request
*/
export const failedJITM = ( action ) => ( dispatch, getState ) => {
const siteId = action.siteId || action.site_id || getSelectedSiteId( getState() );
dispatch( setJetpackConnectionMaybeUnhealthy( siteId ) );
dispatch( clearJITM( siteId, action.messagePath ) );
};
// Clean this up when we release a major version of Odyssey Stats e.g. v1.1.
const doJetpackFetchJITM = ( action ) =>
http(
{
method: 'GET',
apiNamespace: 'jetpack/v4',
path: '/jitm',
query: {
message_path: action.messagePath,
query: action.searchQuery,
},
locale: action.locale,
},
{ ...action }
);
// Clean this up when we release a major version of Odyssey Stats e.g. v1.1.
const doJetpackDismissJITM = ( action ) =>
http(
{
method: 'POST',
apiNamespace: 'jetpack/v4',
path: '/jitm',
body: JSON.stringify( {
feature_class: action.featureClass,
id: action.id,
} ),
json: false,
},
action
);
registerHandlers( 'state/data-layer/wpcom/sites/jitm/index.js', {
[ JITM_FETCH ]: [
dispatchRequest( {
fetch: ! isRunningInLegacyJetpackSite ? doFetchJITM : doJetpackFetchJITM,
onSuccess: receiveJITM,
onError: failedJITM,
fromApi: makeJsonSchemaParser( jitmSchema, transformApiRequest ),
} ),
],
[ JITM_DISMISS ]: [
dispatchRequest( {
fetch: ! isRunningInLegacyJetpackSite ? doDismissJITM : doJetpackDismissJITM,
onSuccess: noop,
onError: noop,
} ),
],
} );
|