Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/controllers/announcements/blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def create

authorize block, policy_class: Announcement::BlockPolicy

block.save!
unless block.save
return render json: { errors: block.errors.map(&:full_message) }, status: :bad_request
end

render json: { id: block.id, html: block.rendered_html }
end
Expand Down
24 changes: 20 additions & 4 deletions app/javascript/controllers/tiptap_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ export default class extends Controller {

async donationGoal() {
const attrs = await this.createBlock('Announcement::Block::DonationGoal')
this.editor.chain().focus().addDonationGoal(attrs).run()

if (attrs !== null) {
this.editor.chain().focus().addDonationGoal(attrs).run()
}
}

async hcbCode() {
Expand All @@ -182,12 +185,17 @@ export default class extends Controller {
hcb_code: hcbCode,
})

this.editor.chain().focus().addHcbCode(attrs).run()
if (attrs !== null) {
this.editor.chain().focus().addHcbCode(attrs).run()
}
}

async donationSummary() {
const attrs = await this.createBlock('Announcement::Block::DonationSummary')
this.editor.chain().focus().addDonationSummary(attrs).run()

if (attrs !== null) {
this.editor.chain().focus().addDonationSummary(attrs).run()
}
}

async createBlock(type, parameters) {
Expand All @@ -204,6 +212,14 @@ export default class extends Controller {
},
}).then(r => r.json())

return res
if ('errors' in res) {
const message = `Could not insert block: ${res.errors.join(', ')}`

alert(message)

return null
} else {
return res
}
}
}
12 changes: 12 additions & 0 deletions app/models/announcement/block/hcb_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
class Announcement
class Block
class HcbCode < ::Announcement::Block
validate :hcb_code_in_event

def render_html(is_email: false)
hcb_code = ::HcbCode.find_by_hashid(parameters["hcb_code"])

Expand All @@ -34,6 +36,16 @@ def render_html(is_email: false)
Announcements::BlocksController.renderer.render partial: "announcements/blocks/hcb_code", locals: { hcb_code:, event: announcement.event, is_email:, block: self }
end

private

def hcb_code_in_event
hcb_code = ::HcbCode.find_by_hashid(parameters["hcb_code"])

if hcb_code.nil? || hcb_code.event != announcement.event
errors.add(:base, "Transaction can not be found.")
end
end

end

end
Expand Down