File size: 2,519 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
import config from '@automattic/calypso-config';
import { translate } from 'i18n-calypso';
import { EMAIL_ACCOUNTS_REQUEST } from 'calypso/state/action-types';
import { registerHandlers } from 'calypso/state/data-layer/handler-registry';
import { convertToCamelCase } from 'calypso/state/data-layer/utils';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';
import {
	receiveGetEmailAccountsSuccess,
	receiveGetEmailAccountsFailure,
} from 'calypso/state/email-accounts/actions';
import { errorNotice } from 'calypso/state/notices/actions';

export const getEmailAccounts = ( action ) => {
	if ( config.isEnabled( 'email-accounts/enabled' ) ) {
		return receiveGetEmailAccountsSuccess( action.siteId, {
			accounts: [
				{
					domain_name: 'domain.com',
					product_name: 'Titan Basic',
					product_slug: 'titan_basic',
					product_type: 'titan',
					site_id: action.siteId,
					mailboxes: [
						{
							name: 'one',
							first_name: 'One',
							last_name: 'Last',
							state: 'active',
						},
						{
							name: 'two',
							first_name: 'Two',
							last_name: 'Last',
							state: 'active',
						},
					],
				},
				{
					domain_name: 'other-domain.com',
					product_name: 'G Suite Business',
					product_slug: 'gapps_unlimited',
					product_type: 'gapps',
					site_id: action.siteId,
					mailboxes: [
						{
							name: 'three',
							first_name: 'Three',
							last_name: 'Last',
							state: 'suspended',
							meta: {
								has_agreed_to_terms: true,
							},
						},
					],
				},
			],
		} );
	}

	return http(
		{
			method: 'GET',
			path: `/sites/${ action.siteId }/email-accounts`,
		},
		action
	);
};

export const getEmailAccountsFailure = ( action, error ) => {
	return [
		errorNotice( translate( 'Failed to retrieve email accounts' ) ),
		receiveGetEmailAccountsFailure( action.siteId, error ),
	];
};

export const getEmailAccountsSuccess = ( action, response ) => {
	if ( response ) {
		return receiveGetEmailAccountsSuccess( action.siteId, response );
	}
	return getEmailAccountsFailure( action, {
		message: 'Failed to retrieve your email accounts. No response was received',
	} );
};

registerHandlers( 'state/data-layer/wpcom/email-accounts/index.js', {
	[ EMAIL_ACCOUNTS_REQUEST ]: [
		dispatchRequest( {
			fetch: getEmailAccounts,
			onSuccess: getEmailAccountsSuccess,
			onError: getEmailAccountsFailure,
			fromApi: convertToCamelCase,
		} ),
	],
} );