Delete words from server if logged in

This commit is contained in:
Robbie Antenesse 2019-05-23 15:07:10 -06:00
parent f6a6a8cc0b
commit 7c3724f22e
5 changed files with 105 additions and 3 deletions

View File

@ -0,0 +1 @@
export const DELETED_WORDS_LOCALSTORAGE_KEY = 'deletedWords';

View File

@ -2,7 +2,15 @@ import '../../scss/Account/main.scss';
import { renderLoginForm } from "./render";
import { triggerLoginChanges } from './login';
import { syncDictionary, uploadWords, uploadDetails, uploadWholeDictionary } from './sync';
import {
syncDictionary,
uploadWords,
uploadDetails,
uploadWholeDictionary,
deleteWords
} from './sync';
import { saveDeletedWordLocally } from './utilities';
import { addMessage } from '../utilities';
export function showLoginForm() {
renderLoginForm();
@ -27,4 +35,15 @@ export function uploadWord(word) {
export function syncImportedWords(words) {
uploadWords(words);
}
export function deleteWord(wordId) {
deleteWords([wordId]).catch(err => {
console.error(err);
saveDeletedWordLocally(wordId);
addMessage('Could not connect. Trying again in 10 seconds.');
setTimeout(() => {
deleteWord(wordId);
}, 10000);
});
}

View File

@ -4,6 +4,7 @@ import { request } from "./helpers";
import { saveToken } from "./utilities";
import { renderAll } from "../render";
import { sortWords } from "../wordManagement";
import { getLocalDeletedWords, clearLocalDeletedWords, saveDeletedWordsLocally } from "./utilities";
/* Outline for syncing
login
@ -152,9 +153,36 @@ export function syncWords(remoteWords, deletedWords) {
});
const localWordsToUpload = words.filter(word => {
// Find words that don't exist in remote words after clearing deleted words
const remote = remoteWords.find(remoteWord => remoteWord.id === word.wordId);
const remote = remoteWords.find(remoteWord => remoteWord.wordId === word.wordId);
return typeof remote === 'undefined';
});
const localDeletedWords = getLocalDeletedWords();
if (localDeletedWords.length > 0) {
const deletedWordIds = [];
remoteWords = remoteWords.filter(remoteWord => {
const deleted = deletedWords.find(deletedWord => deletedWord.id === remoteWord.wordId);
if (deleted) {
if (deleted.deletedOn > remoteWord.createdOn) {
deletedWordIds.push(deleted.id);
return false;
}
}
return true;
});
let deletePromise;
if (deletedWordIds.length > 0) {
deletePromise = deleteWords(deletedWordIds);
} else {
deletePromise = Promise.resolve(true);
}
deletePromise.then(success => {
if (success) {
clearLocalDeletedWords();
}
});
}
remoteWords.forEach(remoteWord => {
const localWord = words.find(word => word.wordId === remoteWord.wordId);
@ -195,4 +223,19 @@ export function uploadWords(words) {
addMessage('Could not upload words');
return false;
});
}
export function deleteWords(wordIds) {
return request({
action: 'delete-words',
wordIds,
}, successful => {
addMessage('Deleted from Server');
return successful;
}, error => {
console.error(error);
addMessage('Could not delete words');
saveDeletedWordsLocally(wordIds);
return false;
});
}

View File

@ -1,5 +1,38 @@
import { setCookie } from "../StackOverflow/cookie";
import { DELETED_WORDS_LOCALSTORAGE_KEY } from "./constants";
import { getTimestampInSeconds } from "../../helpers";
export function saveToken(token) {
setCookie('token', token, 30);
}
}
export function saveDeletedWordsLocally(wordIds) {
let storedDeletedWords = getLocalDeletedWords();
wordIds.forEach(wordId => {
if (storedDeletedWords.findIndex(stored => stored.id === wordId) < 0) {
storedDeletedWords.push({
id: wordId,
deletedOn: getTimestampInSeconds(),
});
}
});
window.localStorage.setItem(DELETED_WORDS_LOCALSTORAGE_KEY, JSON.stringify(storedDeletedWords));
}
export function saveDeletedWordLocally(wordId) {
saveDeletedWordsLocally([wordId]);
}
export function getLocalDeletedWords() {
let storedDeletedWords = window.localStorage.getItem(DELETED_WORDS_LOCALSTORAGE_KEY);
if (!storedDeletedWords) {
storedDeletedWords = [];
} else {
storedDeletedWords = JSON.parse(storedDeletedWords);
}
return storedDeletedWords;
}
export function clearLocalDeletedWords() {
window.localStorage.removeItem(DELETED_WORDS_LOCALSTORAGE_KEY);
}

View File

@ -105,6 +105,12 @@ export function deleteWord(wordId) {
window.currentDictionary.words.splice(wordIndex, 1);
addMessage('Word Deleted Successfully');
sortWords(true);
if (hasToken()) {
import('./account/index.js').then(account => {
account.deleteWord(wordId);
});
}
}
}