aboutsummaryrefslogtreecommitdiffstats
path: root/app/javascript/helpers
diff options
context:
space:
mode:
author0Napster2017-10-11 12:03:04 +0200
committerGitHub2017-10-11 12:03:04 +0200
commitbf6d0e38ac7dfd7f99d67169e665384edd047ec0 (patch)
treee08a253483cc17a4ef5623e72e9df084f2217a5e /app/javascript/helpers
parent98861e13d56403e75aa000d62880dcbc34987a37 (diff)
parent8993eced364551163879dc1735ad7d583fc664ac (diff)
downloadchouette-core-bf6d0e38ac7dfd7f99d67169e665384edd047ec0.tar.bz2
Merge pull request #86 from af83/webpacker
Add webpacker gem and migrate the React apps
Diffstat (limited to 'app/javascript/helpers')
-rw-r--r--app/javascript/helpers/clone.js12
-rw-r--r--app/javascript/helpers/date_filters.js29
2 files changed, 41 insertions, 0 deletions
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..53c9da641
--- /dev/null
+++ b/app/javascript/helpers/date_filters.js
@@ -0,0 +1,29 @@
+export default function DateFilter(buttonId, message, ...inputIds) {
+ this.button = document.getElementById(buttonId)
+ this.inputIds = inputIds
+ this.message = message
+
+ const getVal = (str, key) => {
+ let newStr = str.replace(/NUM/, key)
+ return document.getElementById(newStr).value
+ }
+
+ 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 = () => getDates().every(date => !!date)
+
+ const noInputFilled = () => getDates().every(date => !date)
+
+ this.button && this.button.addEventListener('click', (event) => {
+ if (!allInputFilled() && !noInputFilled()) {
+ event.preventDefault()
+ alert(this.message)
+ }
+ })
+} \ No newline at end of file