Skip to content

Commit 9eb4213

Browse files
committed
Firebase POST data.
1 parent 5c7caf1 commit 9eb4213

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

index.html

+1-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@
99
<body>
1010
<script src="https://www.gstatic.com/firebasejs/4.6.2/firebase.js"></script>
1111
<script>
12-
const config = {
13-
apiKey: "AIzaSyCplihRW4j3gUo7uvpndnyXAYfQgPRtTn4",
14-
authDomain: "emrg-pcg.firebaseapp.com",
15-
databaseURL: "https://emrg-pcg.firebaseio.com",
16-
projectId: "emrg-pcg",
17-
storageBucket: "emrg-pcg.appspot.com",
18-
messagingSenderId: "972956794987"
19-
};
20-
firebase.initializeApp(config);
12+
const config = { databaseURL: "https://emrg-pcg.firebaseio.com" };
2113
</script>
2214
<script src="/third_party/bundle.min.js"></script>
2315
<script src="/third_party/codemirror.min.js"></script>

js/editor.js

+35-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { h, render, Component } = preact;
22
const { route, Router, Link } = preactRouter;
33

44
const BASE_REST_URL = config.databaseURL;
5-
const SKETCH_REST_URL = BASE_REST_URL + '/sketch/';
5+
const SKETCH_REST_URL = BASE_REST_URL + '/sketch';
66

77
function debounce(func, wait) {
88
let timeout;
@@ -143,7 +143,7 @@ class Home extends Component {
143143
}
144144

145145
async function loadSketch(url) {
146-
let getRequest = new Request(`${SKETCH_REST_URL}${url}.json`);
146+
let getRequest = new Request(`${SKETCH_REST_URL}/${url}.json`);
147147
const res = await fetch(getRequest, { method: 'GET' });
148148
const json = await res.json();
149149
return json;
@@ -226,7 +226,8 @@ class Editor extends Component {
226226
localSource = window.localStorage.getItem('empty');
227227
if (localSource !== null && localSource !== undefined) {
228228
this.props.onSourceChanged(localSource, true, true);
229-
this.setState({ source: localSource, origSource: INITIAL_TEXT });
229+
this.setState({ source: localSource });
230+
this.props.setRemoteSource(INITIAL_TEXT);
230231
} else {
231232
this.props.onSourceChanged(INITIAL_TEXT, true);
232233
}
@@ -248,8 +249,9 @@ class Editor extends Component {
248249
const sketch = Object.assign({ key: this.props.id }, json);
249250
let newState = { loading: false, source: sketch.source };
250251
localSource = window.localStorage.getItem(this.props.id);
252+
let remoteSource;
251253
if (localSource !== null && localSource !== undefined && localSource !== sketch.source) {
252-
newState.origSource = sketch.source;
254+
remoteSource = sketch.source;
253255
newState.source = localSource;
254256
}
255257
const urlSeed = getURLParameter('seed');
@@ -262,6 +264,9 @@ class Editor extends Component {
262264
}
263265

264266
this.setState(newState);
267+
if (remoteSource !== undefined) {
268+
this.props.setRemoteSource(remoteSource);
269+
}
265270
if (this.props.onSourceChanged) {
266271
this.props.onSourceChanged(newState.source, true, localSource !== null && localSource !== undefined);
267272
}
@@ -351,10 +356,11 @@ class Editor extends Component {
351356
}
352357
}
353358

354-
restoreOriginalVersion() {
359+
restoreRemoteVersion() {
355360
if (confirm('Are you sure you want to restore to the original version? This will discard your changes.')) {
356-
this.setState({ source: this.state.origSource, origSource: undefined });
357-
this.props.onSourceChanged(this.state.origSource, true);
361+
this.setState({ source: this.props.remoteSource });
362+
this.props.setRemoteSource(undefined);
363+
this.props.onSourceChanged(this.props.remoteSource, true);
358364
window.localStorage.removeItem(this.props.id || 'empty');
359365
this.generate();
360366
}
@@ -364,10 +370,10 @@ class Editor extends Component {
364370
const debugView = h('div', { className: 'editor__debug' }, this.state.debugOutput);
365371
const source = state.loading ? 'Loading...' : state.source;
366372
let localVersionDiv;
367-
if (this.state.origSource) {
373+
if (this.props.remoteSource) {
368374
localVersionDiv = h('div', { className: 'localversion' },
369375
'You\'ve previously made some changes to this sketch. ',
370-
h('a', {class: 'underline', href: '#', onClick: this.restoreOriginalVersion.bind(this) }, 'Restore original version.'));
376+
h('a', {class: 'underline', href: '#', onClick: this.restoreRemoteVersion.bind(this) }, 'Restore original version.'));
371377
}
372378
return h('div', { className: 'editor' }, localVersionDiv,
373379
h('div', { className: 'editor__source-wrap'},
@@ -407,6 +413,10 @@ class Sketch extends Component {
407413
this.setState({ importError: true });
408414
}
409415

416+
setRemoteSource(remoteSource) {
417+
this.setState({ remoteSource });
418+
}
419+
410420
onSourceChanged(source, initialLoad, localSource=false) {
411421
this.setState({ unsaved: localSource || !initialLoad, source });
412422
if (!initialLoad) {
@@ -433,7 +443,7 @@ class Sketch extends Component {
433443
});
434444
}
435445

436-
onSave() {
446+
async onSave() {
437447
console.assert(this.state.source !== undefined);
438448
console.assert(this.state.seed !== undefined);
439449
if (this.state.saving) return;
@@ -442,15 +452,21 @@ class Sketch extends Component {
442452
sketch.source = this.state.source;
443453
sketch.seed = this.state.seed;
444454
if (this.props.id) sketch.parent = this.props.id;
445-
firebase.database().goOnline();
446-
const ref = firebase.database().ref('sketch').push();
447-
ref.set(sketch, () => {
455+
const res = await fetch(new Request(`${SKETCH_REST_URL}.json`), {
456+
method: 'POST',
457+
body: JSON.stringify(sketch),
458+
headers: new Headers({'Content-Type': 'application/json'})
459+
});
460+
const json = await res.json();
461+
if (json) {
462+
const ref = json.name;
448463
this.setState({ saving: false, unsaved: false });
449464
window.localStorage.removeItem(this.props.id || 'empty');
450-
route(`/sketch/${ref.key}`);
451-
}).then(() => {
452-
firebase.database().goOffline();
453-
});
465+
this.setRemoteSource(undefined);
466+
route(`/sketch/${json.name}`);
467+
} else {
468+
throw new Error('Error: Could not save sketch');
469+
}
454470
}
455471

456472
render(props, state) {
@@ -461,6 +477,8 @@ class Sketch extends Component {
461477
),
462478
h(Editor, {
463479
id: props.id,
480+
remoteSource: this.state.remoteSource,
481+
setRemoteSource: this.setRemoteSource.bind(this),
464482
importError: state.importError,
465483
onImportError: this.onImportError.bind(this),
466484
onSourceChanged: this.onSourceChanged.bind(this),

0 commit comments

Comments
 (0)