Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
ensure autosave entries are deleted on restore and resource save
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo Gomes committed Nov 22, 2019
1 parent cfe4336 commit 8ae0857
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
$app->on('singleton.saveData.after', function($singleton, $data) use($app) {
$settings = $this->retrieve('config/autosave', ['singletons' => []]);
if ($settings['singletons'] === '*' || in_array($singleton, $settings['singletons'])) {
if ($settings['singletons'] === '*' || in_array($singleton['name'], $settings['singletons'])) {
$app->module('autosave')->removeEntry($singleton['_id']);
}
});
20 changes: 19 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@
$this->module('autosave')->extend([
'get' => function($id) {
$data = $this->app->storage->findOne('cockpit/autosave', ['_oid' => $id]);
if (!$data) {
$type = $data['_type'] ?? NULL;
$resource = NULL;
if (!$data || !$data['_oid']) {
return;
}

if ($type === 'collection') {
$resource = $this->app->storage->findOne("collections/{$data['_name']}", ['_id' => $data['_oid']]);
if (!$resource || $resource['_modified'] === $data['data']['_modified']) {
$this->app->storage->remove('cockpit/autosave', ['_oid' => $id]);
return;
}
}

$user = ['name' => 'N/A', 'email' => 'N/A', 'group' => 'N/A'];
if ($data['_creator'] && $account = $this->app->storage->findOne('cockpit/accounts', ['_id' => $data['_creator']])) {
$user['name'] = $account['name'] ?? $account['user'];
Expand All @@ -26,14 +36,22 @@
'save' => function($autosaved) {
$id = $autosaved['id'];
$data = $autosaved['data'];
$type = $autosaved['type'];
$name = $autosaved['name'];

$user = $this->app->module('cockpit')->getUser();

$existing = $this->app->storage->findOne('cockpit/autosave', ['_oid' => $id]);

if ($type === 'collection') {
$data['_modified'] = time();
}

$entry = [
'_oid' => $id,
'data' => $data,
'_type' => $type,
'_name' => $name,
'_creator' => $user['_id'] ?? NULL,
'_modified' => time()
];
Expand Down
22 changes: 18 additions & 4 deletions views/partials/autosave-collection-aside.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
var $this = this;
this.isReady = false;
this.isUpdating = false;
this.autosaved = false;
this.autosaved = null;
this.autosavetime = null;

this.on('mount', function() {
Expand All @@ -14,10 +14,14 @@
}
this._entry = JSON.parse(JSON.stringify(this.entry));
$this.isReady = true;
App.$(this.root).on('submit', function(e) {
$this.autosaved = false;
$this.autosavetime = null;
});
});

this.on('bindingupdated', function(data) {
if (this.isReady && this.entry['_id'] && !this.isUpdating && !_.isEqual(this.entry, this._entry)) {
if (this.isReady && this.entry['_id'] && !this.isUpdating) {
window.setTimeout(this.autosave, 2000);
$this.isUpdating = true;
$this.update();
Expand All @@ -26,7 +30,11 @@

this.get = function() {
App.callmodule('autosave:get', this.entry._id, 'access').then(function(data) {
$this.autosaved = data.result || null;
if (data.result && data.result.data && data.result.data._modified !== $this.entry._modified) {
$this.autosaved = data.result;
} else {
$this.autosaved = null;
}
$this.isUpdating = false;
$this.update();
}).catch(function(e){
Expand All @@ -35,7 +43,13 @@
};

this.autosave = function() {
App.callmodule('autosave:save', {id: $this.entry._id, data: $this.entry }, 'access').then(function(data) {
var autosaveData = {
id: $this.entry._id,
data: $this.entry,
type: 'collection',
name: $this.collection._id
}
App.callmodule('autosave:save', autosaveData, 'access').then(function(data) {
$this.autosavetime = data.result.updated || null;
$this._entry = JSON.parse(JSON.stringify($this.entry));
$this.isUpdating = false;
Expand Down
16 changes: 13 additions & 3 deletions views/partials/autosave-singleton-aside.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@
$this.get();
$this._data = JSON.parse(JSON.stringify(this.data));
$this.isReady = true;
App.$(this.root).on('submit', function(e) {
$this.autosaved = false;
$this.autosavetime = null;
});
});
this.on('bindingupdated', function(data) {
if (this.isReady && !this.isUpdating && !_.isEqual(this.data, this._data)) {
if (this.isReady && !this.isUpdating) {
window.setTimeout(this.autosave, 2000);
$this.isUpdating = true;
$this.update();
}
});
this.get = function() {
App.callmodule('autosave:get', this.singleton._id, 'access').then(function(data) {
App.callmodule('autosave:get', this.singleton._id, 'access').then(function(data) {
$this.autosaved = data.result || null;
$this.isUpdating = false;
$this.update();
Expand All @@ -33,7 +37,13 @@
};
this.autosave = function() {
App.callmodule('autosave:save', {id: $this.singleton._id, data: $this.data }, 'access').then(function(data) {
var autosaveData = {
id: $this.singleton._id,
data: $this.data,
type: 'singleton',
name: $this.singleton.name
}
App.callmodule('autosave:save', autosaveData, 'access').then(function(data) {
$this.autosavetime = data.result.updated || null;
$this._data = JSON.parse(JSON.stringify($this.data));
$this.isUpdating = false;
Expand Down
1 change: 1 addition & 0 deletions views/partials/restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
tinyMCE.get(id).setContent(content);
}
});
App.callmodule('autosave:remove', $this.autosaved.id, 'access');
$this.autosaved = null;
$this.update();
App.ui.notify('Autosaved resource restored with success', 'success');
Expand Down

0 comments on commit 8ae0857

Please sign in to comment.