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:
@@ -28,10 +28,11 @@ class HomiesController < ApplicationController
|
|||||||
|
|
||||||
def settle
|
def settle
|
||||||
homie_id = params[:homie_id].to_i
|
homie_id = params[:homie_id].to_i
|
||||||
|
amount = params[:amount].present? ? params[:amount].to_d : nil
|
||||||
homie = Homie.find(homie_id)
|
homie = Homie.find(homie_id)
|
||||||
|
|
||||||
if homie.settle
|
if homie.settle(amount)
|
||||||
json_response []
|
cash
|
||||||
else
|
else
|
||||||
error_response :bad_request
|
error_response :bad_request
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ class Homie < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def settle
|
def settle(amount)
|
||||||
total = MoneyMove.where(homie_id: id).all.sum(:amount)
|
total = amount.nil? ? MoneyMove.where(homie_id: id).all.sum(:amount) : amount
|
||||||
all_money_moves = MoneyMove.where(homie_id: id).all
|
all_money_moves = MoneyMove.where(homie_id: id).all
|
||||||
|
|
||||||
transaction do
|
transaction do
|
||||||
@@ -22,7 +22,7 @@ class Homie < ApplicationRecord
|
|||||||
MoneyMove.create_settle_record(id, total)
|
MoneyMove.create_settle_record(id, total)
|
||||||
end
|
end
|
||||||
true
|
true
|
||||||
rescue StandardError => e
|
rescue StandardError
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,13 +5,12 @@ import axios from 'axios';
|
|||||||
import { MAKE_MONEY_MOVE } from '../RouteNames';
|
import { MAKE_MONEY_MOVE } from '../RouteNames';
|
||||||
import { withRouter } from 'react-router-dom';
|
import { withRouter } from 'react-router-dom';
|
||||||
import { formatMoney } from '../common/formatting';
|
import { formatMoney } from '../common/formatting';
|
||||||
import YesNoModal from "../common/YesNoModal";
|
import InputModal from "../common/InputModal";
|
||||||
import { errorToast } from "../common/errorHelpers";
|
import { errorToast } from "../common/errorHelpers";
|
||||||
import M from 'materialize-css';
|
import M from 'materialize-css';
|
||||||
|
|
||||||
const Cash = (props) => {
|
const Cash = (props) => {
|
||||||
const [homiesCash, setHomiesCash] = useState([]);
|
const [homiesCash, setHomiesCash] = useState([]);
|
||||||
const [, forceUpdate] = useReducer(x => x + 1, 0);
|
|
||||||
//const [importance, setImportance] = useState(10);
|
//const [importance, setImportance] = useState(10);
|
||||||
|
|
||||||
useEffect( () => {
|
useEffect( () => {
|
||||||
@@ -26,18 +25,18 @@ const Cash = (props) => {
|
|||||||
getCashForHomies();
|
getCashForHomies();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const settleFlowForHomie = async (id) => {
|
const settleFlowForHomie = async (id, amountToSettle) => {
|
||||||
try {
|
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) {
|
if (response.status === 200 && response.data) {
|
||||||
M.toast({ html: 'Settled' });
|
M.toast({ html: 'Settled' });
|
||||||
|
setHomiesCash(response.data);
|
||||||
}else{
|
}else{
|
||||||
errorToast();
|
errorToast();
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
errorToast();
|
errorToast();
|
||||||
}
|
}
|
||||||
forceUpdate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const cashTableBody = homiesCash.map( (homieLine) => {
|
const cashTableBody = homiesCash.map( (homieLine) => {
|
||||||
@@ -51,9 +50,11 @@ const Cash = (props) => {
|
|||||||
</td>
|
</td>
|
||||||
<td className="cash-cell-left">
|
<td className="cash-cell-left">
|
||||||
[
|
[
|
||||||
<YesNoModal
|
<InputModal
|
||||||
body={"Maan, y'a sure about this? Flow history goes bye bye!"}
|
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>}
|
triggerNode={<a href='#'>Settle</a>}
|
||||||
/>
|
/>
|
||||||
]
|
]
|
||||||
|
|||||||
50
client/src/common/InputModal.js
Normal file
50
client/src/common/InputModal.js
Normal 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;
|
||||||
@@ -10,7 +10,7 @@ Rails.application.routes.draw do
|
|||||||
get 'cash'
|
get 'cash'
|
||||||
end
|
end
|
||||||
member do
|
member do
|
||||||
delete 'settle'
|
post 'settle'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user