mirror of
https://github.com/chinchang/web-maker.git
synced 2025-07-14 10:36:19 +02:00
port remoteItem code to itemService and some refactor
This commit is contained in:
@ -73,6 +73,9 @@
|
|||||||
<path d="M13,9V3.5L18.5,9M6,2C4.89,2 4,2.89 4,4V20A2,2 0 0,0 6,22H18A2,2 0 0,0 20,20V8L14,2H6Z" />
|
<path d="M13,9V3.5L18.5,9M6,2C4.89,2 4,2.89 4,4V20A2,2 0 0,0 6,22H18A2,2 0 0,0 20,20V8L14,2H6Z" />
|
||||||
</svg>Open
|
</svg>Open
|
||||||
</a>
|
</a>
|
||||||
|
<a>
|
||||||
|
<img width="20" src="https://www.gravatar.com/avatar/6be4e636e8aacd405bed5cd15124a875" class="main-header__avatar-img"/>
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="content-wrap flex flex-grow">
|
<div class="content-wrap flex flex-grow">
|
||||||
|
@ -8,36 +8,51 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
async getAllItems() {
|
async getAllItems() {
|
||||||
var db = await window.db.getDb();
|
var d = deferred();
|
||||||
|
var remoteDb = await window.db.getDb();
|
||||||
|
|
||||||
return db
|
remoteDb
|
||||||
.doc(`users/${window.user.uid}`)
|
.doc(`users/${window.user.uid}`)
|
||||||
.get()
|
.get()
|
||||||
.then(doc => {
|
.then(doc => {
|
||||||
return doc.data().items;
|
return doc.data().items;
|
||||||
})
|
})
|
||||||
.then(async itemIds => {
|
.then(itemIdsObj => {
|
||||||
|
var itemIds = Object.getOwnPropertyNames(itemIdsObj || {});
|
||||||
console.log('itemids', itemIds);
|
console.log('itemids', itemIds);
|
||||||
|
|
||||||
|
if (!itemIds.length) {
|
||||||
|
d.resolve([]);
|
||||||
|
}
|
||||||
|
|
||||||
var items = [];
|
var items = [];
|
||||||
for (var id in itemIds) {
|
for (let i = 0; i < itemIds.length; i++) {
|
||||||
var item = await this.getItem(id);
|
const id = itemIds[i];
|
||||||
|
utils.log('Starting to fetch item ', id);
|
||||||
|
this.getItem(id).then(item => {
|
||||||
items.push(item);
|
items.push(item);
|
||||||
|
// Check if we have all items now.
|
||||||
|
if (itemIds.length === items.length) {
|
||||||
|
d.resolve(items);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return items;
|
return items;
|
||||||
});
|
});
|
||||||
|
return d.promise;
|
||||||
},
|
},
|
||||||
|
|
||||||
async setUser() {
|
async setUser() {
|
||||||
var db = await window.db.getDb();
|
const remoteDb = await window.db.getDb();
|
||||||
return db.doc(`users/${window.user.uid}`).set({
|
return remoteDb.doc(`users/${window.user.uid}`).set({
|
||||||
items: {}
|
items: {}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
async setItem(id, item) {
|
async setItem(id, item) {
|
||||||
var db = await window.db.getDb();
|
var remoteDb = await window.db.getDb();
|
||||||
console.log(`Starting to save item ${id}`);
|
console.log(`Starting to save item ${id}`);
|
||||||
return db
|
return remoteDb
|
||||||
.collection('items')
|
.collection('items')
|
||||||
.doc(id)
|
.doc(id)
|
||||||
.set(item, {
|
.set(item, {
|
||||||
@ -49,9 +64,27 @@
|
|||||||
.catch(error => console.log(error));
|
.catch(error => console.log(error));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async removeItem(id) {
|
||||||
|
if (window.IS_EXTENSION) {
|
||||||
|
var d = deferred();
|
||||||
|
db.local.remove(id, d.resolve);
|
||||||
|
return d.promise;
|
||||||
|
}
|
||||||
|
const remoteDb = await window.db.getDb();
|
||||||
|
console.log(`Starting to save item ${id}`);
|
||||||
|
return remoteDb
|
||||||
|
.collection('items')
|
||||||
|
.doc(id)
|
||||||
|
.delete()
|
||||||
|
.then(arg => {
|
||||||
|
console.log('Document removed', arg);
|
||||||
|
})
|
||||||
|
.catch(error => console.log(error));
|
||||||
|
},
|
||||||
|
|
||||||
async setItemForUser(itemId) {
|
async setItemForUser(itemId) {
|
||||||
var db = await window.db.getDb();
|
var remoteDb = await window.db.getDb();
|
||||||
return db
|
return remoteDb
|
||||||
.collection('users')
|
.collection('users')
|
||||||
.doc(window.user.uid)
|
.doc(window.user.uid)
|
||||||
.update({
|
.update({
|
||||||
@ -61,6 +94,33 @@
|
|||||||
console.log(`Item ${itemId} set for user`, arg);
|
console.log(`Item ${itemId} set for user`, arg);
|
||||||
})
|
})
|
||||||
.catch(error => console.log(error));
|
.catch(error => console.log(error));
|
||||||
|
},
|
||||||
|
|
||||||
|
async unsetItemForUser(itemId) {
|
||||||
|
if (window.IS_EXTENSION) {
|
||||||
|
return window.db.local.get(
|
||||||
|
{
|
||||||
|
items: {}
|
||||||
|
},
|
||||||
|
function(result) {
|
||||||
|
delete result.items[itemId];
|
||||||
|
db.local.set({
|
||||||
|
items: result.items
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const remoteDb = await window.db.getDb();
|
||||||
|
return remoteDb
|
||||||
|
.collection('users')
|
||||||
|
.doc(window.user.uid)
|
||||||
|
.update({
|
||||||
|
[`items.${itemId}`]: firebase.firestore.FieldValue.delete()
|
||||||
|
})
|
||||||
|
.then(arg => {
|
||||||
|
console.log(`Item ${itemId} unset for user`, arg);
|
||||||
|
})
|
||||||
|
.catch(error => console.log(error));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
@ -483,6 +483,7 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
var items = [];
|
var items = [];
|
||||||
if (!window.IS_EXTENSION) {
|
if (!window.IS_EXTENSION) {
|
||||||
items = await itemService.getAllItems();
|
items = await itemService.getAllItems();
|
||||||
|
|
||||||
if (shouldSaveGlobally) {
|
if (shouldSaveGlobally) {
|
||||||
items.forEach(item => {
|
items.forEach(item => {
|
||||||
savedItems[item.id] = item;
|
savedItems[item.id] = item;
|
||||||
@ -524,6 +525,8 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
}
|
}
|
||||||
function setCurrentItem(item) {
|
function setCurrentItem(item) {
|
||||||
currentItem = item;
|
currentItem = item;
|
||||||
|
utils.log('Current Item set', item);
|
||||||
|
|
||||||
// Reset auto-saving flag
|
// Reset auto-saving flag
|
||||||
isAutoSavingEnabled = false;
|
isAutoSavingEnabled = false;
|
||||||
// Reset unsaved count, in UI also.
|
// Reset unsaved count, in UI also.
|
||||||
@ -588,26 +591,17 @@ globalConsoleContainerEl, externalLibrarySearchInput, keyboardShortcutsModal
|
|||||||
|
|
||||||
itemTile.remove();
|
itemTile.remove();
|
||||||
// Remove from items list
|
// Remove from items list
|
||||||
db.local.get(
|
itemService.unsetItemForUser(itemId);
|
||||||
{
|
|
||||||
items: {}
|
|
||||||
},
|
|
||||||
function(result) {
|
|
||||||
delete result.items[itemId];
|
|
||||||
db.local.set({
|
|
||||||
items: result.items
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Remove individual item too.
|
// Remove individual item too.
|
||||||
db.local.remove(itemId, function() {
|
itemService.removeItem(itemId).then(() => {
|
||||||
alertsService.add('Item removed.');
|
alertsService.add('Item removed.');
|
||||||
// This item is open in the editor. Lets open a new one.
|
// This item is open in the editor. Lets open a new one.
|
||||||
if (currentItem.id === itemId) {
|
if (currentItem.id === itemId) {
|
||||||
createNewItem();
|
createNewItem();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Remove from cached list
|
// Remove from cached list
|
||||||
delete savedItems[itemId];
|
delete savedItems[itemId];
|
||||||
|
|
||||||
|
@ -348,6 +348,9 @@ body > #demo-frame {
|
|||||||
.main-header__btn-wrap > a:hover {
|
.main-header__btn-wrap > a:hover {
|
||||||
border-color: rgba(146, 151, 179, 0.5);
|
border-color: rgba(146, 151, 179, 0.5);
|
||||||
}
|
}
|
||||||
|
.main-header__avatar-img {
|
||||||
|
border-radius: 50%;
|
||||||
|
}
|
||||||
.logo {
|
.logo {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
height: 25px;
|
height: 25px;
|
||||||
|
Reference in New Issue
Block a user