refactoring database setup

This commit is contained in:
Daniel
2018-01-26 13:24:57 -05:00
parent 707e8df333
commit 0680703c5c
6 changed files with 66 additions and 22 deletions

View File

@@ -60,8 +60,8 @@ class API {
setupDB() {
try {
this.portal = new CollectionDB(this);
this.cards = new CollectionDB(this);
this.portal = new CollectionDB(this, 'portal');
this.cards = new CollectionDB(this, 'cards');
}
catch (err) {
console.log('setting up database failed', err);
@@ -91,20 +91,41 @@ class API {
};
}
// Input format
// [{cards: 'attacks'}, {portal: 'attacks'}]
async buildCollection(input) {
return await Promise.all(input.map((item) => {
return new Promise((resolve, reject) => {
if ('cards' in item) {
// console.log(this.cards[item.cards].data);
return this.cards.setupType(item.cards, resolve);
}
if ('portal' in item) {
// console.log(this.portal[item.portal].data);
return this.portal.setupType(item.portal, resolve);
}
console.log('cards or portal');
return reject();
});
}));
}
}
export default API.getInstance();
// export default new API();
class CollectionDB {
@observable built = []; // Keeps track of what collections have been populated
// Keeps track of what collections have been populated
@observable built = [];
building = [];
constructor(API) {
constructor(API, format) {
this.api = API;
// ignoring persistence for now
// this.setupDB();
//autorun(() => console.log(this.creatures));
this.format = format;
this.setupDB();
}
setupDB() {
let db = new loki("chaotic_portal.db");
this.attacks = db.addCollection('attacks');
this.battlegear = db.addCollection('battlegear');
@@ -112,9 +133,10 @@ class CollectionDB {
this.locations = db.addCollection('locations');
this.mugic = db.addCollection('mugic');
this.db = db;
}
// setupDB() {
// ignoring persistence for now
//autorun(() => console.log(this.creatures));
// var self = this;
// let db = new loki("chaotic_portal.db", { autosave: true, autoload: true, autoloadCallback: databaseInitialize, autosaveInterval: 4000, persistenceMethod: 'localStorage' });
// this.db = db;
@@ -142,7 +164,7 @@ class CollectionDB {
// entries = db.addCollection("mugic");
// self.mugic = entries;
// };
// }
}
setup(spreadsheet, type, callback) {
this.api.getSpreadsheet(spreadsheet, (data) => {
@@ -158,6 +180,20 @@ class CollectionDB {
});
}
// example format
// this.setup(this.api.urls.Attacks["portal"], "Attack", (data) => {});
async setupType(type, resolve) {
let uc_type = type.charAt(0).toUpperCase() + type.slice(1);
console.log(type, uc_type, this.format);
// this.setup(this.api.urls[uc_type][this.format], uc_type, (data) => {
// this[type].insert(data);
// this.built.push(type);
// }
resolve();
}
setupAttacks(type="portal") {
this.setup(this.api.urls.Attacks[type], "Attack", (data) => {
this.attacks.insert(data);

View File

@@ -1,6 +1,6 @@
import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter as Router, Link, Route } from 'react-router-dom';
import { BrowserRouter as Router, Link, Route, withRouter } from 'react-router-dom';
import {observer, inject} from 'mobx-react';
import s from '../styles/app.style';
@@ -11,11 +11,11 @@ import Collection from './collection/index';
import Portal from './portal/index';
import Home from './Home';
const BlockAvoider = withRouter(Base)
render(
<Router>
<div>
<Route path="*" component={Base}/>
</div>
<BlockAvoider />
</Router>
, document.getElementById('root'),
);
@@ -42,7 +42,6 @@ function Base(props) {
return (
<div>
<link rel="stylesheet" type="text/css" href="/src/css/legacy.css" />
<div className="fix-pgBkgrnd-repeat-x">
<div className={"fix-img-bkgrnd fix-img-bkgrnd_"+bkgrnd}></div>
</div>

View File

@@ -1,6 +1,7 @@
import React from 'react';
import Interactive from 'react-interactive';
import { Link, Route } from 'react-router-dom';
import {observable} from 'mobx';
import {observer, inject} from 'mobx-react';
import s from '../../../styles/app.style';
import API from '../../SpreadsheetData';
@@ -8,6 +9,7 @@ import Attack from '../Single/Attack';
@inject((stores, props, context) => props) @observer
export default class Attacks extends React.Component {
@observable loaded = false;
render() {
@@ -30,9 +32,15 @@ export default class Attacks extends React.Component {
return (<span>Loading...</span>);
}
const attacks = API.portal.attacks.data;
if (this.loaded == false) {
API.buildCollection([{'cards': 'attacks'}, {'portal': 'attacks'}])
.then(() => {
this.loaded = true;
});
return (<span>Loading...</span>);
}
const output = attacks.map((attack, i) => {
const output = API.portal.attacks.data.map((attack, i) => {
const card_data = API.cards.attacks.findOne({'gsx$name': attack.gsx$name});
return (
<div key={i}>