When we start developing our application, it was working correctly but all skills loaded in the same URL. ( skill.susi.ai/SkillPage ). When user clicks the edit button every skill loaded in the same URL ( skill.susi.ai/EditSkill ). We got a requirement to load each of our skills in separate routes. This is how we implemented the child routes of the application.
We wanted to show each individual skill under this type of URL,
skill.susi.ai/ [SKILL GROUP] / [SKILL NAME] / [LANGUAGE]
When user clicks on the edit button, we needed to show that particular skill under this URL.
skill.susi.ai/ [SKILL GROUP] / [SKILL NAME] / edit / [LANGUAGE]
First we set our routings in index.js file.
We have to add the “exact” attribute, if we don’t add that it will not redirect users to “404” page when user trying to access wrong routes.
Next step is sending data from one component to another component.
In SUSI Skill CMS, user can choose any skill from the home page. Then after it goes to the skill page and shows details about the selected skill. We have to modify the button as,
Now the user clicks on the card. It changes the URL and loads the corresponding component according to the routes that we defined in “index.js” file previously.
Second thing that we need to do is to catch URL routs and render relevant data according to the URL routes.
Let’s say I clicked on “distance” skill. Then user will go to this URL “http://skills.susi.ai/Knowledge/distance/en ”
Now It loads the “SkillListing” component according to the route we defined in “ index.js ” here ””.
To derive data from URL we simply used these codes in “SkillListing.js”.
We collected data from the URL and made another URL, we used this URL to get details of the skill from the server. We used this urls as below.
If the Ajax request is success, those data are passed to “updateData()” and it updates the component and shows to users like this.
We applied same mechanism to the edit button and edit page. This is how we modified skill.susi.ai ‘s Routings. If you like to contribute SUSI Skill CMS please fork our repository on github. here
- Previous Blogpost about routing: https://blog.fossasia.org/implementation-of-react-routers-in-susi-web-chat/
- React Router v4 tutorial https://medium.com/@pshrmn/a-simple-react-router-v4-tutorial-7f23ff27adf