diff --git a/src/Helpers.js b/src/Helpers.js index 958b33c..4d813eb 100644 --- a/src/Helpers.js +++ b/src/Helpers.js @@ -109,6 +109,10 @@ export function characterIsUppercase (character) { return character === character.toUpperCase(); } +export function timestampInSeconds () { + return Math.round(Date.now() / 1000); +} + export function getWordsStats (words, partsOfSpeech, isCaseSensitive = false) { const wordStats = { numberOfWords: [ diff --git a/src/managers/DictionaryData.js b/src/managers/DictionaryData.js index f56daba..334834b 100644 --- a/src/managers/DictionaryData.js +++ b/src/managers/DictionaryData.js @@ -2,6 +2,7 @@ import assert from 'assert'; import store from 'store'; import wordDb from './WordDatabase'; import idManager from './IDManager'; +import { timestampInSeconds } from '../Helpers'; const defaultDictionary = { name: 'New', @@ -41,6 +42,8 @@ const defaultDictionary = { isComplete: false, isPublic: false, }, + lastUpdated: null, + createdOn: timestampInSeconds(), }; class DictionaryData { @@ -324,6 +327,18 @@ class DictionaryData { return store.set('Lexiconga', updatedValues); } + get lastUpdated () { + return store.get('Lexiconga').lastUpdated + || defaultDictionary.lastUpdated; + } + + set lastUpdated (value) { + assert(typeof value === 'number', 'lastUpdated must be passed as a number.'); + const updatedValues = store.get('Lexiconga'); + updatedValues.lastUpdated = value; + return store.set('Lexiconga', updatedValues); + } + get wordsPromise () { if (this.sortByDefinition) { return wordDb.words.toCollection().sortBy('definition'); diff --git a/src/managers/Updater.js b/src/managers/Updater.js index a316e2f..324187f 100644 --- a/src/managers/Updater.js +++ b/src/managers/Updater.js @@ -1,3 +1,5 @@ +import { timestampInSeconds } from "../Helpers"; + export class Updater { constructor (appWithDictionaryState, dictionary) { this.app = appWithDictionaryState; @@ -64,6 +66,8 @@ export class Updater { if (updatedDetails.isEmpty()) { reject('No dictionary details have changed.'); } else { + this.dictionary.lastUpdated = timestampInSeconds(); + updatedDetails.lastUpdated = this.dictionary.lastUpdated; this.app.setState(updatedDetails, () => { if (updatedDetails.hasOwnProperty('settings')) { this.app.updateDisplayedWords(); diff --git a/src/managers/Word.js b/src/managers/Word.js index 90e55a7..c72fe55 100644 --- a/src/managers/Word.js +++ b/src/managers/Word.js @@ -1,6 +1,7 @@ import PropTypes from 'prop-types'; import store from 'store'; import wordDb from './WordDatabase'; +import {timestampInSeconds} from '../Helpers'; export class Word { constructor (values = {}) { @@ -11,8 +12,8 @@ export class Word { partOfSpeech: PropTypes.string, definition: PropTypes.string, details: PropTypes.string, - createdTime: PropTypes.number, - modifiedTime: PropTypes.number, + createdOn: PropTypes.number, + lastUpdated: PropTypes.number, }, values, 'value', 'Word'); const { @@ -22,8 +23,8 @@ export class Word { partOfSpeech = '', definition = '', details = '', - createdTime = null, - modifiedTime = null, + createdOn = null, + lastUpdated = null, } = values; this.name = name; @@ -31,16 +32,15 @@ export class Word { this.partOfSpeech = partOfSpeech; this.definition = definition; this.details = details; - this.createdTime = createdTime; - this.modifiedTime = modifiedTime; + this.createdOn = createdOn; + this.lastUpdated = lastUpdated; // Only create an id property if an ID exists. if (id) this.id = id; } create () { - const timestampInSeconds = Math.round(Date.now() / 1000); - this.createdTime = timestampInSeconds; + this.createdOn = timestampInSeconds(); // Delete id if it exists to allow creation of new word. if (this.hasOwnProperty('id')) delete this.id; @@ -56,8 +56,7 @@ export class Word { } update () { - const timestampInSeconds = Math.round(Date.now() / 1000); - this.modifiedTime = timestampInSeconds; + this.lastUpdated = timestampInSeconds(); return wordDb.words.put(this) .then((id) => { diff --git a/src/managers/WordDatabase.js b/src/managers/WordDatabase.js index b88eb0f..047b611 100644 --- a/src/managers/WordDatabase.js +++ b/src/managers/WordDatabase.js @@ -4,7 +4,7 @@ import {Word} from './Word'; const db = new Dexie('Lexiconga'); db.version(1).stores({ - words: '++id, name, partOfSpeech, createdTime, modifiedTime', + words: '++id, name, partOfSpeech, createdOn, lastUpdated', }); if (['emptydb', 'donotsave'].includes(process.env.NODE_ENV)) {