Skip to content

Commit 0d1cf6e

Browse files
committed
feat: Enhance checklist item update with flash messaging and localization support
1 parent 27c4d13 commit 0d1cf6e

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

app/controllers/better_together/person_checklist_items_controller.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,29 @@ def create
3232
checklist_item: @checklist_item)
3333
pci.completed_at = params[:completed] ? Time.zone.now : nil
3434

35-
if pci.save
36-
Rails.logger.info("DBG PersonChecklistItemsController#create: saved pci id=#{pci.id} completed_at=#{pci.completed_at}")
37-
# If checklist completed, trigger a hook (implement as ActiveSupport::Notifications for now)
38-
notify_if_checklist_complete(person)
39-
render json: { id: pci.id, completed_at: pci.completed_at }, status: :ok
40-
else
41-
render json: { errors: pci.errors.full_messages }, status: :unprocessable_entity
35+
respond_to do |format|
36+
if pci.save
37+
Rails.logger.info("DBG PersonChecklistItemsController#create: saved pci id=#{pci.id} completed_at=#{pci.completed_at}")
38+
# If checklist completed, trigger a hook (implement as ActiveSupport::Notifications for now)
39+
notify_if_checklist_complete(person)
40+
format.json { render json: { id: pci.id, completed_at: pci.completed_at, flash: { type: 'notice', message: t('flash.checklist_item.updated') } }, status: :ok }
41+
format.html { redirect_back(fallback_location: BetterTogether.base_path_with_locale, notice: t('flash.checklist_item.updated')) }
42+
format.turbo_stream do
43+
flash.now[:notice] = t('flash.checklist_item.updated')
44+
render turbo_stream: turbo_stream.replace('flash_messages', partial: 'layouts/better_together/flash_messages', locals: { flash: })
45+
end
46+
else
47+
format.json { render json: { errors: pci.errors.full_messages, flash: { type: 'alert', message: t('flash.checklist_item.update_failed') } }, status: :unprocessable_entity }
48+
format.html { redirect_back(fallback_location: BetterTogether.base_path_with_locale, alert: t('flash.checklist_item.update_failed')) }
49+
format.turbo_stream do
50+
flash.now[:alert] = t('flash.checklist_item.update_failed')
51+
render turbo_stream: turbo_stream.replace('flash_messages', partial: 'layouts/better_together/flash_messages', locals: { flash: })
52+
end
53+
end
4254
end
4355
rescue StandardError => e
4456
Rails.logger.error("PersonChecklistItemsController#create unexpected error: #{e.class} - #{e.message}\n#{e.backtrace.join("\n")}")
45-
render json: { errors: [e.message] }, status: :internal_server_error
57+
render json: { errors: [e.message], flash: { type: 'alert', message: e.message } }, status: :internal_server_error
4658
end
4759

4860
private

app/javascript/controllers/better_together/person_checklist_item_controller.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ export default class extends Controller {
131131
try { errBody = await r.json() } catch (e) { /* ignore */ }
132132
lastError = { status: r.status, body: errBody }
133133

134+
// Show server-provided flash if present
135+
try {
136+
if (errBody && errBody.flash && window.BetterTogetherNotifications && typeof window.BetterTogetherNotifications.displayFlashMessage === 'function') {
137+
window.BetterTogetherNotifications.displayFlashMessage(errBody.flash.type || 'alert', errBody.flash.message || errBody.errors?.join(', ') || 'An error occurred')
138+
}
139+
} catch (e) { /* noop */ }
140+
134141
// Retry on server errors (5xx). For 4xx, break early.
135142
if (r.status >= 500) {
136143
const backoff = 200 * attempt
@@ -144,6 +151,12 @@ export default class extends Controller {
144151
const data = await r.json()
145152
// update UI with returned state
146153
this.updateUI(data)
154+
// Show server-provided flash if present
155+
try {
156+
if (data && data.flash && window.BetterTogetherNotifications && typeof window.BetterTogetherNotifications.displayFlashMessage === 'function') {
157+
window.BetterTogetherNotifications.displayFlashMessage(data.flash.type || 'notice', data.flash.message || '')
158+
}
159+
} catch (e) { /* noop */ }
147160
// Dispatch an event for checklist-level listeners with detail
148161
this.element.dispatchEvent(new CustomEvent('person-checklist-item:toggled', { bubbles: true, detail: { checklist_item_id: this.checklistItemIdValue, status: 'toggled', data } }))
149162
return
@@ -162,6 +175,13 @@ export default class extends Controller {
162175
this.containerTarget.classList.add('person-checklist-error')
163176
setTimeout(() => this.containerTarget.classList.remove('person-checklist-error'), 3000)
164177
}
178+
// Show a fallback flash message for persistent failures
179+
try {
180+
const msg = (typeof I18n !== 'undefined' && I18n && I18n.t) ? I18n.t('flash.checklist_item.update_failed') : 'Failed to update checklist item.'
181+
if (window.BetterTogetherNotifications && typeof window.BetterTogetherNotifications.displayFlashMessage === 'function') {
182+
window.BetterTogetherNotifications.displayFlashMessage('alert', msg)
183+
}
184+
} catch (e) { /* noop */ }
165185
}
166186

167187
getCSRFToken() {

config/locales/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,9 @@ en:
18011801
person_block:
18021802
blocked: Person was successfully blocked.
18031803
unblocked: Person was successfully unblocked.
1804+
checklist_item:
1805+
updated: Checklist item updated.
1806+
update_failed: Failed to update checklist item.
18041807
globals:
18051808
actions: Actions
18061809
add_block: Add block

config/locales/es.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,9 @@ es:
12381238
person_block:
12391239
cannot_block_manager: no puede ser un administrador de la plataforma
12401240
cannot_block_self: no puedes bloquearte a ti mismo
1241+
checklist_item:
1242+
updated: Elemento de la lista actualizado.
1243+
update_failed: Error al actualizar el elemento de la lista.
12411244
person_blocks:
12421245
index:
12431246
actions: Acciones

0 commit comments

Comments
 (0)