2020-04-12 18:27:01 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-05-15 19:48:00 +02:00
|
|
|
import { toast } from 'react-toastify';
|
2020-05-19 07:50:16 +02:00
|
|
|
import { useAccountState } from 'src/context/AccountContext';
|
|
|
|
import { InvoiceCard } from 'src/views/transactions/InvoiceCard';
|
|
|
|
import {
|
|
|
|
useGetResumeQuery,
|
|
|
|
GetResumeQuery,
|
|
|
|
} from 'src/graphql/queries/__generated__/getResume.generated';
|
2020-05-26 07:03:33 +02:00
|
|
|
import { GridWrapper } from 'src/components/gridWrapper/GridWrapper';
|
|
|
|
import { withApollo } from 'config/client';
|
2020-04-12 18:27:01 +02:00
|
|
|
import {
|
|
|
|
Card,
|
|
|
|
CardWithTitle,
|
|
|
|
SubTitle,
|
|
|
|
} from '../src/components/generic/Styled';
|
|
|
|
import { getErrorContent } from '../src/utils/error';
|
|
|
|
import { PaymentsCard } from '../src/views/transactions/PaymentsCards';
|
|
|
|
import { LoadingCard } from '../src/components/loading/LoadingCard';
|
|
|
|
import { ColorButton } from '../src/components/buttons/colorButton/ColorButton';
|
|
|
|
import { FlowBox } from '../src/views/home/reports/flow';
|
|
|
|
|
|
|
|
const TransactionsView = () => {
|
|
|
|
const [indexOpen, setIndexOpen] = useState(0);
|
|
|
|
const [token, setToken] = useState('');
|
|
|
|
const [fetching, setFetching] = useState(false);
|
|
|
|
|
2020-05-19 07:50:16 +02:00
|
|
|
const { auth } = useAccountState();
|
2020-04-12 18:27:01 +02:00
|
|
|
|
2020-04-16 22:52:41 +02:00
|
|
|
const { loading, data, fetchMore } = useGetResumeQuery({
|
2020-04-17 20:11:37 +02:00
|
|
|
skip: !auth,
|
2020-04-12 18:27:01 +02:00
|
|
|
variables: { auth, token: '' },
|
|
|
|
onError: error => toast.error(getErrorContent(error)),
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!loading && data && data.getResume && data.getResume.token) {
|
|
|
|
setToken(data.getResume.token);
|
|
|
|
}
|
|
|
|
}, [data, loading]);
|
|
|
|
|
|
|
|
if (loading || !data || !data.getResume) {
|
|
|
|
return <LoadingCard title={'Transactions'} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
const resumeList = JSON.parse(data.getResume.resume);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<FlowBox />
|
|
|
|
<CardWithTitle>
|
|
|
|
<SubTitle>Transactions</SubTitle>
|
2020-05-23 18:45:28 +02:00
|
|
|
<Card bottom={'8px'} mobileCardPadding={'0'} mobileNoBackground={true}>
|
2020-04-12 18:27:01 +02:00
|
|
|
{resumeList.map((entry: any, index: number) => {
|
|
|
|
if (entry.type === 'invoice') {
|
|
|
|
return (
|
|
|
|
<InvoiceCard
|
|
|
|
invoice={entry}
|
|
|
|
key={index}
|
|
|
|
index={index + 1}
|
|
|
|
setIndexOpen={setIndexOpen}
|
|
|
|
indexOpen={indexOpen}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<PaymentsCard
|
|
|
|
payment={entry}
|
|
|
|
key={index}
|
|
|
|
index={index + 1}
|
|
|
|
setIndexOpen={setIndexOpen}
|
|
|
|
indexOpen={indexOpen}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
<ColorButton
|
|
|
|
fullWidth={true}
|
|
|
|
withMargin={'16px 0 0'}
|
|
|
|
loading={fetching}
|
|
|
|
disabled={fetching}
|
|
|
|
onClick={() => {
|
|
|
|
setFetching(true);
|
|
|
|
fetchMore({
|
|
|
|
variables: { auth, token },
|
2020-04-17 15:36:35 +02:00
|
|
|
updateQuery: (
|
|
|
|
prev,
|
|
|
|
{
|
|
|
|
fetchMoreResult: result,
|
|
|
|
}: { fetchMoreResult: GetResumeQuery }
|
|
|
|
) => {
|
2020-04-12 18:27:01 +02:00
|
|
|
if (!result) return prev;
|
|
|
|
const newToken = result.getResume.token || '';
|
|
|
|
const prevEntries = JSON.parse(prev.getResume.resume);
|
|
|
|
const newEntries = JSON.parse(result.getResume.resume);
|
|
|
|
|
|
|
|
const allTransactions = newToken
|
|
|
|
? [...prevEntries, ...newEntries]
|
|
|
|
: prevEntries;
|
|
|
|
|
|
|
|
setFetching(false);
|
|
|
|
return {
|
|
|
|
getResume: {
|
|
|
|
token: newToken,
|
|
|
|
resume: JSON.stringify(allTransactions),
|
|
|
|
__typename: 'getResumeType',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Show More
|
|
|
|
</ColorButton>
|
|
|
|
</Card>
|
|
|
|
</CardWithTitle>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-05-26 07:03:33 +02:00
|
|
|
const Wrapped = () => (
|
|
|
|
<GridWrapper>
|
|
|
|
<TransactionsView />
|
|
|
|
</GridWrapper>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default withApollo(Wrapped);
|