import React, { useState, useEffect } from 'react';
import { useLocation, useHistory } from 'react-router-dom';
import Platform from 'loot-core/src/client/platform';
import q, { runQuery } from 'loot-core/src/client/query-helpers';
import { send } from 'loot-core/src/platform/client/fetch';
import { getRecurringDescription } from 'loot-core/src/shared/schedules';
import {
View,
Stack,
Button,
ButtonWithLoading,
P
} from 'loot-design/src/components/common';
import {
Table,
TableHeader,
Row,
Field,
SelectCell
} from 'loot-design/src/components/table';
import useSelected, {
useSelectedDispatch,
useSelectedItems,
SelectedProvider
} from 'loot-design/src/components/useSelected';
import { colors } from 'loot-design/src/style';
import { Page } from '../Page';
import DisplayId from '../util/DisplayId';
import { ScheduleAmountCell } from './SchedulesTable';
let ROW_HEIGHT = 43;
function DiscoverSchedulesTable({ schedules, loading }) {
let selectedItems = useSelectedItems();
let dispatchSelected = useSelectedDispatch();
function renderItem({ item }) {
let selected = selectedItems.has(item.id);
let amountOp = item._conditions.find(c => c.field === 'amount').op;
let recurDescription = getRecurringDescription(item.date);
return (
{
dispatchSelected({ type: 'select', id: item.id });
}}
borderColor={selected ? colors.b8 : colors.border}
style={{
cursor: 'pointer',
backgroundColor: selected ? colors.selected : 'white',
':hover': {
backgroundColor: selected ? colors.selected : colors.hover
}
}}
>
{
dispatchSelected({ type: 'select', id: item.id });
}}
/>
{recurDescription}
);
}
return (
0}
onSelect={() => dispatchSelected({ type: 'select-all' })}
/>
PayeeAccount
When
Amount
selectedItems.has(id)}
renderItem={renderItem}
renderEmpty="No schedules found"
/>
);
}
export default function DiscoverSchedules() {
let location = useLocation();
let history = useHistory();
let [schedules, setSchedules] = useState();
let [creating, setCreating] = useState(false);
let selectedInst = useSelected('discover-schedules', schedules, []);
useEffect(() => {
async function run() {
setSchedules(await send('schedule/discover'));
}
run();
}, []);
async function onCreate() {
let items = selectedInst.items;
let selected = schedules.filter(s => selectedInst.items.has(s.id));
setCreating(true);
for (let schedule of selected) {
let scheduleId = await send('schedule/create', {
conditions: schedule._conditions
});
// Now query for matching transactions and link them automatically
let { filters } = await send('make-filters-from-conditions', {
conditions: schedule._conditions
});
if (filters.length > 0) {
let { data: transactions } = await runQuery(
q('transactions')
.filter({ $and: filters })
.select('id')
);
await send('transactions-batch-update', {
updated: transactions.map(t => ({
id: t.id,
schedule: scheduleId
}))
});
}
}
setCreating(false);
history.goBack();
}
return (
We found some possible schedules in your current transactions. Select
the ones you want to create.
If you expected a schedule here and don't see it, it might be because
the payees of the transactions don't match. Make sure you rename payees
on all transactions for a schedule to be the same payee.
You can always do this later
{Platform.isBrowser
? ' from the "Find schedules" item in the sidebar menu'
: ' from the "Tools > Find schedules" menu item'}
.