Skip to content

Commit

Permalink
Reload orderscreen credit (#791)
Browse files Browse the repository at this point in the history
* Reload user credit when selecting user

* Fix linting errors
  • Loading branch information
wilco375 authored Oct 19, 2022
1 parent 587e913 commit b1612be
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/credit_mutations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
NewCreditMutationNotificationJob.perform_later(@mutation) if Rails.env.production? || Rails.env.staging?
format.html { redirect_to which_redirect?, flash: { success: 'Inleg of mutatie aangemaakt' } }
format.json do
render json: @mutation, include: { user: { methods: %i[credit avatar_thumb_or_default_url minor insufficient_credit can_order] } }
render json: @mutation, include: { user: { methods: User.orderscreen_json_includes } }
end

else
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def proper_json
end

def json_includes
{ user: { methods: %i[credit avatar_thumb_or_default_url minor insufficient_credit can_order] },
{ user: { methods: User.orderscreen_json_includes },
activity: { only: %i[id title] },
order_rows: { only: [:id, :product_count, { product: { only: %i[id name credit] } }] } }
end
Expand Down
12 changes: 8 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class UsersController < ApplicationController
class UsersController < ApplicationController # rubocop:disable Metrics/ClassLength
before_action :authenticate_user!

after_action :verify_authorized
Expand Down Expand Up @@ -27,10 +27,14 @@ def show
@user = User.includes(:credit_mutations, roles_users: :role).find(params[:id])
authorize @user

@user_json = @user.to_json(only: %i[id name deactivated])
@new_mutation = CreditMutation.new(user: @user)
if request.format.json?
render json: @user.as_json(methods: User.orderscreen_json_includes)
else
@user_json = @user.to_json(only: %i[id name deactivated])
@new_mutation = CreditMutation.new(user: @user)

@new_user = @user
@new_user = @user
end
end

def create
Expand Down
15 changes: 15 additions & 0 deletions app/javascript/packs/order_screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ document.addEventListener('turbolinks:load', () => {
this.orderRows = [];
}

if (user !== null) {
// Reload user to get latest credit balance
this.$http.get('/users/'+user.id).then((response) => {
const user = response.body;
this.$set(this.users, this.users.indexOf(user), user);

// Selected user might have changed in the meantime
if (this.selectedUser && this.selectedUser.id == user.id) {
this.selectedUser = user;
}
}, (response) => {
this.handleXHRError(response);
});
}

this.payWithCash = false;
this.payWithPin = false;
this.selectedUser = user;
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def self.calculate_spendings(from: '01-01-1970', to: Time.zone.now)
.group(:id).sum('product_count * price_per_product')
end

def self.orderscreen_json_includes
%i[credit avatar_thumb_or_default_url minor insufficient_credit can_order]
end

private

def no_deactivation_when_nonzero_credit
Expand Down

0 comments on commit b1612be

Please sign in to comment.