diff --git a/src/components/CollectionDB.js b/src/components/CollectionDB.js new file mode 100644 index 0000000..8ab24be --- /dev/null +++ b/src/components/CollectionDB.js @@ -0,0 +1,86 @@ +import loki from 'lokijs'; +import {observable, autorun} from "mobx"; + +export default class CollectionDB { + @observable built = []; // Keeps track of what collections have been populated + + constructor(API) { + this.api = API; + // ignoring persistence for now + // this.setupDB(); + //autorun(() => console.log(this.creatures)); + let db = new loki("chaotic_portal.db"); + this.attacks = db.addCollection('attacks'); + this.battlegear = db.addCollection('battlegear'); + this.creatures = db.addCollection('creatures'); + this.locations = db.addCollection('locations'); + this.mugic = db.addCollection('mugic'); + this.db = db; + } + + // setupDB() { + // var self = this; + // let db = new loki("chaotic_portal.db", { autosave: true, autoload: true, autoloadCallback: databaseInitialize, autosaveInterval: 4000, persistenceMethod: 'localStorage' }); + // this.db = db; + + // let databaseInitialize = () => { + // var entries; + // if ((entries = db.getCollection("attacks")) === null) + // entries = db.addCollection("attacks"); + // self.attacks = entries; + + // if ((entries = db.getCollection("battlegear")) === null) + // entries = db.addCollection("battlegear"); + // self.battlegear = entries; + + // console.log(db.getCollection("creatures")); + // if ((entries = db.getCollection("creatures")) === null) + // entries = db.addCollection("creatures"); + // self.creatures = db.addCollection('creatures'); + + // if ((entries = db.getCollection("locations")) === null) + // entries = db.addCollection("locations"); + // self.locations = entries + + // if ((entries = db.getCollection("mugic")) === null) + // entries = db.addCollection("mugic"); + // self.mugic = entries; + // }; + // } + + setup(spreadsheet, callback) { + this.api.getSpreadsheet(spreadsheet, (data) => { + callback(data.map((item) => { + let temp = {}; + delete item.content; + for (const key of Object.keys(item)) { + temp[key] = item[key].$t; + } + return temp; + })); + }); + } + + setupAttacks() { + + } + + setupBattleGear() { + + } + + setupCreatures(tribe="Generic") { + this.setup(this.api.urls.Creatures[tribe], (data) => { + this.creatures.insert(data); + this.built.push("creatures_"+tribe); + }); + } + + setupLocations() { + + } + + setupMugic(tribe) { + + } +} diff --git a/src/components/SpreadsheetData.js b/src/components/SpreadsheetData.js index 2eec047..eebf412 100644 --- a/src/components/SpreadsheetData.js +++ b/src/components/SpreadsheetData.js @@ -1,13 +1,12 @@ import 'whatwg-fetch'; -import CardsDB from './database/cards'; -import PortalDB from './database/portal'; +import CollectionDB from './CollectionDB'; import {observable} from "mobx"; class API { @observable portal = null; @observable cards = null; @observable urls = null; - @observable instance = null; + instance = null; static base_url = "https://spreadsheets.google.com/feeds/list/"; static data_format = "/od6/public/values?alt=json"; @@ -54,8 +53,8 @@ class API { setupDB() { try { - this.portal = new PortalDB(this); - this.cards = new CardsDB(this); + this.portal = new CollectionDB(this, "portal"); + this.cards = new CollectionDB(this, "cards"); } catch (err) { console.log('setting up database failed', err); diff --git a/src/components/database/cards.js b/src/components/database/cards.js deleted file mode 100644 index 42425ae..0000000 --- a/src/components/database/cards.js +++ /dev/null @@ -1,35 +0,0 @@ -export default class CardsDB { - constructor(API) { - // this.api = API; - // this.attacks = db.addCollection('attacks'); - // this.battlegear = db.addCollection('battlegear'); - // this.creatures = db.addCollection('creatures'); - // this.locations = db.addCollection('locations'); - // this.mugic = db.addCollection('mugic'); - // this.db = db; - } - - setupCreatures(spreadsheet) { - this.getSpreadsheet(spreadsheet, function(json) { - - }); - } - - setupMugic(spreadsheet) { - this.getSpreadsheet(spreadsheet, function(json) { - - }); - } - - setupBattleGear(spreadsheet) { - this.getSpreadsheet(spreadsheet, function(json) { - - }); - } - - setupLocations(spreadsheet) { - this.getSpreadsheet(spreadsheet, function(json) { - - }); - } -} diff --git a/src/components/database/portal.js b/src/components/database/portal.js deleted file mode 100644 index 5ec5142..0000000 --- a/src/components/database/portal.js +++ /dev/null @@ -1,86 +0,0 @@ -import loki from 'lokijs'; -import {observable, autorun} from "mobx"; - -export default class PortalDB { - @observable built = []; // Keeps track of what collections have been populated - - constructor(API) { - this.api = API; - // ignoring persistence for now - // this.setupDB(); - //autorun(() => console.log(this.creatures)); - let db = new loki("chaotic_portal.db"); - this.attacks = db.addCollection('attacks'); - this.battlegear = db.addCollection('battlegear'); - this.creatures = db.addCollection('creatures'); - this.locations = db.addCollection('locations'); - this.mugic = db.addCollection('mugic'); - this.db = db; - } - - setupDB() { - var self = this; - let db = new loki("chaotic_portal.db", { autosave: true, autoload: true, autoloadCallback: databaseInitialize, autosaveInterval: 4000, persistenceMethod: 'localStorage' }); - this.db = db; - - let databaseInitialize = () => { - var entries; - if ((entries = db.getCollection("attacks")) === null) - entries = db.addCollection("attacks"); - self.attacks = entries; - - if ((entries = db.getCollection("battlegear")) === null) - entries = db.addCollection("battlegear"); - self.battlegear = entries; - - console.log(db.getCollection("creatures")); - if ((entries = db.getCollection("creatures")) === null) - entries = db.addCollection("creatures"); - self.creatures = db.addCollection('creatures'); - - if ((entries = db.getCollection("locations")) === null) - entries = db.addCollection("locations"); - self.locations = entries - - if ((entries = db.getCollection("mugic")) === null) - entries = db.addCollection("mugic"); - self.mugic = entries; - }; - } - - setup(spreadsheet, callback) { - this.api.getSpreadsheet(spreadsheet, (data) => { - callback(data.map((item) => { - let temp = {}; - delete item.content; - for (const key of Object.keys(item)) { - temp[key] = item[key].$t; - } - return temp; - })); - }); - } - - setupAttacks() { - - } - - setupBattleGear() { - - } - - setupCreatures(tribe) { - this.setup(this.api.urls.Creatures[tribe], (data) => { - this.creatures.insert(data); - this.built.push("creatures_"+tribe); - }); - } - - setupLocations() { - - } - - setupMugic(tribe) { - - } -} diff --git a/src/components/portal/Category/Creatures.js b/src/components/portal/Category/Creatures.js index f51e104..b7ea23e 100644 --- a/src/components/portal/Category/Creatures.js +++ b/src/components/portal/Category/Creatures.js @@ -4,54 +4,20 @@ import { Link } from 'react-router'; import PageNotFound from '../../PageNotFound'; import API from '../../SpreadsheetData'; import s from '../../../styles/app.style'; +import UnderConstruction from '../../UnderConstruction'; export default class Creatures extends React.Component { - constructor(props) { - super (props); - this.state = {tribe: '', creatures: {}}; - } - - componentWillReceiveProps(nextProps) { - this.getData(nextProps); - } - - componentDidMount() { - this.getData(this.props); - } - - // ** Process the tribe ** // - // /portal/Creatures/ - // /portal/{Tribe}/Creatures/ - // The first / gets counted - getData(props) { - if (props.children) return this.props = props; - let path = props.location.pathname.split("/"); - if (path[path.length-1] == "") path.pop(); // Remove trailing backslash - - // Set tribe - let tribe = (path.length === 4) ? path[2] : "All"; - this.setState({tribe: tribe}); - - // For each tribe, get its spreadsheet, set the state - var self = this; - let urls = (tribe == "All") ? API.Creatures : {[tribe]: API.Creatures[tribe]}; - Object.keys(urls).map((tribe) => { - API.getSpreadsheet(urls[tribe], (data) => { - self.setState({creatures: - Object.assign(self.state.creatures, {[tribe]: data}) - }); - }); - }); - // self.setState({creatures: this.state.creatures.concat([data])}); - } - - hacks(event) { - console.log(event); - this.setState({click: true}); - } - render() { + if (this.props.children) { + return (