|
2 | 2 | (function ($) {
|
3 | 3 | const params = window.mailchimp_sf_admin_params || {};
|
4 | 4 | const oauthBaseUrl = 'https://woocommerce.mailchimpapp.com';
|
| 5 | + const spinner = '.mailchimp-sf-oauth-connect-wrapper .spinner'; |
| 6 | + const errorSelector = '.mailchimp-sf-oauth-section .oauth-error'; |
5 | 7 |
|
6 | 8 | /**
|
7 | 9 | * Open Mailchimp OAuth popup.
|
|
31 | 33 | if (popup.closed) {
|
32 | 34 | // Clear interval.
|
33 | 35 | window.clearInterval(oauthInterval);
|
34 |
| - // TODO: Hide/show error/loading messages. |
35 | 36 |
|
36 | 37 | // Check status of OAuth connection.
|
37 | 38 | $.post(`${oauthBaseUrl}/api/status/${token}`, function (statusData) {
|
|
52 | 53 | 'Error calling OAuth finish endpoint. Data:',
|
53 | 54 | finishResponse,
|
54 | 55 | );
|
| 56 | + if (finishResponse.data && finishResponse.data.message) { |
| 57 | + $(errorSelector).html(finishResponse.data.message); |
| 58 | + } else { |
| 59 | + $(errorSelector).html(params.generic_error); |
| 60 | + } |
| 61 | + $(errorSelector).show(); |
55 | 62 | }
|
56 | 63 | }).fail(function () {
|
57 | 64 | console.error('Error calling OAuth finish endpoint.');
|
| 65 | + $(errorSelector).html(params.generic_error); |
| 66 | + $(errorSelector).show(); |
58 | 67 | });
|
59 | 68 | } else {
|
60 | 69 | console.log(
|
61 | 70 | 'Error calling OAuth status endpoint. No credentials provided at login popup? Data:',
|
62 | 71 | statusData,
|
63 | 72 | );
|
64 | 73 | }
|
| 74 | + $(spinner).removeClass('is-active'); |
65 | 75 | }).fail(function () {
|
| 76 | + $(errorSelector).html(params.generic_error); |
| 77 | + $(errorSelector).show(); |
66 | 78 | console.error('Error calling OAuth status endpoint.');
|
| 79 | + $(spinner).removeClass('is-active'); |
67 | 80 | });
|
68 | 81 | }
|
69 | 82 | }, 250);
|
|
73 | 86 | $(window).on('load', function () {
|
74 | 87 | // Mailchimp OAuth connection.
|
75 | 88 | $('#mailchimp_sf_oauth_connect').click(function () {
|
76 |
| - $('.mailchimp-sf-oauth-section .oauth-error').html(''); |
| 89 | + $(errorSelector).hide(); |
| 90 | + $(errorSelector).html(''); |
| 91 | + $(spinner).addClass('is-active'); |
| 92 | + |
77 | 93 | $.post(
|
78 | 94 | params.ajax_url,
|
79 | 95 | {
|
|
85 | 101 | // Open Mailchimp OAuth popup.
|
86 | 102 | openMailChimpOauthPopup(response.data.token);
|
87 | 103 | } else {
|
88 |
| - // eslint-disable-next-line no-console |
89 |
| - console.error(response.data); |
90 | 104 | if (response.data && response.data.message) {
|
91 |
| - $('.mailchimp-sf-oauth-section .oauth-error').html( |
92 |
| - response.data.message, |
93 |
| - ); |
| 105 | + $(errorSelector).html(response.data.message); |
94 | 106 | } else {
|
95 |
| - $('.mailchimp-sf-oauth-section .oauth-error').html( |
96 |
| - 'An error occurred. Please try again.', |
97 |
| - ); |
| 107 | + $(errorSelector).html(params.generic_error); |
98 | 108 | }
|
| 109 | + $(errorSelector).show(); |
| 110 | + $(spinner).removeClass('is-active'); |
99 | 111 | }
|
100 | 112 | },
|
101 | 113 | ).fail(function () {
|
102 |
| - $('.mailchimp-sf-oauth-section .oauth-error').html( |
103 |
| - 'An error occurred. Please try again.', |
104 |
| - ); |
| 114 | + $(errorSelector).html(params.generic_error); |
| 115 | + $(errorSelector).show(); |
| 116 | + $(spinner).removeClass('is-active'); |
105 | 117 | });
|
106 | 118 | });
|
107 | 119 | });
|
|
0 commit comments