/* Copyright © XlXi 2022 */ import { Component, createRef } from 'react'; import axios from 'axios'; import { buildGenericApiUrl } from '../util/HTTP.js'; import Loader from './Loader'; axios.defaults.withCredentials = true; class SummaryTab extends Component { constructor(props) { super(props); this.state = { loaded: false, filters: [ {label: 'Past Day', tag: 'pastday', ref: createRef()}, {label: 'Past Week', tag: 'pastweek', ref: createRef()}, {label: 'Past Month', tag: 'pastmonth', ref: createRef()}, {label: 'Past Year', tag: 'pastyear', ref: createRef()}, {label: 'All Time', tag: 'all', ref: createRef()} ], items: [], total: null }; this.categoryDropdown = createRef(); this.setTag = this.setTag.bind(this); } componentDidMount() { this.setTag('pastday'); } setTag(tag) { this.setState({ loaded: false }); let selectedTag = this.state.filters.find(obj => { return obj.tag === tag }); this.categoryDropdown.current.innerText = selectedTag.label; this.state.filters.map(({ label, tag, ref }) => { if(ref.current.innerText == selectedTag.label) ref.current.classList.add('active'); else ref.current.classList.remove('active'); }); axios.get(buildGenericApiUrl('api', `shop/v1/user-summary?filter=${tag}`)) .then(res => { const response = res.data; this.setState({ items: response.columns, total: response.total, loaded: true }); }); } render() { return (

Time Period:

{ this.state.loaded ? <>

Tokens

{ this.state.items.map(({ name, total }, index) => ) }
Categories Credit
{ name } { total != 0 ? { total } : null }

Total { this.state.total }

:
}
) } } class TransactionsTab extends Component { constructor(props) { super(props); this.state = { loaded: false, loadingMore: false, filters: [ {label: 'Purchases', tag: 'purchases', ref: createRef()}, {label: 'Sales', tag: 'sales', ref: createRef()}, {label: 'Commissions', tag: 'commissions', ref: createRef()}, {label: 'Group Payouts', tag: 'grouppayouts', ref: createRef()} ], items: [] }; this.categoryDropdown = createRef(); this.setTag = this.setTag.bind(this); this.loadMore = this.loadMore.bind(this); } componentWillMount() { window.addEventListener('scroll', this.loadMore); } componentWillUnmount() { window.removeEventListener('scroll', this.loadMore); } componentDidMount() { this.setTag('purchases'); } setTag(tag) { this.setState({ loaded: false }); let selectedTag = this.state.filters.find(obj => { return obj.tag === tag }); this.categoryDropdown.current.innerText = selectedTag.label; this.state.filters.map(({ label, tag, ref }) => { if(ref.current.innerText == selectedTag.label) ref.current.classList.add('active'); else ref.current.classList.remove('active'); }); this.nextCursor = null; axios.get(buildGenericApiUrl('api', `shop/v1/user-transactions?filter=${tag}`)) .then(res => { const response = res.data; this.nextCursor = response.next_cursor; this.setState({ items: response.data, filter: tag, loaded: true }); }); } loadMore() { // XlXi: Taking the height of the footer into account. if (window.innerHeight + document.documentElement.scrollTop >= document.scrollingElement.scrollHeight-200) { if (!!(this.nextCursor) && !this.state.loading && !this.state.loadingMore) { this.setState({ loadingMore: true }); axios.get(buildGenericApiUrl('api', `shop/v1/user-transactions?filter=${this.state.filter}&cursor=${this.nextCursor}`)) .then(res => { const response = res.data; this.nextCursor = response.next_cursor; this.setState({ items: this.state.items.concat(response.data), loadingMore: false }); }); } } } render() { return (

Currently Selected:

{ this.state.loaded ? <>

Tokens

{ this.state.items.map(({ date, member, description, amount, item }, index) => ) }
Date Member Description Amount
{ date } { member.name } { description }{ item != null ? { item.name } : null }

{ amount }

{ this.state.loadingMore ?
: null } :
}
) } } class Transactions extends Component { constructor(props) { super(props); this.state = { loaded: false, tabs: [ {label: 'Summary', name: 'summary', ref: createRef()}, {label: 'My Transactions', name: 'transactions', ref: createRef()} ] }; this.tabIndex = 0; this.setCurrentTab = this.setCurrentTab.bind(this); this.setTab = this.setTab.bind(this); } componentDidMount() { const params = new Proxy(new URLSearchParams(window.location.search), { get: (searchParams, prop) => searchParams.get(prop), }); let queryTab = this.state.tabs.find(obj => { if(!params.tab) return false; return obj.name === params.tab.toLowerCase(); }); if(queryTab) this.setTab(queryTab.name); else this.setTab('summary'); } setCurrentTab(instance) { this.currentTab = instance; this.setState({loaded: true}); } setTab(tabType) { this.setState({loaded: false}); this.tabIndex += 1; switch(tabType) { case 'summary': this.setCurrentTab(); break; case 'transactions': this.setCurrentTab(); break; } this.state.tabs.map(({ name, ref }) => { if(name == tabType) ref.current.classList.add('active'); else ref.current.classList.remove('active'); }); } render() { return ( <>
{ this.state.loaded ? this.currentTab : }
); } } export default Transactions;