Merge branch 'allow-entering-custom-amount-to-settle' into 'master'

Allow entering custom amount to settle

See merge request saburly/gangsta/roraccounting!18
This commit was merged in pull request #18.
This commit is contained in:
Bilal Catic
2020-10-06 06:43:05 +00:00
5 changed files with 65 additions and 13 deletions

View File

@@ -28,10 +28,11 @@ 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
json_response []
if homie.settle(amount)
cash
else
error_response :bad_request
end

View File

@@ -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

View File

@@ -5,13 +5,12 @@ 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';
const Cash = (props) => {
const [homiesCash, setHomiesCash] = useState([]);
const [, forceUpdate] = useReducer(x => x + 1, 0);
//const [importance, setImportance] = useState(10);
useEffect( () => {
@@ -26,18 +25,18 @@ const Cash = (props) => {
getCashForHomies();
}, []);
const settleFlowForHomie = async (id) => {
const settleFlowForHomie = async (id, 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' });
setHomiesCash(response.data);
}else{
errorToast();
}
} catch(e) {
errorToast();
}
forceUpdate();
}
const cashTableBody = homiesCash.map( (homieLine) => {
@@ -51,9 +50,11 @@ const Cash = (props) => {
</td>
<td className="cash-cell-left">
[
<YesNoModal
<InputModal
body={"Maan, y'a sure about this? Flow history goes bye bye!"}
yesAction={() => settleFlowForHomie(homieLine.homie.id)}
inputLabel={'Settle to'}
defaultInputValue={homieLine.amount}
confirmAction={(settleAmount) => settleFlowForHomie(homieLine.homie.id, settleAmount)}
triggerNode={<a href='#'>Settle</a>}
/>
]

View File

@@ -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(
<Modal
actions={[
<Row>
<Col s={3}>
<Button modal="confirm" node="button" waves="green" onClick={() => confirmAction(inputField.current.value)}>Do it</Button>
</Col>
<Col s={3}>
<Button modal="close" node="button" waves="red" onClick={stopAction}>Stop</Button>
</Col>
</Row>
]}
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}
>
<p><strong>{body}</strong></p>
<br/>
<div className="input-field col s6">
<input ref={inputField} defaultValue={defaultInputValue} id="input-num" type="number" className="validate" />
<label className="active" htmlFor="input-num">{inputLabel}</label>
</div>
</Modal>
)
}
export default InputModal;

View File

@@ -10,7 +10,7 @@ Rails.application.routes.draw do
get 'cash'
end
member do
delete 'settle'
post 'settle'
end
end
end