Skip to content

Commit

Permalink
Merge pull request #346 from setlife-network/sr/issue-345/currency-ra…
Browse files Browse the repository at this point in the history
…te-clients

Fixed reate currency
  • Loading branch information
otech47 committed Mar 5, 2021
2 parents a8b38ae + 17b4b99 commit 4b9e06c
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 52 deletions.
2 changes: 2 additions & 0 deletions src/components/AllocationAddForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ const AllocationAddForm = (props) => {
<RateProratedMonthlyForm
clientCurrency={clientCurrency}
currentRate={mostRecentAllocation ? mostRecentAllocation.rate : null}
selectedPayment={selectedPayment}
setNewAllocationRate={setNewAllocationRate}
setCurrency={setRateCurrency}
startDate={moment(startDate)}
Expand All @@ -492,6 +493,7 @@ const AllocationAddForm = (props) => {
<RateMaxBudgetForm
clientCurrency={clientCurrency}
currentRate={mostRecentAllocation ? mostRecentAllocation.rate : null}
selectedPayment={selectedPayment}
setCurrency={setRateCurrency}
setNewAllocationRate={setNewAllocationRate}
startDate={moment(startDate)}
Expand Down
52 changes: 33 additions & 19 deletions src/components/AllocationOverview.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const AllocationOverview = (props) => {
error: errorAllocation,
loading: loadingAllocation
} = useQuery(GET_ALLOCATION_INFO, {
fetchPolicy: 'cache-and-network',
variables: {
id: allocationInfo.id
}
Expand Down Expand Up @@ -131,6 +132,11 @@ const AllocationOverview = (props) => {
}
}, [contributorAllocation])

const handleClose = () => {
setContributorAllocation(null)
onClose()
}

const handleDeleteAllocation = async () => {
const paymentDeleted = await deleteAllocation()
onClose()
Expand Down Expand Up @@ -172,30 +178,36 @@ const AllocationOverview = (props) => {
selectedRate.id = newRate.data.createRate.id
}
//update allocation with that rate id
const updatedAllocation = await updateAllocation({
variables: {
id: allocation.id,
amount: Number(rate.total_amount),
start_date: moment(startDate).format('YYYY-MM-DD'),
end_date: moment(endDate).format('YYYY-MM-DD'),
date_paid: null,
rate_id: Number(selectedRate.id),
payment_id: payment.id
try {
const updatedAllocation = await updateAllocation({
variables: {
id: allocation.id,
amount: Number(rate.total_amount),
start_date: moment(startDate).format('YYYY-MM-DD'),
end_date: moment(endDate).format('YYYY-MM-DD'),
date_paid: null,
rate_id: Number(selectedRate.id),
payment_id: payment ? payment.id : null
}
})
if (loadingUpdatedAllocation) return ''
else if (updatedAllocation.errors) {
throw updatedAllocation.errors
} else {
onClose()
}
})
if (loadingUpdatedAllocation) return ''
else if (updatedAllocation.errors) {
console.log('Error updating the allocation');
} else {
} catch (error) {
console.log(`error ${error}`);
onClose()
}

}

if (loadingAllocation || loadingClientPayments) return <LoadingProgress/>
if (errorAllocation || errorClientPayments) return `Error`

const { getAllocationById: allocation } = dataAllocation
const payments = [{ id: null, amount: null, date_paid: null }]
const payments = [null]
if (clientPayments) {
payments.unshift(...clientPayments.payments)
}
Expand All @@ -204,11 +216,12 @@ const AllocationOverview = (props) => {
setContributorAllocation(allocation)
}

console.log('contributorRates');
console.log(contributorRates);

return (
<Dialog className='AllocationOverview' onClose={onClose} open={open}>
<Dialog
className='AllocationOverview'
onClose={() => handleClose()}
open={open}
>
<DialogTitle>
{`Allocation Detail`}
</DialogTitle>
Expand All @@ -227,6 +240,7 @@ const AllocationOverview = (props) => {
currency={allocation.project.client.currency}
endDate={updatedAllocationEndDate}
rate={allocation.rate}
selectedPayment={updatedAllocationPayment}
setEndDate={setUpdatedAllocationEndDate}
setNewAllocationRate={setUpdatedAllocationRate}
setSelectedCurrency={setSelectedCurrency}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ContributorTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ const ContributorTile = (props) => {
{selectedAllocation &&
<AllocationOverview
allocationInfo={selectedAllocation}
onClose={() => handleAllocationOverview(false)}
onClose={() => handleAllocationOverview({ value: false })}
open={openAllocationOverview}
/>
}
Expand Down
48 changes: 25 additions & 23 deletions src/components/EditAllocationInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,24 @@ const EditAllocationInfo = (props) => {

const [openPayments, setOpenPayments] = useState(false)

useEffect(() => {
if (!selectedPayment) {
setSelectedPayment(payments[findIndex(payments, ['amount', allocation.payment])])
}
})

const handleClickPayments = () => {
setOpenPayments(!openPayments)
}
const onClickPayment = (payment) => {
setSelectedPayment(payment)
if (payment) {
setSelectedPayment(payment)
} else {
setSelectedPayment(null)
}
setOpenPayments(false)
}

const paymentAmount = (
selectedPayment
? selectedPayment.amount
? formatAmount({
amount: selectedPayment.amount / 100,
currencyInformation: currencyInformation
})
: 'Proposed'
? formatAmount({
amount: selectedPayment.amount / 100,
currencyInformation: currencyInformation
})
: 'Proposed'
)
const datePaid = (
Expand All @@ -72,29 +68,35 @@ const EditAllocationInfo = (props) => {

const listPayments = (payments) => {
const paymentsList = differenceWith(payments, [selectedPayment], isEqual)
return paymentsList.map(p => {
const paymentAmount = formatAmount({
amount: p.amount / 100,
currencyInformation: currencyInformation
})
return paymentsList.map(payment => {
const paymentAmount = (
payment
? (formatAmount({
amount: payment.amount / 100,
currencyInformation: currencyInformation
}))
: null
)
return (
<List component='div' disablePadding>
<ListItem button onClick={() => onClickPayment(p)}>
<ListItem button onClick={() => onClickPayment(payment)}>
<Grid container>
<Grid item xs={6}>
<ListItemText
primary={`${
p.amount
payment
? `${paymentAmount}`
: 'Propose'
}`}
/>
</Grid>
<Grid item xs={3} align='center'>
<Typography variant='caption' color='secondary'>
{`${p.date_paid
? moment(p.date_paid, 'x').format('MM/DD/YYYY')
: ''
{`${payment
? payment.date_paid
? moment(payment.date_paid, 'x').format('MM/DD/YYYY')
: 'Not paid yet'
: 'Proposed'
}`}
</Typography>
</Grid>
Expand Down
6 changes: 6 additions & 0 deletions src/components/EditAllocationRate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const EditAllocationRate = (props) => {
currency,
endDate,
rate,
selectedPayment,
setEndDate,
setNewAllocationRate,
setSelectedCurrency,
Expand All @@ -37,6 +38,9 @@ const EditAllocationRate = (props) => {

const [selectedRateType, setSelectedRateType] = useState(rate.type)

// console.log('selectedPayment');
// console.log(selectedPayment);

const getRangedTimeEntries = (dates) => {
const [start, end] = dates
setStartDate(start)
Expand Down Expand Up @@ -101,6 +105,7 @@ const EditAllocationRate = (props) => {
rateCurrency={rate.currency}
clientCurrency={currency}
currentRate={rate}
selectedPayment={selectedPayment}
setNewAllocationRate={setNewAllocationRate}
setCurrency={setSelectedCurrency}
startDate={moment(startDate)}
Expand All @@ -111,6 +116,7 @@ const EditAllocationRate = (props) => {
currentTotal={allocation.amount}
clientCurrency={currency}
currentRate={rate}
selectedPayment={selectedPayment}
setCurrency={setSelectedCurrency}
setNewAllocationRate={setNewAllocationRate}
startDate={moment(startDate)}
Expand Down
11 changes: 10 additions & 1 deletion src/components/RateMaxBudgetForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const RateMaxBudgetForm = (props) => {
currentTotal,
createRate,
endDate,
selectedPayment,
setCurrency,
setNewAllocationRate,
startDate
Expand All @@ -38,12 +39,19 @@ const RateMaxBudgetForm = (props) => {
setCurrency(rateCurrency)
}
}, [rateCurrency])

useEffect(() => {
setTotalWeeks(endDate.diff(startDate, 'days') / 7)
setCurrentRateInput(currentRate ? currentRate.hourly_rate : 0)
setRateCurrency(currentRate ? currentRate.currency : clientCurrency)
}, [currentRate])

useEffect(() => {
if (selectedPayment) {
setRateCurrency(clientCurrency)
}
}, [selectedPayment])

useEffect(() => {
setTotalHours(totalAmount && currentRateInput ? ((totalAmount / 100) / currentRateInput).toFixed(2) : 0)
setNewAllocationRate({
Expand Down Expand Up @@ -86,7 +94,8 @@ const RateMaxBudgetForm = (props) => {
name='Currency'
fullWidth
onChange={(event) => setRateCurrency(event.target.value)}
value={rateCurrency}
value={selectedPayment ? clientCurrency : rateCurrency}
disabled={selectedPayment}
>
{renderCurrencies(CURRENCIES)}
</Select>
Expand Down
24 changes: 16 additions & 8 deletions src/components/RateProratedMonthlyForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const RateProratedMonthlyForm = (props) => {
clientCurrency,
currentRate,
endDate,
selectedPayment,
setNewAllocationRate,
setCurrency,
startDate
Expand All @@ -33,20 +34,26 @@ const RateProratedMonthlyForm = (props) => {
const [totalWeeks, setTotalWeeks] = useState(null)
const [totalHours, setTotalHours] = useState(0)

useEffect(() => {
if (!rateCurrency) {
setRateCurrency(clientCurrency)
}
setCurrency(rateCurrency)
}, [rateCurrency])

useEffect(() => {
setTotalWeeks(endDate.diff(startDate, 'days') / 7)
setCurrentRateInput(currentRate ? currentRate.hourly_rate : 0)
setMonthlyhoursInput(currentRate ? currentRate.total_expected_hours : 160)
setRateCurrency(currentRate ? currentRate.currency : clientCurrency)
}, [currentRate])

useEffect(() => {
if (selectedPayment) {
setRateCurrency(clientCurrency)
}
}, [selectedPayment])

useEffect(() => {
if (!rateCurrency) {
setRateCurrency(clientCurrency)
}
setCurrency(rateCurrency)
}, [rateCurrency])

useEffect(() => {
setTotalAmount(currentRateInput * monthlyHoursInput)
}, [monthlyHoursInput, currentRateInput])
Expand Down Expand Up @@ -97,7 +104,8 @@ const RateProratedMonthlyForm = (props) => {
name='Currency'
fullWidth
onChange={(event) => setRateCurrency(event.target.value)}
value={rateCurrency}
value={selectedPayment ? clientCurrency : rateCurrency}
disabled={selectedPayment}
>
{renderCurrencies(CURRENCIES)}
</Select>
Expand Down

0 comments on commit 4b9e06c

Please sign in to comment.