Skip to content

Commit 2f74dd6

Browse files
refactor(cc-email-list)!: rework properties to avoid impossible states
BREAKING CHANGE: the properties have changed - `emails` property has been renamed to `emailListState`, - `emails.state` property has been renamed to `emailListState.type`, - `emails.value` property has been renamed to `emailListState.emailList`, - same logic applies to `secondaryAddressState` where `state` has been renamed to `type`. Fixes #1163
1 parent 4245007 commit 2f74dd6

File tree

4 files changed

+133
-109
lines changed

4 files changed

+133
-109
lines changed

src/components/cc-email-list/cc-email-list.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import '../cc-notice/cc-notice.js';
2929

3030
/** @type {PrimaryAddressState} */
3131
const SKELETON_PRIMARY_EMAIL = {
32-
state: 'idle',
32+
type: 'idle',
3333
address: fakeString(35),
3434
verified: false,
3535
};
@@ -88,7 +88,7 @@ export class CcEmailList extends LitElement {
8888
static get properties() {
8989
return {
9090
addEmailFormState: { type: Object, attribute: false },
91-
emails: { type: Object },
91+
emailListState: { type: Object, attribute: false },
9292
};
9393
}
9494

@@ -99,7 +99,7 @@ export class CcEmailList extends LitElement {
9999
this.addEmailFormState = { type: 'idle' };
100100

101101
/** @type {EmailListState} State of the component. */
102-
this.emails = { state: 'loading' };
102+
this.emailListState = { type: 'loading' };
103103

104104
/** @type {CcInputTextRef} */
105105
this._addressInputRef = createRef();
@@ -133,8 +133,8 @@ export class CcEmailList extends LitElement {
133133
}
134134

135135
_onSendConfirmationEmail() {
136-
if (this.emails.state === 'loaded') {
137-
dispatchCustomEvent(this, 'send-confirmation-email', this.emails.value.primaryAddress.address);
136+
if (this.emailListState.type === 'loaded') {
137+
dispatchCustomEvent(this, 'send-confirmation-email', this.emailListState.emailList.primaryAddress.address);
138138
}
139139
}
140140

@@ -181,19 +181,19 @@ export class CcEmailList extends LitElement {
181181
<cc-block>
182182
<div slot="header-title">${i18n('cc-email-list.title')}</div>
183183
184-
${this.emails.state === 'loading'
184+
${this.emailListState.type === 'loading'
185185
? html`
186186
${this._renderPrimarySection(SKELETON_PRIMARY_EMAIL, true)}
187187
${this._renderSecondarySection(SKELETON_SECONDARY_EMAILS)}
188188
`
189189
: ''}
190-
${this.emails.state === 'loaded'
190+
${this.emailListState.type === 'loaded'
191191
? html`
192-
${this._renderPrimarySection(this.emails.value.primaryAddress)}
193-
${this._renderSecondarySection(this.emails.value.secondaryAddresses)}
192+
${this._renderPrimarySection(this.emailListState.emailList.primaryAddress)}
193+
${this._renderSecondarySection(this.emailListState.emailList.secondaryAddresses)}
194194
`
195195
: ''}
196-
${this.emails.state === 'error'
196+
${this.emailListState.type === 'error'
197197
? html`
198198
<cc-notice slot="content" intent="warning" message="${i18n('cc-email-list.loading.error')}"></cc-notice>
199199
`
@@ -233,7 +233,7 @@ export class CcEmailList extends LitElement {
233233
? html`
234234
<cc-button
235235
@cc-button:click=${this._onSendConfirmationEmail}
236-
?waiting=${primaryAddressState.state === 'sending-confirmation-email'}
236+
?waiting=${primaryAddressState.type === 'sending-confirmation-email'}
237237
link
238238
>
239239
${i18n('cc-email-list.primary.action.resend-confirmation-email')}
@@ -250,7 +250,7 @@ export class CcEmailList extends LitElement {
250250
*/
251251
_renderSecondarySection(secondaryAddressStates) {
252252
const addresses = [...secondaryAddressStates].sort(sortBy('address'));
253-
const isOneRowMarkingPrimary = addresses.some((item) => item.state === 'marking-as-primary');
253+
const isOneRowMarkingPrimary = addresses.some((item) => item.type === 'marking-as-primary');
254254

255255
return html`
256256
<cc-block-section slot="content-body">
@@ -259,8 +259,8 @@ export class CcEmailList extends LitElement {
259259
260260
<ul class="secondary-addresses">
261261
${addresses.map((secondaryAddress) => {
262-
const isBusy = secondaryAddress.state === 'marking-as-primary' || secondaryAddress.state === 'deleting';
263-
const isDisabled = (isOneRowMarkingPrimary || isBusy) && secondaryAddress.state !== 'marking-as-primary';
262+
const isBusy = secondaryAddress.type === 'marking-as-primary' || secondaryAddress.type === 'deleting';
263+
const isDisabled = (isOneRowMarkingPrimary || isBusy) && secondaryAddress.type !== 'marking-as-primary';
264264
265265
return html`
266266
<li class="address-line secondary">
@@ -271,7 +271,7 @@ export class CcEmailList extends LitElement {
271271
<div class="buttons">
272272
<cc-button
273273
@cc-button:click=${() => this._onMarkAsPrimary(secondaryAddress.address)}
274-
?waiting="${secondaryAddress.state === 'marking-as-primary'}"
274+
?waiting="${secondaryAddress.type === 'marking-as-primary'}"
275275
?disabled="${isDisabled}"
276276
a11y-name="${i18n('cc-email-list.secondary.action.mark-as-primary.accessible-name', {
277277
address: secondaryAddress.address,
@@ -285,8 +285,8 @@ export class CcEmailList extends LitElement {
285285
outlined
286286
.icon=${iconDelete}
287287
@cc-button:click=${() => this._onDelete(secondaryAddress.address)}
288-
?waiting="${secondaryAddress.state === 'deleting'}"
289-
?disabled="${isBusy && secondaryAddress.state !== 'deleting'}"
288+
?waiting="${secondaryAddress.type === 'deleting'}"
289+
?disabled="${isBusy && secondaryAddress.type !== 'deleting'}"
290290
a11y-name="${i18n('cc-email-list.secondary.action.delete.accessible-name', {
291291
address: secondaryAddress.address,
292292
})}"

src/components/cc-email-list/cc-email-list.smart.js

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ defineSmartComponent({
2727
* @param {OnContextUpdateArgs} args
2828
*/
2929
onContextUpdate({ component, context, onEvent, updateComponent, signal }) {
30-
updateComponent('emails', { state: 'loading' });
30+
updateComponent('emailListState', { type: 'loading' });
3131
updateComponent('addEmailFormState', { type: 'idle' });
3232
component.resetAddEmailForm();
3333

3434
const api = getApi(context.apiConfig, signal);
3535

3636
/**
3737
* @param {string} address
38-
* @param {(state: SecondaryAddressState) => void}callback
38+
* @param {(emailListState: SecondaryAddressState) => void} callback
3939
*/
4040
function updateSecondary(address, callback) {
4141
updateComponent(
42-
'emails',
43-
/** @param {EmailListStateLoaded} emails */
44-
(emails) => {
45-
const secondaryState = emails.value.secondaryAddresses.find((a) => a.address === address);
42+
'emailListState',
43+
/** @param {EmailListStateLoaded} emailListState */
44+
(emailListState) => {
45+
const secondaryState = emailListState.emailList.secondaryAddresses.find((a) => a.address === address);
4646
if (secondaryState != null) {
4747
callback(secondaryState);
4848
}
@@ -53,16 +53,16 @@ defineSmartComponent({
5353
api
5454
.fetchEmailAddresses()
5555
.then(({ self, secondary }) => {
56-
updateComponent('emails', {
57-
state: 'loaded',
58-
value: {
56+
updateComponent('emailListState', {
57+
type: 'loaded',
58+
emailList: {
5959
primaryAddress: {
60-
state: 'idle',
60+
type: 'idle',
6161
address: self.email,
6262
verified: self.emailValidated,
6363
},
6464
secondaryAddresses: secondary.map((secondaryAddress) => ({
65-
state: 'idle',
65+
type: 'idle',
6666
address: secondaryAddress,
6767
verified: true,
6868
})),
@@ -71,15 +71,15 @@ defineSmartComponent({
7171
})
7272
.catch((error) => {
7373
console.error(error);
74-
updateComponent('emails', { state: 'error' });
74+
updateComponent('emailListState', { type: 'error' });
7575
});
7676

7777
onEvent('cc-email-list:send-confirmation-email', (address) => {
7878
updateComponent(
79-
'emails',
80-
/** @param {EmailListStateLoaded} emails */
81-
(emails) => {
82-
emails.value.primaryAddress.state = 'sending-confirmation-email';
79+
'emailListState',
80+
/** @param {EmailListStateLoaded} emailListState */
81+
(emailListState) => {
82+
emailListState.emailList.primaryAddress.type = 'sending-confirmation-email';
8383
},
8484
);
8585

@@ -105,10 +105,10 @@ defineSmartComponent({
105105
)
106106
.finally(() => {
107107
updateComponent(
108-
'emails',
109-
/** @param {EmailListStateLoaded} emails */
110-
(emails) => {
111-
emails.value.primaryAddress.state = 'idle';
108+
'emailListState',
109+
/** @param {EmailListStateLoaded} emailListState */
110+
(emailListState) => {
111+
emailListState.emailList.primaryAddress.type = 'idle';
112112
},
113113
);
114114
});
@@ -117,9 +117,9 @@ defineSmartComponent({
117117
onEvent('cc-email-list:add', (address) => {
118118
updateComponent(
119119
'addEmailFormState',
120-
/** @param {AddEmailFormState} state */
121-
(state) => {
122-
state.type = 'adding';
120+
/** @param {AddEmailFormState} emailListState */
121+
(emailListState) => {
122+
emailListState.type = 'adding';
123123
},
124124
);
125125

@@ -149,9 +149,9 @@ defineSmartComponent({
149149
} else {
150150
updateComponent(
151151
'addEmailFormState',
152-
/** @param {AddEmailFormState} state */
153-
(state) => {
154-
state.errors = {
152+
/** @param {AddEmailFormState} emailListState */
153+
(emailListState) => {
154+
emailListState.errors = {
155155
email: errorCode,
156156
};
157157
},
@@ -162,17 +162,17 @@ defineSmartComponent({
162162
.finally(() => {
163163
updateComponent(
164164
'addEmailFormState',
165-
/** @param {AddEmailFormState} state */
166-
(state) => {
167-
state.type = 'idle';
165+
/** @param {AddEmailFormState} emailListState */
166+
(emailListState) => {
167+
emailListState.type = 'idle';
168168
},
169169
);
170170
});
171171
});
172172

173173
onEvent('cc-email-list:delete', (address) => {
174174
updateSecondary(address, (secondaryAddressState) => {
175-
secondaryAddressState.state = 'deleting';
175+
secondaryAddressState.type = 'deleting';
176176
});
177177

178178
api
@@ -181,10 +181,12 @@ defineSmartComponent({
181181
notifySuccess(i18n('cc-email-list.secondary.action.delete.success', { address }));
182182

183183
updateComponent(
184-
'emails',
185-
/** @param {EmailListStateLoaded} emails */
186-
(emails) => {
187-
emails.value.secondaryAddresses = emails.value.secondaryAddresses.filter((a) => a.address !== address);
184+
'emailListState',
185+
/** @param {EmailListStateLoaded} emailListState */
186+
(emailListState) => {
187+
emailListState.emailList.secondaryAddresses = emailListState.emailList.secondaryAddresses.filter(
188+
(a) => a.address !== address,
189+
);
188190
},
189191
);
190192
})
@@ -194,34 +196,34 @@ defineSmartComponent({
194196
console.error(error);
195197
notifyError(i18n('cc-email-list.secondary.action.delete.error', { address }));
196198
updateSecondary(address, (secondaryAddressState) => {
197-
secondaryAddressState.state = 'idle';
199+
secondaryAddressState.type = 'idle';
198200
});
199201
},
200202
);
201203
});
202204

203205
onEvent('cc-email-list:mark-as-primary', (address) => {
204206
updateSecondary(address, (secondaryAddressState) => {
205-
secondaryAddressState.state = 'marking-as-primary';
207+
secondaryAddressState.type = 'marking-as-primary';
206208
});
207209

208210
api
209211
.markSecondaryEmailAddressAsPrimary(address)
210212
.then(() => {
211213
notifySuccess(i18n('cc-email-list.secondary.action.mark-as-primary.success', { address }));
212214

213-
if (component.emails.state === 'loaded') {
214-
const primaryAddress = component.emails.value.primaryAddress.address;
215+
if (component.emailListState.type === 'loaded') {
216+
const primaryAddress = component.emailListState.emailList.primaryAddress.address;
215217

216218
updateComponent(
217-
'emails',
218-
/** @param {EmailListStateLoaded} emails */
219-
(emails) => {
220-
emails.value.primaryAddress.address = address;
219+
'emailListState',
220+
/** @param {EmailListStateLoaded} emailListState */
221+
(emailListState) => {
222+
emailListState.emailList.primaryAddress.address = address;
221223
},
222224
);
223225
updateSecondary(address, (secondaryAddressState) => {
224-
secondaryAddressState.state = 'idle';
226+
secondaryAddressState.type = 'idle';
225227
secondaryAddressState.address = primaryAddress;
226228
});
227229
}
@@ -232,7 +234,7 @@ defineSmartComponent({
232234
console.error(error);
233235
notifyError(i18n('cc-email-list.secondary.action.mark-as-primary.error', { address }));
234236
updateSecondary(address, (secondaryAddressState) => {
235-
secondaryAddressState.state = 'idle';
237+
secondaryAddressState.type = 'idle';
236238
});
237239
},
238240
);

0 commit comments

Comments
 (0)