diff --git a/client/src/cash/Cash.js b/client/src/cash/Cash.js index c3848b0..91fa203 100644 --- a/client/src/cash/Cash.js +++ b/client/src/cash/Cash.js @@ -1,13 +1,10 @@ -import React, { useState, useEffect, useReducer } from 'react'; +import React, { useState, useEffect } from 'react'; import { Button, Table } from 'react-materialize'; import './Cash.css'; import axios from 'axios'; import { MAKE_MONEY_MOVE } from '../RouteNames'; import { withRouter } from 'react-router-dom'; import { formatMoney, formatTime } from '../common/formatting'; -import InputModal from "../common/InputModal"; -import { errorToast } from "../common/errorHelpers"; -import M from 'materialize-css'; const Cash = (props) => { const [homiesCash, setHomiesCash] = useState([]); @@ -25,20 +22,6 @@ const Cash = (props) => { getCashForHomies(); }, []); - const settleFlowForHomie = async (id, amountToSettle) => { - try { - const response = await axios.post(`/api/gangs/${gang.id}/homies/${id}/settle`, { amount: amountToSettle }); - if (response.status === 200 && response.data) { - M.toast({ html: 'Settled' }); - setHomiesCash(response.data); - }else{ - errorToast(); - } - } catch(e) { - errorToast(); - } - } - const cashTableBody = homiesCash.map( (homieLine) => { return ( @@ -52,15 +35,6 @@ const Cash = (props) => { { formatMoney(homieLine.amount, gang) } - [ - settleFlowForHomie(homieLine.homie.id, settleAmount)} - triggerNode={Settle} - /> - ] ); diff --git a/client/src/common/InputModal.js b/client/src/common/InputModal.js index 48df526..dae5eaf 100644 --- a/client/src/common/InputModal.js +++ b/client/src/common/InputModal.js @@ -40,7 +40,7 @@ const InputModal = (props) => {

{body}


- +
diff --git a/client/src/homies/CashFlow.js b/client/src/homies/CashFlow.js index c71a229..8c924a4 100644 --- a/client/src/homies/CashFlow.js +++ b/client/src/homies/CashFlow.js @@ -4,19 +4,28 @@ import axios from "axios"; import './Flow.css'; import { formatMoney, timestampToDate } from "../common/formatting"; import {errorToast} from "../common/errorHelpers"; +import InputModal from "../common/InputModal"; +import M from "materialize-css"; +import { Button } from 'react-materialize'; const CashFlow = (props) => { const { homie_id } = useParams(); const gang = props.gang; const [cashFlow, setCashFlow] = useState([]); + const [totalCashFlowAmount, setTotalCashFlowAmount] = useState(0); + + const updateData = (data) => { + setCashFlow(data); + setTotalCashFlowAmount(data.reduce((sum, record) => sum + parseInt(record.amount), 0)); + } useEffect( () => { (async () => { try { const response = await axios.get(`/api/money_moves?homie_id=${parseInt(homie_id)}`); if (response.status === 200 && response.data){ - setCashFlow(response.data); + updateData(response.data); } } catch (e) { errorToast(); @@ -45,7 +54,6 @@ const CashFlow = (props) => { const totalCount = cashFlow.length; const firstFlowRow = totalCount > 0 ? cashFlow[0] : undefined; const lastFlowRow = totalCount > 0 ? cashFlow[cashFlow.length - 1] : undefined; - const totalFlow = cashFlow.reduce((sum, record) => sum + parseInt(record.amount), 0); const fromDate = lastFlowRow ? timestampToDate(lastFlowRow['created_at']) : ''; const toDate = firstFlowRow ? timestampToDate(firstFlowRow['created_at']) : ''; @@ -58,19 +66,43 @@ const CashFlow = (props) => { {`${totalCount} Records`}{` • ${fromDate} - ${toDate}`}
- Total cash flow: {formatMoney(totalFlow, gang)} + Total cash flow: {formatMoney(totalCashFlowAmount, gang)}
) } + const settleFlowForHomie = async (id, amountToSettle) => { + try { + const response = await axios.post(`/api/gangs/${gang.id}/homies/${id}/settle`, { amount: amountToSettle }); + if (response.status === 200 && response.data) { + M.toast({ html: 'Settled' }); + updateData(response.data); + }else{ + errorToast(); + } + } catch(e) { + errorToast(); + } + } + return (
{ totalStats() } + +
+ + settleFlowForHomie(homie_id, settleAmount)} + triggerNode={} + />
); }