Last active
September 18, 2018 21:14
-
-
Save metapraveen/c0a46b866faf6fdf011a5d6c6dc504aa to your computer and use it in GitHub Desktop.
Revisions
-
metapraveen revised this gist
Sep 18, 2018 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ /** need to convert styles in query params to css props object // e.g. if URL is like http://localhost:8080/?header-color=0c1119 &header-background-color=fff -
metapraveen revised this gist
Sep 18, 2018 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,6 +35,7 @@ const cssProps = { import toPairs from 'lodash/toPairs'; import camelCase from 'lodash/camelCase'; //original function written function getCSSPropsFromQueryParams(queryParamsObject) { const validCssKeys = [ 'body-color', -
metapraveen renamed this gist
Sep 18, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
metapraveen created this gist
Sep 18, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,147 @@ /** // e.g. if URL is like http://localhost:8080/?header-color=0c1119 &header-background-color=fff &body-color=000 &body-background-color=e5822d &button-color=909694 &button-background-color=f442dc &button-hover-color=2de5a8 &button-hover-background-color=fff // the cssProps injected will be like below const cssProps = { header: { color: '#0c1119', backgroundColor: '#fff' }, body: { color: '#000', backgroundColor: '#e5822d' }, submitButton: { color: '#909694', backgroundColor: '#f442dc', backgroundImage: 'none' }, submitButtonHover: { color: '#2de5a8', backgroundColor: '#fff', backgroundImage: 'none' } }; */ import toPairs from 'lodash/toPairs'; import camelCase from 'lodash/camelCase'; function getCSSPropsFromQueryParams(queryParamsObject) { const validCssKeys = [ 'body-color', 'body-background-color', 'header-color', 'header-background-color', 'button-color', 'button-background-color', 'button-hover-color', 'button-hover-background-color' ]; return toPairs(queryParamsObject).reduce( (acc, [key, value]) => { if (validCssKeys.includes(key)) { // add "#" to the color values const cssValue = `#${queryParamsObject[key]}`; switch (true) { case key.toLocaleLowerCase().includes('header'): // remove "header-" from the key acc.header[camelCase(key.substring(7))] = cssValue; break; case key.toLocaleLowerCase().includes('body'): // remove "body-" from the key acc.body[camelCase(key.substring(5))] = cssValue; break; case key.toLocaleLowerCase().includes('button-hover'): // remove "button-hover-" acc.submitButtonHover[camelCase(key.substring(13))] = cssValue; // submit button has a background image which prevents effect of background color if (key === 'button-hover-background-color') { acc.submitButtonHover.backgroundImage = 'none'; } break; default: // remove "button-" from the key acc.submitButton[camelCase(key.substring(7))] = cssValue; // submit button has a background image which prevents effect of background color if (key === 'button-background-color') { acc.submitButton.backgroundImage = 'none'; } } } return acc; }, { header: {}, body: {}, submitButton: {}, submitButtonHover: {} } ); } //refactored function function getCSSPropsFromQueryParams(queryParamsObject) { return { header: { ...extractUrlParamToObj('header-color', queryParamsObject, value => ({ color: `#${value}` })), ...extractUrlParamToObj( 'header-background-color', queryParamsObject, value => ({ backgroundColor: `#${value}` }) ) }, body: { ...extractUrlParamToObj('body-color', queryParamsObject, value => ({ color: `#${value}` })), ...extractUrlParamToObj( 'body-background-color', queryParamsObject, value => ({ backgroundColor: `#${value}` }) ) }, submitButton: { ...extractUrlParamToObj('button-color', queryParamsObject, value => ({ color: `#${value}` })), ...extractUrlParamToObj( 'button-background-color', queryParamsObject, value => ({ backgroundColor: `#${value}`, backgroundImage: 'none' }) ) }, submitButtonHover: { ...extractUrlParamToObj( 'button-hover-color', queryParamsObject, value => ({ color: `#${value}` }) ), ...extractUrlParamToObj( 'button-hover-background-color', queryParamsObject, value => ({ backgroundColor: `#${value}`, backgroundImage: 'none' }) ) } }; } /** * check if the value for the given key in the queryParamsObject * and call the static mapper passed * @param {string} queryParam * @param {object} queryParamsObject * @param {Function} staticMapper * @returns {object} */ function extractUrlParamToObj(queryParam, queryParamsObject, staticMapper) { if (queryParamsObject[queryParam]) return staticMapper(queryParamsObject[queryParam]); }