import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import './light.css';
import { Container, Checkbox, Form, Header, Segment, Table } from 'semantic-ui-react';
import * as Datetime from 'react-datetime';
import 'react-datetime/css/react-datetime.css';
import moment from 'moment';
import { requester } from './utils.js';
import { TransactionList, TransactionEditor } from './Transactions.js';
export function AdminReportedTransactions(props) {
const { token } = props;
const [transactions, setTransactions] = useState(false);
const [error, setError] = useState(false);
useEffect(() => {
requester('/transactions/', 'GET', token)
.then(res => {
setTransactions(res.results);
setError(false);
})
.catch(err => {
console.log(err);
setError(true);
});
}, []);
return (
{!error ?
transactions ?
:
Loading...
:
Error loading.
}
);
};
let transactionsCache = false;
let summaryCache = false;
let excludePayPalCache = false;
export function AdminHistoricalTransactions(props) {
const { token } = props;
const [input, setInput] = useState({ month: moment() });
const [transactions, setTransactions] = useState(transactionsCache);
const [summary, setSummary] = useState(summaryCache);
const [excludePayPal, setExcludePayPal] = useState(excludePayPalCache);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
const handleDatetime = (v) => setInput({ ...input, month: v });
const handleExcludePayPal = (e, v) => {
setExcludePayPal(v.checked);
excludePayPalCache = v.checked;
};
const handleSubmit = (e) => {
if (loading) return;
setLoading(true);
const month = input.month.format('YYYY-MM');
requester('/transactions/?month=' + month, 'GET', token)
.then(res => {
setLoading(false);
setError(false);
setTransactions(res.results);
transactionsCache = res.results;
})
.catch(err => {
setLoading(false);
console.log(err);
setError(true);
});
requester('/transactions/summary/?month=' + month, 'GET', token)
.then(res => {
setLoading(false);
setError(false);
setSummary(res);
summaryCache = res;
})
.catch(err => {
setLoading(false);
console.log(err);
setError(true);
});
};
return (
Submit
{transactions &&
Found {transactions.length} transactions.
}
{!error ?
summary &&
Category
Dollar
Protocoin
{summary.map(x =>
{x.category}
{'$ ' + x.dollar.toFixed(2)}
{'₱ ' + x.protocoin.toFixed(2)}
)}
:
Error loading summary.
}
{!error ?
transactions &&
{!!transactions.length &&
{moment(transactions[0].date, 'YYYY-MM-DD').format('MMMM YYYY')} Transactions
}
!excludePayPal || x.account_type !== 'PayPal')} />
:
Error loading transactions.
}
);
};
export function AdminAddTransaction(props) {
const { token } = props;
const [input, setInput] = useState({ date: moment().format('YYYY-MM-DD'), info_source: 'Web' });
const [error, setError] = useState(false);
const [loading, setLoading] = useState(false);
const [success, setSuccess] = useState(false);
const handleSubmit = (e) => {
if (loading) return;
setLoading(true);
setSuccess(false);
requester('/transactions/', 'POST', token, input)
.then(res => {
setSuccess(res.id);
setInput({});
setLoading(false);
setError(false);
})
.catch(err => {
setLoading(false);
console.log(err);
setError(err.data);
});
};
return (
Submit
{success && Added! View the transaction.
}
);
};
export function AdminTransactions(props) {
return (
);
}
export function AdminMemberTransactions(props) {
const { result } = props;
const transactions = result.transactions;
return (
Add a transaction
{transactions.length ?
:
None
}
);
};