generated from victorbalssa/expo-ticket-app
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8244cc5
commit c38224e
Showing
6 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import moment from 'moment/moment'; | ||
import React, { useMemo } from 'react'; | ||
import translate from '../../../i18n/locale'; | ||
import { useThemeColors } from '../../../lib/common'; | ||
import { BillType } from '../../../models/bills'; | ||
import { ASkeleton, AStack, AText } from '../ALibrary'; | ||
|
||
type Props = { | ||
bill: BillType; | ||
loading: boolean; | ||
lastItem: boolean; | ||
} | ||
|
||
export default function BillListItem({ bill, loading, lastItem }: Props) { | ||
const { colors } = useThemeColors(); | ||
const paidDate = useMemo(() => bill.attributes.paidDates[0]?.date || null, [bill]); | ||
|
||
const statusColor = useMemo(() => { | ||
// The bill is paid | ||
if (paidDate !== null) { | ||
return colors.green; | ||
} | ||
|
||
// The bill is not expected in the current period | ||
if (bill.attributes.nextExpectedMatch === null) { | ||
return colors.brandStyle4; | ||
} | ||
|
||
// The bill should be paid by now | ||
if (moment(bill.attributes.nextExpectedMatch).diff(moment(), 'days') < 0) { | ||
return colors.brandWarning; | ||
} | ||
|
||
// The expected date is in the future | ||
return undefined; | ||
}, [paidDate]); | ||
|
||
const billContent = useMemo(() => { | ||
if (paidDate !== null) { | ||
return translate('bills_paid'); | ||
} | ||
|
||
if (bill.attributes.nextExpectedMatch === null) { | ||
return translate('bills_not_expected'); | ||
} | ||
|
||
return moment(bill.attributes.nextExpectedMatch).format('ll'); | ||
}, [bill, paidDate]); | ||
|
||
return ( | ||
<AStack | ||
key={bill.id} | ||
row | ||
mx={15} | ||
style={{ | ||
height: 45, | ||
borderColor: colors.listBorderColor, | ||
borderBottomWidth: lastItem ? 0 : 0.5, | ||
}} | ||
justifyContent="space-between" | ||
> | ||
<AText | ||
fontSize={14} | ||
maxWidth="60%" | ||
numberOfLines={1} | ||
> | ||
{bill.attributes.name} | ||
</AText> | ||
|
||
<ASkeleton loading={loading}> | ||
<AText | ||
fontSize={14} | ||
maxWidth={100} | ||
numberOfLines={1} | ||
color={statusColor} | ||
> | ||
{billContent} | ||
</AText> | ||
</ASkeleton> | ||
</AStack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { createModel } from '@rematch/core'; | ||
import moment from 'moment/moment'; | ||
import { RootModel } from './index'; | ||
|
||
export type BillType = { | ||
id: string, | ||
type: string, | ||
attributes: { | ||
name: string, | ||
active: boolean; | ||
amountMin: string, | ||
amountMax: string, | ||
currencyId: string, | ||
currencyCode: string, | ||
currencySymbol: string, | ||
nextExpectedMatch: string | null, | ||
payDates: string[], | ||
paidDates: { | ||
transactionGroupId: string, | ||
transactionJournalId: string, | ||
date: string, | ||
}[], | ||
}, | ||
} | ||
|
||
type BillsStateType = { | ||
bills: BillType[], | ||
} | ||
|
||
const INITIAL_STATE = { | ||
bills: [], | ||
} as BillsStateType; | ||
|
||
export default createModel<RootModel>()({ | ||
|
||
state: INITIAL_STATE, | ||
|
||
reducers: { | ||
setBills(state, payload): BillsStateType { | ||
const { | ||
bills = state.bills, | ||
} = payload; | ||
|
||
return { | ||
...state, | ||
bills, | ||
}; | ||
}, | ||
|
||
resetState() { | ||
return INITIAL_STATE; | ||
}, | ||
}, | ||
|
||
effects: (dispatch) => ({ | ||
/** | ||
* Get bills | ||
* | ||
* @returns {Promise} | ||
*/ | ||
async getBills(_: void, rootState): Promise<void> { | ||
const { | ||
firefly: { | ||
rangeDetails: { | ||
start, | ||
end, | ||
}, | ||
}, | ||
} = rootState; | ||
|
||
const { data: bills } = await dispatch.configuration.apiFetch({ url: `/api/v1/bills?start=${start}&end=${end}` }) as { data: BillType[] }; | ||
|
||
const sortedBills = [...bills] | ||
// Order by next expected date | ||
.sort((a, b) => moment(a.attributes.nextExpectedMatch).diff(moment(b.attributes.nextExpectedMatch))) | ||
// Make sure all paid bills are at the end of the list | ||
.sort((a, b) => a.attributes.paidDates.length - b.attributes.paidDates.length); | ||
|
||
dispatch.bills.setBills({ bills: sortedBills }); | ||
}, | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters