From c43cb0c5716a3671a682c87295f5a7dad61eb65d Mon Sep 17 00:00:00 2001 From: Keegan George Date: Tue, 21 Feb 2023 15:27:59 -0800 Subject: [PATCH] DEV: Apply optimistic update pattern to badge toggle (#20399) --- .../admin/addon/controllers/admin-badges/show.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/admin/addon/controllers/admin-badges/show.js b/app/assets/javascripts/admin/addon/controllers/admin-badges/show.js index a828bbe4d33..382cee16de8 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-badges/show.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-badges/show.js @@ -248,8 +248,13 @@ export default class AdminBadgesShowController extends Controller.extend( @action toggleBadge() { - this.model - .save({ enabled: !this.buffered.get("enabled") }) - .catch(popupAjaxError); + const originalState = this.buffered.get("enabled"); + const newState = !this.buffered.get("enabled"); + + this.buffered.set("enabled", newState); + this.model.save({ enabled: newState }).catch((error) => { + this.buffered.set("enabled", originalState); + return popupAjaxError(error); + }); } }