Skip to content

Commit a594505

Browse files
committed
PD-3859
1 parent d6037b8 commit a594505

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/app/core/record-affiliations/record-affiliations.service.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,21 +292,51 @@ export class RecordAffiliationService {
292292
)
293293
}
294294

295-
updateFeatured(putCode: string): Observable<any> {
295+
updateFeatured(
296+
affiliation: Affiliation,
297+
togglingOn: boolean
298+
): Observable<any> {
299+
const targetPutCode = togglingOn ? affiliation.putCode.value : ''
300+
const previousState = cloneDeep(this.lastEmittedValue)
301+
302+
// Optimistically update the shared affiliations observable so all consumers
303+
// see the featured change immediately.
304+
if (this.lastEmittedValue && this.$affiliations) {
305+
this.lastEmittedValue.forEach((uiGroup) => {
306+
uiGroup.affiliationGroup?.forEach((group) => {
307+
group.affiliations?.forEach((item) => {
308+
if (item.affiliationType?.value !== 'employment') {
309+
return
310+
}
311+
item.featured =
312+
togglingOn && item.putCode?.value === affiliation.putCode.value
313+
})
314+
})
315+
})
316+
this.$affiliations.next(this.lastEmittedValue)
317+
}
318+
296319
this._$loading.next(true)
297320
return this._http
298321
.put(
299322
runtimeEnvironment.API_WEB + 'affiliations/featuredAffiliation.json',
300323
{
301-
putCode: putCode,
324+
putCode: targetPutCode,
302325
},
303326
{
304327
headers: this.headers,
305328
}
306329
)
307330
.pipe(
308331
retry(3),
309-
catchError((error) => this._errorHandler.handleError(error)),
332+
catchError((error) => {
333+
// Revert optimistic update on failure
334+
if (previousState && this.$affiliations) {
335+
this.lastEmittedValue = previousState
336+
this.$affiliations.next(this.lastEmittedValue)
337+
}
338+
return this._errorHandler.handleError(error)
339+
}),
310340
tap(() => this.getAffiliations({ forceReload: true })),
311341
finalize(() => this._$loading.next(false))
312342
)

src/app/record/components/affiliation-stack/affiliation-stack.component.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,15 @@ export class AffiliationStackComponent implements OnInit, OnDestroy {
267267
}>()
268268

269269
toggleFeatured(affiliation: Affiliation) {
270-
const putCode = affiliation.featured ? '' : affiliation.putCode.value
271270
const wasFeatured = affiliation.featured
272-
this.loadingFeatured[affiliation.putCode.value] = true
271+
const togglingOn = !wasFeatured
273272
this._affiliationService
274-
.updateFeatured(putCode)
273+
.updateFeatured(affiliation, togglingOn)
275274
.pipe(
276275
finalize(() => {
277-
this.loadingFeatured[affiliation.putCode.value] = false
276+
this.affiliationStack.affiliations.forEach((item) => {
277+
this.loadingFeatured[item.putCode.value] = false
278+
})
278279
})
279280
)
280281
.subscribe({

0 commit comments

Comments
 (0)