Created
August 20, 2018 07:30
-
-
Save alukos/15cff6f437ce2bcdaaa18b4f9458e1dd to your computer and use it in GitHub Desktop.
Revisions
-
alukos created this gist
Aug 20, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ movePage({ getters, commit }, { name, pageNum, direction }) { return new Promise(async (resolve, reject) => { try { if (pageNum == 1 && direction == 'left') { reject('Error cant move first page to the left'); } const totalPages = getters.pageCount(name); if (pageNum >= totalPages && direction == 'right') { reject('Error cant move last page to the right'); } const newPageNum = direction == 'left' ? pageNum - 1 : pageNum + 1; const db = fb.firestore(); const pageUid = getters.pageItem(name, pageNum).uid; const newPageUid = getters.pageItem(name, newPageNum).uid; const pageRef = db.doc(`pages/${pageUid}`); const newPageRef = db.doc(`pages/${newPageUid}`); await db.runTransaction(async transaction => { const pageDoc = await transaction.get(pageRef); const newPageDoc = await transaction.get(newPageRef); const position = pageDoc.get('position'); const newPosition = newPageDoc.get('position'); await transaction.update(pageRef, { position: newPosition }); await transaction.update(newPageRef, { position }); }); commit('exchangePosition', { name, pageNum, newPageNum }); resolve(newPageNum); } catch (error) { console.error('Error move page: ', error); reject(error); } }); },