@@ -2,7 +2,7 @@ const { h, render, Component } = preact;
2
2
const { route, Router, Link } = preactRouter ;
3
3
4
4
const BASE_REST_URL = config . databaseURL ;
5
- const SKETCH_REST_URL = BASE_REST_URL + '/sketch/ ' ;
5
+ const SKETCH_REST_URL = BASE_REST_URL + '/sketch' ;
6
6
7
7
function debounce ( func , wait ) {
8
8
let timeout ;
@@ -143,7 +143,7 @@ class Home extends Component {
143
143
}
144
144
145
145
async function loadSketch ( url ) {
146
- let getRequest = new Request ( `${ SKETCH_REST_URL } ${ url } .json` ) ;
146
+ let getRequest = new Request ( `${ SKETCH_REST_URL } / ${ url } .json` ) ;
147
147
const res = await fetch ( getRequest , { method : 'GET' } ) ;
148
148
const json = await res . json ( ) ;
149
149
return json ;
@@ -226,7 +226,8 @@ class Editor extends Component {
226
226
localSource = window . localStorage . getItem ( 'empty' ) ;
227
227
if ( localSource !== null && localSource !== undefined ) {
228
228
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 ) ;
230
231
} else {
231
232
this . props . onSourceChanged ( INITIAL_TEXT , true ) ;
232
233
}
@@ -248,8 +249,9 @@ class Editor extends Component {
248
249
const sketch = Object . assign ( { key : this . props . id } , json ) ;
249
250
let newState = { loading : false , source : sketch . source } ;
250
251
localSource = window . localStorage . getItem ( this . props . id ) ;
252
+ let remoteSource ;
251
253
if ( localSource !== null && localSource !== undefined && localSource !== sketch . source ) {
252
- newState . origSource = sketch . source ;
254
+ remoteSource = sketch . source ;
253
255
newState . source = localSource ;
254
256
}
255
257
const urlSeed = getURLParameter ( 'seed' ) ;
@@ -262,6 +264,9 @@ class Editor extends Component {
262
264
}
263
265
264
266
this . setState ( newState ) ;
267
+ if ( remoteSource !== undefined ) {
268
+ this . props . setRemoteSource ( remoteSource ) ;
269
+ }
265
270
if ( this . props . onSourceChanged ) {
266
271
this . props . onSourceChanged ( newState . source , true , localSource !== null && localSource !== undefined ) ;
267
272
}
@@ -351,10 +356,11 @@ class Editor extends Component {
351
356
}
352
357
}
353
358
354
- restoreOriginalVersion ( ) {
359
+ restoreRemoteVersion ( ) {
355
360
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 ) ;
358
364
window . localStorage . removeItem ( this . props . id || 'empty' ) ;
359
365
this . generate ( ) ;
360
366
}
@@ -364,10 +370,10 @@ class Editor extends Component {
364
370
const debugView = h ( 'div' , { className : 'editor__debug' } , this . state . debugOutput ) ;
365
371
const source = state . loading ? 'Loading...' : state . source ;
366
372
let localVersionDiv ;
367
- if ( this . state . origSource ) {
373
+ if ( this . props . remoteSource ) {
368
374
localVersionDiv = h ( 'div' , { className : 'localversion' } ,
369
375
'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.' ) ) ;
371
377
}
372
378
return h ( 'div' , { className : 'editor' } , localVersionDiv ,
373
379
h ( 'div' , { className : 'editor__source-wrap' } ,
@@ -407,6 +413,10 @@ class Sketch extends Component {
407
413
this . setState ( { importError : true } ) ;
408
414
}
409
415
416
+ setRemoteSource ( remoteSource ) {
417
+ this . setState ( { remoteSource } ) ;
418
+ }
419
+
410
420
onSourceChanged ( source , initialLoad , localSource = false ) {
411
421
this . setState ( { unsaved : localSource || ! initialLoad , source } ) ;
412
422
if ( ! initialLoad ) {
@@ -433,7 +443,7 @@ class Sketch extends Component {
433
443
} ) ;
434
444
}
435
445
436
- onSave ( ) {
446
+ async onSave ( ) {
437
447
console . assert ( this . state . source !== undefined ) ;
438
448
console . assert ( this . state . seed !== undefined ) ;
439
449
if ( this . state . saving ) return ;
@@ -442,15 +452,21 @@ class Sketch extends Component {
442
452
sketch . source = this . state . source ;
443
453
sketch . seed = this . state . seed ;
444
454
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 ;
448
463
this . setState ( { saving : false , unsaved : false } ) ;
449
464
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
+ }
454
470
}
455
471
456
472
render ( props , state ) {
@@ -461,6 +477,8 @@ class Sketch extends Component {
461
477
) ,
462
478
h ( Editor , {
463
479
id : props . id ,
480
+ remoteSource : this . state . remoteSource ,
481
+ setRemoteSource : this . setRemoteSource . bind ( this ) ,
464
482
importError : state . importError ,
465
483
onImportError : this . onImportError . bind ( this ) ,
466
484
onSourceChanged : this . onSourceChanged . bind ( this ) ,
0 commit comments