import React from 'react'; import {observable} from "mobx"; import {observer, inject} from 'mobx-react'; import loki from 'lokijs'; import Collapsible from 'react-collapsible'; import API from '../SpreadsheetData'; @inject((stores, props, context) => props) @observer export default class SearchCollection extends React.Component { @observable loaded = false; @observable input = {}; constructor(props) { super(props); this.filter = new loki("filter.db"); // Binding for keeping scope this.search = this.search.bind(this); this.handleChange = this.handleChange.bind(this); this.reset = this.reset.bind(this); this.parseQuery(); } componentDidMount() { // this.search(); } parseQuery() { this.props.handleContent([{'text': 'Loading...'}]); const queryString = this.props.location.search.toLowerCase(); let query = {}; let pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&'); for (let i = 0; i < pairs.length; i++) { let pair = pairs[i].split('='); query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || ''); } console.log(query); if (query.sets) { query.sets.split(',').map(item => { this.input[item.toUpperCase()] = true; }); } } updateQuery() { let query = {sets:[]}; Object.keys(this.input).forEach((key) => { if (key in API.sets) query.sets.push(key); }); let queryString = ""; // Sets if (query.sets.length > 0) { queryString += "sets="; query.sets.forEach(item => { queryString += item.toLowerCase(); }); queryString += "&"; } // encodeURIComponent // Strip trailing & queryString = queryString.replace(/\&$/, ''); this.props.history.push('/collection/?'+(queryString)); } reset(event) { event.preventDefault(); event.stopPropagation(); this.props.history.push('/collection/'); Object.keys(this.input).forEach((key) => { (typeof(this.input[key]) === 'boolean' ? this.input[key] = false : this.input[key] = ''); }); } handleChange(event) { const target = event.target; const value = target.type === 'checkbox' ? target.checked : target.value; const name = target.name; this.input[name] = value; console.log(this.input); } render() { if (API.urls === null || API.portal === null || API.cards === null) { return (Loading...); } if (this.loaded == false) { API.buildCollection([{'cards': 'attacks'}, {'cards': 'battlegear'}, {'cards': 'creatures'}, {'cards': 'locations'}, {'cards': 'mugic'}]) .then(() => { this.loaded = true; this.search(); }); return (Loading...); } let setsInput = []; for (const key in API.sets) { setsInput.push(); } return (