From b6f08e58fae35d5dd8a610af31c2950b37746695 Mon Sep 17 00:00:00 2001 From: cedricnjanga Date: Fri, 6 Oct 2017 10:17:17 +0200 Subject: Add webpacker gem and migrate the React apps --- app/javascript/helpers/clone.js | 12 ++++++++++++ app/javascript/helpers/date_filters.js | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 app/javascript/helpers/clone.js create mode 100644 app/javascript/helpers/date_filters.js (limited to 'app/javascript/helpers') diff --git a/app/javascript/helpers/clone.js b/app/javascript/helpers/clone.js new file mode 100644 index 000000000..00127e2b1 --- /dev/null +++ b/app/javascript/helpers/clone.js @@ -0,0 +1,12 @@ +import _ from 'lodash' + +/* This function helps having a bit more security when we pass data from the backend to the React parts + It clones the obj (window variable) and then conditionnaly delete the window variable +*/ + +export default function clone(window, key, deletable = false) { + let obj = _.cloneDeep(window[key]) + + if (deletable) delete window[key] + return obj +} \ No newline at end of file diff --git a/app/javascript/helpers/date_filters.js b/app/javascript/helpers/date_filters.js new file mode 100644 index 000000000..621e163ad --- /dev/null +++ b/app/javascript/helpers/date_filters.js @@ -0,0 +1,35 @@ +export default function DateFilter(buttonId, message, ...inputIds) { + this.buttonId = buttonId + this.inputIds = inputIds + this.message = message + + const getVal = (str, key) => { + let newStr = str.replace(/NUM/, key) + return $(newStr).val() + } + + const getDates = () => { + return this.inputIds.reduce((arr, id) => { + let newIds = [1, 2, 3].map(key => getVal(id, key)) + arr.push(...newIds) + return arr + },[]) + } + + const allInputFilled = () => { + return getDates().every(date => !!date) + } + + const noInputFilled = () => { + return getDates().every(date => !date) + } + + const button = document.getElementById(this.buttonId) + + button && button.addEventListener('click', (event) => { + if (!allInputFilled() && !noInputFilled()) { + event.preventDefault() + alert(this.message) + } + }) +} \ No newline at end of file -- cgit v1.2.3 From 08b002abfb101b1edce9750231c989591e049bf7 Mon Sep 17 00:00:00 2001 From: cedricnjanga Date: Mon, 9 Oct 2017 17:14:27 +0200 Subject: Working standard webpacker config --- app/javascript/helpers/date_filters.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'app/javascript/helpers') diff --git a/app/javascript/helpers/date_filters.js b/app/javascript/helpers/date_filters.js index 621e163ad..53c9da641 100644 --- a/app/javascript/helpers/date_filters.js +++ b/app/javascript/helpers/date_filters.js @@ -1,11 +1,11 @@ export default function DateFilter(buttonId, message, ...inputIds) { - this.buttonId = buttonId + this.button = document.getElementById(buttonId) this.inputIds = inputIds this.message = message const getVal = (str, key) => { let newStr = str.replace(/NUM/, key) - return $(newStr).val() + return document.getElementById(newStr).value } const getDates = () => { @@ -13,20 +13,14 @@ export default function DateFilter(buttonId, message, ...inputIds) { let newIds = [1, 2, 3].map(key => getVal(id, key)) arr.push(...newIds) return arr - },[]) + }, []) } - const allInputFilled = () => { - return getDates().every(date => !!date) - } - - const noInputFilled = () => { - return getDates().every(date => !date) - } - - const button = document.getElementById(this.buttonId) + const allInputFilled = () => getDates().every(date => !!date) - button && button.addEventListener('click', (event) => { + const noInputFilled = () => getDates().every(date => !date) + + this.button && this.button.addEventListener('click', (event) => { if (!allInputFilled() && !noInputFilled()) { event.preventDefault() alert(this.message) -- cgit v1.2.3