We had a feature in SUSI Web Chat to make custom themes but those themes were not storing on the server. We needed to store those theme data on server. In this post I discuss how we implemented that feature. This is the PR that I sent to solve this issue.
Previously we had two theme options. According to the user’s choice it changes theme colors. Since we needed to store custom themes and use them without any conflicts with existing “light” and “dark” themes we made another theme option called “custom”. After user clicks on the custom theme it automatically changes to “custom” mode.
This is how we did it in “onClick” of the custom theme .
Then after we collected all the chosen color values to a variable. While we store our color values on a variable we avoid the “#” letter which is at very first of the color value. Because we can’t send that value to the server with “#” character.
After selecting color values user have to press the save button to push those selected values to server. We execute below method on click of the save button.
Using this method we derived those data that we added into the variable and made a single string array. Then after we executed the action that we needed to execute to store data on the server.
It is “Actions.customThemeChanged(customData);”.
This action is defined in “Settings.actions.js” file.
We used this Action name constant “CHANGE_CUSTOM_THEME” in “ChatConstant.js” file
We defined this “pushCustomThemeToServer” function on “API.actions.js” file. here
Here we check whether user is logged in or not. If user is logged in we get the access token from cookies and attach it to the request URL and execute the “makeServerCall” function that we defined previously.
Now our data are saved on server. Use this url to check what settings you have in your user account.
Now we can use stored values. First we need to update state. For that we got theme values from server like this
Here we got data from server and put it to the array.
Then after we set it to state. While adding custom theme settings to state we set the “#” character before each colour value. Here is the code
Now we have to use these data with our JSX elements. This is how we did this.
We checked the current theme mode. If it is “custom” we used the values we got from server. Otherwise we used corresponding colors for other “light” and “dark” theme. Here is the full code.
You can use these variables wherever you need to show colors. As an example this is how we passed header color to top bar.
This is how we stored and fetched custom theme data from store.
- How to store and receive data from SUSI server using HTTP requests. https://github.com/fossasia/chat.susi.ai/blob/master/docs/Accounting.md
- How Flux Architecture works: https://facebook.github.io/flux/