1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

(feat) allow to apply coupon in cart without login

This commit is contained in:
Du Peng 2022-09-07 17:52:23 +02:00
parent 6d2239bc15
commit 75b3295f65
3 changed files with 15 additions and 9 deletions

View File

@ -3,7 +3,7 @@
# API Controller for resources of type Coupon
# Coupons are used in payments
class API::CouponsController < API::ApiController
before_action :authenticate_user!
before_action :authenticate_user!, except: %i[validate]
before_action :set_coupon, only: %i[show update destroy]
# Number of notifications added to the page when the user clicks on 'load next notifications'
@ -31,18 +31,18 @@ class API::CouponsController < API::ApiController
if @coupon.nil?
render json: { status: 'rejected' }, status: :not_found
else
_user_id = if !current_user.admin?
current_user.id
else
_user_id = if current_user&.admin?
params[:user_id]
else
current_user&.id
end
amount = params[:amount].to_f * 100.0
status = @coupon.status(_user_id, amount)
if status != 'active'
render json: { status: status }, status: :unprocessable_entity
else
if status == 'active'
render :validate, status: :ok, location: @coupon
else
render json: { status: status }, status: :unprocessable_entity
end
end
end

View File

@ -194,7 +194,7 @@ const StoreCart: React.FC<StoreCartProps> = ({ onSuccess, onError, currentUser,
<p>[TODO: texte venant des paramètres de la boutique]</p>
</div>
{cart && !cartIsEmpty() && cart.user &&
{cart && !cartIsEmpty() &&
<div className='store-cart-coupon'>
<CouponInput user={cart.user as User} amount={cart.total} onChange={applyCoupon} />
</div>

View File

@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { FabInput } from '../base/fab-input';
import { FabAlert } from '../base/fab-alert';
@ -28,6 +28,12 @@ export const CouponInput: React.FC<CouponInputProps> = ({ user, amount, onChange
const [error, setError] = useState<boolean>(false);
const [coupon, setCoupon] = useState<Coupon>();
useEffect(() => {
if (user && coupon) {
handleChange(coupon.code);
}
}, [user]);
/**
* callback for validate the code
*/