import { h, Component } from 'preact'; import { ListingRow } from './dashboard/listingRow'; export class ListingDashboard extends Component { state = { listings: [], orgListings: [], orgs: [], userCredits: 0, selectedListings: 'user', }; componentDidMount() { const t = this; const container = document.getElementById('classifieds-listings-dashboard'); let listings = []; let orgs = []; let orgListings = []; listings = JSON.parse(container.dataset.listings); orgs = JSON.parse(container.dataset.orgs); orgListings = JSON.parse(container.dataset.orglistings); const userCredits = container.dataset.usercredits; t.setState({ listings, orgListings, orgs, userCredits }); } render() { const { listings, orgListings, userCredits, orgs, selectedListings, } = this.state; const showListings = (selected, userListings, organizationListings) => { return selected === 'user' ? userListings.map(listing => ) : organizationListings.map(listing => listing.organization_id === selected ? ( ) : ( '' ), ); }; const orgButtons = orgs.map(org => ( this.setState({ selectedListings: org.id })} className={`rounded-btn ${selectedListings === org.id ? 'active' : ''}`} > {org.name} )); const listingLength = (selected, userListings, organizationListings) => { return selected === 'user' ? (

Listings Made: {' '} {userListings.length}

) : (

Listings Made: {' '} { organizationListings.filter( listing => listing.organization_id === selected, ).length }

); }; const creditCount = (selected, userCreds, organizations) => { return selected === 'user' ? (

Credits Available: {' '} {userCredits}

) : (

Credits Available: {' '} {organizations.find(org => org.id === selected).unspent_credits_count}

); }; return (
this.setState({ selectedListings: 'user' })} className={`rounded-btn ${ selectedListings === 'user' ? 'active' : '' }`} > Personal {orgButtons}

Listings

{listingLength(selectedListings, listings, orgListings)} Create a Listing

Credits

{creditCount(selectedListings, userCredits, orgs)} Buy Credits
{showListings(selectedListings, listings, orgListings)}
); } }