Dialog Component in SUSI.AI is rendered in App.js to remove code redundancy. Redux is integrated in the Dialog component which allows us to open/close the dialog from any component by altering the modal states. This implementation allows us to get rid of the need of having dialog component in different components.
Redux Code
There are two actions and reducers which control the dialog component. Default state of isModalOpen is false and modalType is an empty string. To open a dialog modal the action openModal is dispatched, which sets isModalOpen to true and the modalType. To close a dialog modal the action closeModal is dispatched, which sets isModalOpen to default state i.e. false.
import { handleActions } from 'redux-actions'; import actionTypes from '../actionTypes'; const defaultState = { modalProps: { isModalOpen: false, modalType: '', }, }; export default handleActions( { [actionTypes.UI_OPEN_MODAL](state, { payload }) { return { ...state, modalProps: { isModalOpen: true, ...payload, }, }; }, [actionTypes.UI_CLOSE_MODAL](state) { return { ...state, modalProps: defaultState.modalProps, }; }, } defaultState, );
Shared Dialog Component
Dialog Modal can be opened from any component by dispatching an action.
To open a Dialog Modal: this.props.actions.openModal({modalType: [modal name]});
To close a Dialog Modal: this.props.actions.closeModal();
Shared Dialog Component has a DialogData object which contains objects with two main properties : Dialog component and Dialog size. Other props can also be passed along with these two properties such as fullScreen. Dialog Content of different Dialogs are present in their respective folders. Each Dialog Content has a Title, Content and Actions.Different Dialog types present are:
- Confirm Delete with Input: This dialog modal is used when a user deletes account, device and skill.
- Confirm Dialog: This dialog modal is used where confirmation is required from the user/admin such as on changing skill status, on password reset,etc.
- Share Dialog: This dialog modal opens up when the share icon is clicked in the chat.
- Standard Action Dialog: This dialog modal opens up on restore skill, delete feedback, system settings and bot.
- Tour Dialog: This dialog modal opens up SUSI.AI tour.
To add a new Dialog to DialogSection, the steps are:
- Import the Dialog Content Component
- Add the Dialog Component to DialogData object in the following manner:
const DialogData = { [dialog componet name]: { Component : [imported dialog component name], size : [size of the Dialog Component]}, }
Code (Reduced)
const DialogData = { login: { Component: Login, size: 'sm' }, } const DialogSection = props => { const { actions, modalProps: { isModalOpen, modalType, ...otherProps }, visited, } = props; const getDialog = () => { if (isModalOpen) { return DialogData[modalType]; } return DialogData.noComponent; }; const { size, Component, fullScreen = false } = getDialog(); return ( <Dialog maxWidth={size} fullWidth={true} open={isModalOpen || !visited} onClose={isModalOpen ? actions.closeModal : actions.setVisited} fullScreen={fullScreen} > <DialogContainer> {Component ? <Component {...otherProps} /> : null} </DialogContainer> </Dialog> ) };
In conclusion, having a shared dialog component reduces redundant code and allows to have a similar Dialog UI across the repo. Also having one component managing all the dialogs removes the possibility of two dialogs being fired up at once.