mirror of
https://github.com/chinchang/web-maker.git
synced 2025-07-23 23:11:12 +02:00
Merge pull request #567 from chinchang/add-import-fallback
try saving with refreshed ids when importing to DB fails
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { deferred } from './deferred';
|
import { deferred } from './deferred';
|
||||||
import { log } from './utils';
|
import { log, refreshItemIds } from './utils';
|
||||||
import {
|
import {
|
||||||
collection,
|
collection,
|
||||||
deleteField,
|
deleteField,
|
||||||
@@ -170,21 +170,49 @@ export const itemService = {
|
|||||||
} else {
|
} else {
|
||||||
const remoteDb = window.db.getDb();
|
const remoteDb = window.db.getDb();
|
||||||
|
|
||||||
const batch = writeBatch(remoteDb);
|
function save(items) {
|
||||||
/* eslint-disable guard-for-in */
|
const batch = writeBatch(remoteDb);
|
||||||
for (var id in items) {
|
/* eslint-disable guard-for-in */
|
||||||
items[id].createdBy = window.user.uid;
|
for (var id in items) {
|
||||||
batch.set(doc(remoteDb, `items/${id}`), items[id]);
|
items[id].createdBy = window.user.uid;
|
||||||
batch.update(doc(remoteDb, `users/${window.user.uid}`), {
|
batch.set(doc(remoteDb, `items/${id}`), items[id]);
|
||||||
[`items.${id}`]: true
|
batch.update(doc(remoteDb, `users/${window.user.uid}`), {
|
||||||
});
|
[`items.${id}`]: true
|
||||||
|
});
|
||||||
// Set these items on our cached user object too
|
}
|
||||||
window.user.items = window.user.items || {};
|
return batch.commit();
|
||||||
window.user.items[id] = true;
|
/* eslint-enable guard-for-in */
|
||||||
}
|
}
|
||||||
batch.commit().then(d.resolve);
|
|
||||||
/* eslint-enable guard-for-in */
|
function onSuccess(items) {
|
||||||
|
window.user.items = window.user.items || {};
|
||||||
|
for (var id in items) {
|
||||||
|
// Set these items on our cached user object too
|
||||||
|
window.user.items[id] = true;
|
||||||
|
}
|
||||||
|
d.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
save(items)
|
||||||
|
.then(() => {
|
||||||
|
onSuccess(items);
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
// The only reason we know for this failing is the same creations were
|
||||||
|
// imported in other account. And hence they can't again be saved in the
|
||||||
|
// DB with same ID and different `createdBy`
|
||||||
|
// So now we save them with different IDs
|
||||||
|
log('Saving imported items failed. Trying with refreshed IDs now...');
|
||||||
|
const refreshedItems = refreshItemIds(items);
|
||||||
|
save(refreshedItems)
|
||||||
|
.then(() => {
|
||||||
|
onSuccess(refreshedItems);
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
log('Error saving items', e);
|
||||||
|
alert('Your items could not be saved. Please try again later.');
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return d.promise;
|
return d.promise;
|
||||||
},
|
},
|
||||||
|
13
src/utils.js
13
src/utils.js
@@ -672,3 +672,16 @@ export function persistAuthUserLocally(user) {
|
|||||||
keys.map(key => (obj[key] = user[key]));
|
keys.map(key => (obj[key] = user[key]));
|
||||||
window.localStorage.setItem('user', JSON.stringify(obj));
|
window.localStorage.setItem('user', JSON.stringify(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* items is an object of {itemId: item}. This fn changes all the keys to new item IDs
|
||||||
|
* */
|
||||||
|
export function refreshItemIds(items) {
|
||||||
|
const newItems = {};
|
||||||
|
for (var id in items) {
|
||||||
|
const newId = generateRandomId();
|
||||||
|
items[id].id = newId;
|
||||||
|
newItems[newId] = items[id];
|
||||||
|
}
|
||||||
|
return newItems;
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user