From 6a6f6505389f5d88c5de699085a6d2fa573fb735 Mon Sep 17 00:00:00 2001 From: Bilal Date: Mon, 5 Oct 2020 14:25:04 +0300 Subject: [PATCH 1/4] Allow entering custom settle value in input modal --- client/src/cash/Cash.js | 13 +++++---- client/src/common/InputModal.js | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 client/src/common/InputModal.js diff --git a/client/src/cash/Cash.js b/client/src/cash/Cash.js index 6f22627..76435b2 100644 --- a/client/src/cash/Cash.js +++ b/client/src/cash/Cash.js @@ -5,7 +5,7 @@ import axios from 'axios'; import { MAKE_MONEY_MOVE } from '../RouteNames'; import { withRouter } from 'react-router-dom'; import { formatMoney } from '../common/formatting'; -import YesNoModal from "../common/YesNoModal"; +import InputModal from "../common/InputModal"; import { errorToast } from "../common/errorHelpers"; import M from 'materialize-css'; @@ -26,9 +26,10 @@ const Cash = (props) => { getCashForHomies(); }, []); - const settleFlowForHomie = async (id) => { + const settleFlowForHomie = async (id, amountToSettle) => { + console.log('Amount to settle : ', amountToSettle); try { - const response = await axios.delete(`/api/homies/${id}/settle`); + const response = await axios.post(`/api/homies/${id}/settle`, { amount: amountToSettle }); if (response.status === 200 && response.data) { M.toast({ html: 'Settled' }); }else{ @@ -51,9 +52,11 @@ const Cash = (props) => { [ - settleFlowForHomie(homieLine.homie.id)} + inputLabel={'Settle to'} + defaultInputValue={homieLine.amount} + confirmAction={(settleAmount) => settleFlowForHomie(homieLine.homie.id, settleAmount)} triggerNode={Settle} /> ] diff --git a/client/src/common/InputModal.js b/client/src/common/InputModal.js new file mode 100644 index 0000000..48df526 --- /dev/null +++ b/client/src/common/InputModal.js @@ -0,0 +1,50 @@ +import React, { useRef } from 'react'; +import {Modal, Button, Row, Col} from 'react-materialize'; + +const InputModal = (props) => { + const { title, body, confirmAction, stopAction, triggerNode, defaultInputValue, inputLabel } = props; + const inputField = useRef(null); + + return( + + + + + + + + + ]} + bottomSheet + fixedFooter={false} + header={title} + id="Modal-1" + open={false} + options={{ + dismissible: true, + endingTop: '10%', + inDuration: 250, + onCloseEnd: null, + onCloseStart: null, + onOpenEnd: null, + onOpenStart: null, + opacity: 0.5, + outDuration: 250, + preventScrolling: true, + startingTop: '4%' + }} + trigger={triggerNode} + > +

{body}

+
+
+ + +
+
+ ) +} + +export default InputModal; \ No newline at end of file -- 2.47.3 From 04abd4be7859d6a5aae0143cb48abe6bb3f4d564 Mon Sep 17 00:00:00 2001 From: Bilal Date: Mon, 5 Oct 2020 14:25:42 +0300 Subject: [PATCH 2/4] accept and use custom settle value --- app/controllers/homies_controller.rb | 3 ++- app/models/homie.rb | 6 +++--- config/routes.rb | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/homies_controller.rb b/app/controllers/homies_controller.rb index 4bdd945..5b4387c 100644 --- a/app/controllers/homies_controller.rb +++ b/app/controllers/homies_controller.rb @@ -28,9 +28,10 @@ class HomiesController < ApplicationController def settle homie_id = params[:homie_id].to_i + amount = params[:amount].present? ? params[:amount].to_d : nil homie = Homie.find(homie_id) - if homie.settle + if homie.settle(amount) json_response [] else error_response :bad_request diff --git a/app/models/homie.rb b/app/models/homie.rb index 9d5d5f6..1c1efa4 100644 --- a/app/models/homie.rb +++ b/app/models/homie.rb @@ -12,8 +12,8 @@ class Homie < ApplicationRecord end end - def settle - total = MoneyMove.where(homie_id: id).all.sum(:amount) + def settle(amount) + total = amount.nil? ? MoneyMove.where(homie_id: id).all.sum(:amount) : amount all_money_moves = MoneyMove.where(homie_id: id).all transaction do @@ -22,7 +22,7 @@ class Homie < ApplicationRecord MoneyMove.create_settle_record(id, total) end true - rescue StandardError => e + rescue StandardError false end end diff --git a/config/routes.rb b/config/routes.rb index e3f2417..7094a56 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,7 +10,7 @@ Rails.application.routes.draw do get 'cash' end member do - delete 'settle' + post 'settle' end end end -- 2.47.3 From d13e656651f057e70f6880b62becaa57dff04af5 Mon Sep 17 00:00:00 2001 From: Bilal Date: Tue, 6 Oct 2020 09:40:33 +0300 Subject: [PATCH 3/4] refresh page after settling --- client/src/cash/Cash.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/client/src/cash/Cash.js b/client/src/cash/Cash.js index 76435b2..c3e2aac 100644 --- a/client/src/cash/Cash.js +++ b/client/src/cash/Cash.js @@ -11,7 +11,6 @@ import M from 'materialize-css'; const Cash = (props) => { const [homiesCash, setHomiesCash] = useState([]); - const [, forceUpdate] = useReducer(x => x + 1, 0); //const [importance, setImportance] = useState(10); useEffect( () => { @@ -27,18 +26,17 @@ const Cash = (props) => { }, []); const settleFlowForHomie = async (id, amountToSettle) => { - console.log('Amount to settle : ', amountToSettle); try { const response = await axios.post(`/api/homies/${id}/settle`, { amount: amountToSettle }); if (response.status === 200 && response.data) { M.toast({ html: 'Settled' }); + setHomiesCash(response.data); }else{ errorToast(); } } catch(e) { errorToast(); } - forceUpdate(); } const cashTableBody = homiesCash.map( (homieLine) => { -- 2.47.3 From a141d034754849c0ca319b95c8f82b48f208e41f Mon Sep 17 00:00:00 2001 From: Bilal Date: Tue, 6 Oct 2020 09:40:49 +0300 Subject: [PATCH 4/4] send homies data on settle --- app/controllers/homies_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/homies_controller.rb b/app/controllers/homies_controller.rb index 5b4387c..86ecc92 100644 --- a/app/controllers/homies_controller.rb +++ b/app/controllers/homies_controller.rb @@ -32,7 +32,7 @@ class HomiesController < ApplicationController homie = Homie.find(homie_id) if homie.settle(amount) - json_response [] + cash else error_response :bad_request end -- 2.47.3