File size: 1,405 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
import { LEGAL_REQUEST, TOS_ACCEPT } from 'calypso/state/action-types';
import { recordTracksEvent } 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 { setLegalData } from 'calypso/state/legal/actions';

const requestLegalData = ( action ) => {
	return http(
		{
			method: 'GET',
			path: `/legal`,
			apiNamespace: 'wpcom/v2',
		},
		action
	);
};

const storeLegalData = ( action, legalData ) => [
	setLegalData( legalData ),
	recordTracksEvent( 'calypso_tos_accept' ),
];

const formatLegalData = ( { tos: { accepted, active_date, display_prompt } } ) => {
	return {
		tos: {
			accepted,
			activeDate: active_date,
			displayPrompt: display_prompt,
		},
	};
};

const acceptTos = ( action ) => {
	return http(
		{
			method: 'POST',
			path: `/legal`,
			apiNamespace: 'wpcom/v2',
			body: {
				action: 'accept_tos',
			},
		},
		action
	);
};

registerHandlers( 'state/data-layer/legal/index.js', {
	[ LEGAL_REQUEST ]: [
		dispatchRequest( {
			fetch: requestLegalData,
			onSuccess: storeLegalData,
			fromApi: formatLegalData,
		} ),
	],
	[ TOS_ACCEPT ]: [
		dispatchRequest( {
			fetch: acceptTos,
			onSuccess: storeLegalData,
			fromApi: formatLegalData,
		} ),
	],
} );