Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
add local stroge backup
Browse files Browse the repository at this point in the history
* Fixes #120. Saves current version of data before window/tab is
closed by the its url from where it was fetched earlier. And check
before fetching a data if it exists on local stroge. Finally clear the
data if it is save successfully.

* Refactors api.service.
  • Loading branch information
harunurhan committed Mar 24, 2017
1 parent 5e9acc2 commit 50ce6d1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 24 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"font-awesome": "^4.7.0",
"lodash": "4.17.2",
"ng2-json-editor": "~0.6.0",
"ng2-webstorage": "^1.5.1",
"rxjs": "^5.0.1",
"ts-helpers": "^1.1.1",
"zone.js": "^0.7.2"
Expand Down
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { APP_BASE_HREF } from '@angular/common';
import { HttpModule } from '@angular/http';

import { JsonEditorModule } from 'ng2-json-editor';
import { Ng2Webstorage } from 'ng2-webstorage';

import { AppComponent } from './app.component';
import { EditorHoldingPenComponent } from './editor-holdingpen';
Expand Down Expand Up @@ -31,7 +32,8 @@ import { AppConfigService } from './app-config.service';
BrowserModule,
HttpModule,
routing,
JsonEditorModule
JsonEditorModule,
Ng2Webstorage
],
providers: [
{ provide: APP_BASE_HREF, useValue: '/editor' },
Expand Down
7 changes: 5 additions & 2 deletions src/app/editor-container/editor-container.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { ApiService } from '../shared/services';

import { AppConfigService } from '../app-config.service';

@Component({
Expand All @@ -47,7 +46,11 @@ export class EditorContainerComponent implements OnInit {
.subscribe(params => {
this.apiService.fetchRecord(params['type'], params['recid'])
.then(record => {
this.record = record['metadata'];
// load record.metadata or record (if it is from local storage)
this.record = record['metadata'] || record;
window.onbeforeunload = () => {
this.apiService.saveDataLocally(this.record);
};
this.config = this.appConfig.getConfigForRecord(this.record);
return this.apiService.fetchUrl(this.record['$schema']);
}).then(schema => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class EditorHoldingPenToolbarSaveComponent {
constructor(private apiService: ApiService) { }

onClickSave(event: Object) {
this.apiService.saveWorkflowObject(this.workflowObject)
this.apiService.saveData(this.workflowObject)
.subscribe(resp => {
window.location.href = `/holdingpen/${this.workflowObject.id}`;
});
Expand Down
7 changes: 6 additions & 1 deletion src/app/editor-holdingpen/editor-holdingpen.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { ApiService } from '../shared/services';
import 'rxjs/add/operator/mergeMap';

import { ApiService } from '../shared/services';
import { AppConfigService } from '../app-config.service';

@Component({
Expand All @@ -49,12 +49,17 @@ export class EditorHoldingPenComponent implements OnInit {
this.apiService.fetchWorkflowObject(params['objectid'])
.then(workflowObject => {
this.workflowObject = workflowObject;
window.onbeforeunload = () => {
this.apiService.saveDataLocally(this.workflowObject);
};
this.config = this.appConfig.getConfigForRecord(this.workflowObject['metadata']);
return this.apiService.fetchUrl(this.workflowObject['metadata']['$schema']);
}).then(schema => {
this.schema = schema;

}).catch(error => console.error(error));
});

}

onRecordChange(record: Object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class EditorToolbarSaveComponent {
bodyHtml: this.domSanitizer.bypassSecurityTrustHtml('<iframe id="iframe-preview"></iframe>'),
type: 'confirm',
onConfirm: () => {
this.apiService.saveRecord(this.record).subscribe({
this.apiService.saveData(this.record).subscribe({
next: () => {
this.route.params
.subscribe(params => {
Expand Down
39 changes: 21 additions & 18 deletions src/app/shared/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,51 @@

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';

import { AppConfigService } from '../../app-config.service';
import { LocalStorageService } from 'ng2-webstorage';

@Injectable()
export class ApiService {
// pid_type and pid_value (see invenio-pidstore)
private pidType: string;
private pidValue: string;
// workflow object id (see invenio-workflows)
private objectId: string;

constructor(private http: Http, private config: AppConfigService) {
}
private dataUrl: string;

constructor(private http: Http,
private config: AppConfigService,
private localStorageService: LocalStorageService) { }

fetchUrl(url: string): Promise<Object> {
return this.http.get(url)
.map(res => res.json())
.toPromise();
}

private fetchFromUrlOrLocalStorage(url: string): Promise<Object> {
this.dataUrl = url;
let localCopy = this.localStorageService.retrieve(url);
return localCopy ? Promise.resolve(localCopy) : this.fetchUrl(url);
}

fetchRecord(pidType: string, pidValue: string): Promise<Object> {
this.pidType = pidType;
this.pidValue = pidValue;
return this.fetchUrl(this.config.apiUrl(pidType, pidValue));
return this.fetchFromUrlOrLocalStorage(this.config.apiUrl(pidType, pidValue));
}

fetchWorkflowObject(objectId: string): Promise<Object> {
this.objectId = objectId;
return this.fetchUrl(this.config.holdingPenApiUrl(this.objectId));
return this.fetchFromUrlOrLocalStorage(this.config.holdingPenApiUrl(objectId));
}

saveRecord(record: Object): Observable<Object> {
saveData(data: Object): Observable<Object> {
this.localStorageService.clear(this.dataUrl);
return this.http
.put(this.config.apiUrl(this.pidType, this.pidValue), record)
.put(this.dataUrl, data)
.map(res => res.json());
}

saveWorkflowObject(record: Object): Observable<Object> {
return this.http
.put(this.config.holdingPenApiUrl(this.objectId), record)
.map(res => res.json());
saveDataLocally(data: Object) {
this.localStorageService.store(this.dataUrl, data);
}
}

0 comments on commit 50ce6d1

Please sign in to comment.