11import React from 'react'
22import PropTypes from 'prop-types'
3+ import update from 'react-addons-update'
4+ import { findIndex , some } from 'lodash'
5+ import { withRouter } from 'react-router-dom'
6+
37import UserSummary from '../UserSummary/UserSummary'
48import MenuList from '../MenuList/MenuList'
59import NotificationsIcon from '../../assets/icons/ui-bell.svg'
@@ -21,37 +25,93 @@ const navLinks = [{
2125 to : '/settings/profile' ,
2226 Icon : MyProfileIcon ,
2327 iconClassName : 'fill' ,
24- } , {
25- label : 'NOTIFICATION SETTINGS' ,
26- to : '/settings/notifications' ,
27- Icon : NotificationSettingsIcon ,
28- iconClassName : 'fill' ,
29- } , {
30- label : 'ACCOUNT & SECURITY' ,
31- to : '/settings/account' ,
32- Icon : AccountSecurityIcon ,
33- iconClassName : 'fill' ,
28+ children : [
29+ {
30+ label : 'PROFILE INFORMATION' ,
31+ to : '/settings/profile' ,
32+ Icon : MyProfileIcon ,
33+ iconClassName : 'fill' ,
34+ } ,
35+ {
36+ label : 'NOTIFICATION SETTINGS' ,
37+ to : '/settings/notifications' ,
38+ Icon : NotificationSettingsIcon ,
39+ iconClassName : 'fill' ,
40+ } , {
41+ label : 'ACCOUNT & SECURITY' ,
42+ to : '/settings/account' ,
43+ Icon : AccountSecurityIcon ,
44+ iconClassName : 'fill' ,
45+ }
46+ ]
3447} , {
3548 label : 'NOTIFICATIONS' ,
3649 to : '/notifications' ,
3750 Icon : NotificationsIcon ,
3851 iconClassName : 'fill' ,
3952} ]
4053
41- const UserSidebar = ( { user} ) => {
42- return (
43- < div styleName = "container" >
44- < div className = "sideAreaWrapper" >
45- < UserSummary user = { user } />
46- < hr styleName = "separator" />
47- < MenuList navLinks = { navLinks } />
54+ class UserSidebar extends React . Component {
55+ constructor ( props ) {
56+ super ( props )
57+ this . state = {
58+ navLinks
59+ }
60+ }
61+
62+ componentDidMount ( ) {
63+ this . initAccordion ( )
64+ }
65+
66+ initAccordion ( ) {
67+ const { match} = this . props
68+ const { navLinks} = this . state
69+
70+ const matchedPath = match && match . path
71+ const activeNavIndex = findIndex ( navLinks , nav => nav . children && some ( nav . children , c => c . to === matchedPath ) )
72+
73+ if ( activeNavIndex >= 0 ) {
74+ this . setAccordionOpen ( activeNavIndex , true )
75+ }
76+ }
77+
78+ setAccordionOpen ( i , open ) {
79+ const { navLinks } = this . state
80+ const updatedNavLinks = update ( navLinks , {
81+ [ i ] : {
82+ $set : {
83+ ...navLinks [ i ] ,
84+ isAccordionOpen : open
85+ }
86+ }
87+ } )
88+
89+ this . setState ( {
90+ navLinks : updatedNavLinks
91+ } )
92+ }
93+
94+ render ( ) {
95+ const { user} = this . props
96+ const { navLinks} = this . state
97+
98+ return (
99+ < div styleName = "container" >
100+ < div className = "sideAreaWrapper" >
101+ < UserSummary user = { user } />
102+ < hr styleName = "separator" />
103+ < div styleName = "section-title" >
104+ SYSTEM
105+ </ div >
106+ < MenuList navLinks = { navLinks } onAccordionToggle = { ( i , open ) => this . setAccordionOpen ( i , open ) } />
107+ </ div >
48108 </ div >
49- </ div >
50- )
109+ )
110+ }
51111}
52112
53113UserSidebar . propTypes = {
54114 user : PropTypes . object . isRequired
55115}
56116
57- export default UserSidebar
117+ export default withRouter ( UserSidebar )
0 commit comments