File size: 1,455 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
import { translate } from 'i18n-calypso';
import { REWIND_ACTIVITY_SHARE_REQUEST } from 'calypso/state/action-types';
import { recordTracksEvent, withAnalytics } from 'calypso/state/analytics/actions';
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 { errorNotice, successNotice } from 'calypso/state/notices/actions';

const requestShare = ( action ) =>
	http(
		{
			apiNamespace: 'wpcom/v2',
			method: 'POST',
			path: `/sites/${ action.siteId }/activity/${ action.rewindId }/share`,
			body: { email: action.email },
		},
		action
	);

const successfulShare = ( { siteId, rewindId } ) =>
	withAnalytics(
		recordTracksEvent( 'calypso_activity_event_share_success', {
			site_id: siteId,
			rewind_id: rewindId,
		} ),
		successNotice( translate( "We've shared the event!" ) )
	);

const failedShare = ( { siteId, rewindId } ) =>
	withAnalytics(
		recordTracksEvent( 'calypso_activity_event_share_failed', {
			site_id: siteId,
			rewind_id: rewindId,
		} ),
		errorNotice( translate( 'The event failed to send, please try again.' ) )
	);

registerHandlers( 'state/data-layer/wpcom/activity-log/share/index.js', {
	[ REWIND_ACTIVITY_SHARE_REQUEST ]: [
		dispatchRequest( {
			fetch: requestShare,
			onSuccess: successfulShare,
			onError: failedShare,
		} ),
	],
} );