diff --git a/src/Helper.js b/src/Helper.js index 54372f4..d4a3dee 100644 --- a/src/Helper.js +++ b/src/Helper.js @@ -15,6 +15,38 @@ class Helper { characterIsUppercase (character) { return character === character.toUpperCase(); } + + arraysAreEqual (array1, array2) { + // if the other array2 is a falsy value, return + if (!array1 || !array2) + return false; + + // compare lengths - can save a lot of time + if (array1.length != array2.length) + return false; + + for (var i = 0, l=array1.length; i < l; i++) { + // Check if we have nested arrays + if (array1[i] instanceof Array && array2[i] instanceof Array) { + // recurse into the nested arrays + if (!array1[i].equals(array2[i])) + return false; + } + else if (array1[i] != array2[i]) { + // Warning - two different object instances will never be equal: {x:20} != {x:20} + return false; + } + } + return true; + } + + objectIsEmpty (obj) { + for(var key in obj) { + if(obj.hasOwnProperty(key)) + return false; + } + return true; + } } export default new Helper; diff --git a/src/Updater.js b/src/Updater.js new file mode 100644 index 0000000..daf0ce1 --- /dev/null +++ b/src/Updater.js @@ -0,0 +1,58 @@ +import helper from './Helper'; + +export class Updater { + constructor (appWithDictionaryState, dictionary) { + this.app = appWithDictionaryState; + this.dictionary = dictionary; + } + + setDictionaryName (newName) { + this.app.setState({ + name: newName, + }, () => { + this.dictionary.name = newName; + }); + } + + setDictionarySpecification (newSpecification) { + this.app.setState({ + specification: newSpecification, + }, () => { + this.dictionary.specification = newSpecification; + }); + } + + updateDictionaryDetails (dicitonaryDetails = {}) { + return new Promise((resolve, reject) => { + const updatedDetails = {}; + + if (dicitonaryDetails.name) { + updatedDetails['name'] = dicitonaryDetails.name; + this.dictionary.name = dicitonaryDetails.name; + } + + if (dicitonaryDetails.specification) { + updatedDetails['specification'] = dicitonaryDetails.specification; + this.dicitonary.specification = dicitonaryDetails.specification; + } + + if (dicitonaryDetails.description) { + updatedDetails['description'] = dicitonaryDetails.description; + this.dictionary.description = dicitonaryDetails.description; + } + + if (dicitonaryDetails.partsOfSpeech) { + updatedDetails['partsOfSpeech'] = dicitonaryDetails.partsOfSpeech; + this.dictionary.partsOfSpeech = dicitonaryDetails.partsOfSpeech; + } + + if (helper.objectIsEmpty(updatedDetails)) { + reject('No dictionary details have changed.'); + } else { + this.app.setState(updatedDetails, () => { + resolve(); + }); + } + }); + } +} \ No newline at end of file diff --git a/src/components/MainDisplay.jsx b/src/components/MainDisplay.jsx index 90d8f18..03a524d 100644 --- a/src/components/MainDisplay.jsx +++ b/src/components/MainDisplay.jsx @@ -7,7 +7,7 @@ import { WordForm } from './management/WordForm'; import { DictionaryDetails } from './display/DictionaryDetails'; import { WordsList } from './display/WordsList'; -export const MainDisplay = ({ dictionaryInfo, wordsToDisplay, updateDisplay, lastRender }) => { +export const MainDisplay = ({ dictionaryInfo, wordsToDisplay, updateDisplay, updater, lastRender }) => { return (
@@ -22,9 +22,11 @@ export const MainDisplay = ({ dictionaryInfo, wordsToDisplay, updateDisplay, las { + return ( +
+
+ +
+ { + editDictionaryModal.setState({ + name: event.target.value, + hasChanged: event.target.value != editDictionaryModal.props.name, + }); + }} + /> +
+
+ +
+ +
+ { + editDictionaryModal.setState({ + specification: event.target.value, + hasChanged: event.target.value != editDictionaryModal.props.specification, + }); + }} + /> +
+
+ +
+ +
+