From f050d010fdd270be7832856da2aec64fc0d33045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 15 Mar 2023 15:24:00 +0700 Subject: [PATCH] Refactor admin donation pages, fix errors Not sending the right response codes for Turbo to handle. --- app/assets/stylesheets/components/forms.css | 4 ++++ app/controllers/admin/donations_controller.rb | 4 ++-- app/models/donation.rb | 2 ++ app/views/admin/donations/_form.html.erb | 21 ++++++++++++------- app/views/admin/donations/edit.html.erb | 4 ---- app/views/admin/donations/new.html.erb | 4 ---- app/views/admin/donations/show.html.erb | 4 ++-- 7 files changed, 23 insertions(+), 20 deletions(-) diff --git a/app/assets/stylesheets/components/forms.css b/app/assets/stylesheets/components/forms.css index 5603ba4..633c293 100644 --- a/app/assets/stylesheets/components/forms.css +++ b/app/assets/stylesheets/components/forms.css @@ -10,6 +10,10 @@ @apply inline-block; } + .field_with_errors input { + @apply w-full bg-red-100; + } + .error-msg { @apply text-red-700; } diff --git a/app/controllers/admin/donations_controller.rb b/app/controllers/admin/donations_controller.rb index c29fb80..0a90184 100644 --- a/app/controllers/admin/donations_controller.rb +++ b/app/controllers/admin/donations_controller.rb @@ -41,7 +41,7 @@ class Admin::DonationsController < Admin::BaseController end format.json { render :show, status: :created, location: @donation } else - format.html { render :new } + format.html { render :new, status: :unprocessable_entity } format.json { render json: @donation.errors, status: :unprocessable_entity } end end @@ -59,7 +59,7 @@ class Admin::DonationsController < Admin::BaseController end format.json { render :show, status: :ok, location: @donation } else - format.html { render :edit } + format.html { render :edit, status: :unprocessable_entity } format.json { render json: @donation.errors, status: :unprocessable_entity } end end diff --git a/app/models/donation.rb b/app/models/donation.rb index 30f13a0..1a374dd 100644 --- a/app/models/donation.rb +++ b/app/models/donation.rb @@ -3,7 +3,9 @@ class Donation < ApplicationRecord belongs_to :user # Validations + validates_presence_of :user validates_presence_of :amount_sats + validates_presence_of :paid_at # Hooks # TODO before_create :store_fiat_value diff --git a/app/views/admin/donations/_form.html.erb b/app/views/admin/donations/_form.html.erb index 0522cb0..b04562a 100644 --- a/app/views/admin/donations/_form.html.erb +++ b/app/views/admin/donations/_form.html.erb @@ -1,16 +1,16 @@ <%= form_with(url: url, model: donation, local: true) do |form| %> <% if donation.errors.any? %> -
+

<%= pluralize(donation.errors.count, "error") %> prohibited this donation from being saved:

-
    +
      <% donation.errors.full_messages.each do |message| %>
    • <%= message %>
    • <% end %>
    -
+ <% end %> -
+
<%= form.label :user_id %> <%= form.collection_select :user_id, User.where(ou: "kosmos.org").order(:cn), :id, :cn, {} %> @@ -28,9 +28,14 @@ <%= form.label :paid_at %> <%= form.text_field :paid_at %> -
+ -

- <%= form.submit class: 'btn-md btn-blue' %> -

+
+

+ <%= link_to 'Cancel', + @donation.id.present? ? admin_donation_path(@donation) : admin_donations_path, + class: 'btn-md btn-gray' %> + <%= form.submit class: 'ml-2 btn-md btn-blue' %> +

+
<% end %> diff --git a/app/views/admin/donations/edit.html.erb b/app/views/admin/donations/edit.html.erb index 27340f1..3152cc1 100644 --- a/app/views/admin/donations/edit.html.erb +++ b/app/views/admin/donations/edit.html.erb @@ -2,8 +2,4 @@ <%= render MainSimpleComponent.new do %> <%= render 'form', donation: @donation, url: admin_donation_path(@donation) %> - -

- <%= link_to 'Cancel', admin_donation_path(@donation), class: 'btn-sm btn-gray' %> -

<% end %> diff --git a/app/views/admin/donations/new.html.erb b/app/views/admin/donations/new.html.erb index 176b659..49d4671 100644 --- a/app/views/admin/donations/new.html.erb +++ b/app/views/admin/donations/new.html.erb @@ -2,8 +2,4 @@ <%= render MainSimpleComponent.new do %> <%= render 'form', donation: @donation, url: admin_donations_path %> - -

- <%= link_to 'Back', admin_donations_path, class: 'ks-text-link' %> -

<% end %> diff --git a/app/views/admin/donations/show.html.erb b/app/views/admin/donations/show.html.erb index 1526789..bec899d 100644 --- a/app/views/admin/donations/show.html.erb +++ b/app/views/admin/donations/show.html.erb @@ -33,9 +33,9 @@
-

- <%= link_to 'Edit', edit_admin_donation_path(@donation), class: 'btn-md btn-blue mr-1' %> +

<%= link_to 'Back', admin_donations_path, class: 'btn-md btn-gray' %> + <%= link_to 'Edit', edit_admin_donation_path(@donation), class: 'ml-2 btn-md btn-blue mr-1' %>

<% end %>