Delete words from server if logged in
This commit is contained in:
parent
f6a6a8cc0b
commit
7c3724f22e
|
@ -0,0 +1 @@
|
||||||
|
export const DELETED_WORDS_LOCALSTORAGE_KEY = 'deletedWords';
|
|
@ -2,7 +2,15 @@ import '../../scss/Account/main.scss';
|
||||||
|
|
||||||
import { renderLoginForm } from "./render";
|
import { renderLoginForm } from "./render";
|
||||||
import { triggerLoginChanges } from './login';
|
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() {
|
export function showLoginForm() {
|
||||||
renderLoginForm();
|
renderLoginForm();
|
||||||
|
@ -27,4 +35,15 @@ export function uploadWord(word) {
|
||||||
|
|
||||||
export function syncImportedWords(words) {
|
export function syncImportedWords(words) {
|
||||||
uploadWords(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);
|
||||||
|
});
|
||||||
}
|
}
|
|
@ -4,6 +4,7 @@ import { request } from "./helpers";
|
||||||
import { saveToken } from "./utilities";
|
import { saveToken } from "./utilities";
|
||||||
import { renderAll } from "../render";
|
import { renderAll } from "../render";
|
||||||
import { sortWords } from "../wordManagement";
|
import { sortWords } from "../wordManagement";
|
||||||
|
import { getLocalDeletedWords, clearLocalDeletedWords, saveDeletedWordsLocally } from "./utilities";
|
||||||
|
|
||||||
/* Outline for syncing
|
/* Outline for syncing
|
||||||
login
|
login
|
||||||
|
@ -152,9 +153,36 @@ export function syncWords(remoteWords, deletedWords) {
|
||||||
});
|
});
|
||||||
const localWordsToUpload = words.filter(word => {
|
const localWordsToUpload = words.filter(word => {
|
||||||
// Find words that don't exist in remote words after clearing deleted words
|
// 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';
|
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 => {
|
remoteWords.forEach(remoteWord => {
|
||||||
const localWord = words.find(word => word.wordId === remoteWord.wordId);
|
const localWord = words.find(word => word.wordId === remoteWord.wordId);
|
||||||
|
@ -195,4 +223,19 @@ export function uploadWords(words) {
|
||||||
addMessage('Could not upload words');
|
addMessage('Could not upload words');
|
||||||
return false;
|
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;
|
||||||
|
});
|
||||||
}
|
}
|
|
@ -1,5 +1,38 @@
|
||||||
import { setCookie } from "../StackOverflow/cookie";
|
import { setCookie } from "../StackOverflow/cookie";
|
||||||
|
import { DELETED_WORDS_LOCALSTORAGE_KEY } from "./constants";
|
||||||
|
import { getTimestampInSeconds } from "../../helpers";
|
||||||
|
|
||||||
export function saveToken(token) {
|
export function saveToken(token) {
|
||||||
setCookie('token', token, 30);
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -105,6 +105,12 @@ export function deleteWord(wordId) {
|
||||||
window.currentDictionary.words.splice(wordIndex, 1);
|
window.currentDictionary.words.splice(wordIndex, 1);
|
||||||
addMessage('Word Deleted Successfully');
|
addMessage('Word Deleted Successfully');
|
||||||
sortWords(true);
|
sortWords(true);
|
||||||
|
|
||||||
|
if (hasToken()) {
|
||||||
|
import('./account/index.js').then(account => {
|
||||||
|
account.deleteWord(wordId);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue