Skip to content

Commit

Permalink
page restricted based on roles
Browse files Browse the repository at this point in the history
  • Loading branch information
sidhu subhash committed Feb 25, 2020
1 parent 627425e commit 308e341
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 53 deletions.
11 changes: 10 additions & 1 deletion src/walkin-core/src/routes/CoreLanding/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,18 @@ class CoreLandingPage extends React.Component<
console.log('CoreLandingPage userData API', res.data.user);
await localStorage.setItem('role', res.data.user.roles[0].name);
this.setState({ spin: false });
let activeRoute = '/rewardx/customer_search';
switch (res.data.user.roles[0].name) {
case 'Finance':
activeRoute = '/rewardx/reports';
break;
case 'Customer_Care':
activeRoute = '/rewardx/customer_search';
break;
}
setTimeout(() => {
this.props.history.push({
pathname: '/rewardx/customer_search',
pathname: activeRoute,
});
// push url
}, 2000);
Expand Down
12 changes: 6 additions & 6 deletions src/walkin-rewardx/src/containers/BusinessRules/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ class BusinessRules extends React.Component<
componentWillMount() {
const roles: any = localStorage.getItem('role');
console.log('CustomerSearch roles', roles);
if (roles && roles !== 'ADMIN') {
message.warn('You do not have access to this page');
this.props.history.push({
pathname: '/rewardx/customer_search',
});
} else {
if (['ADMIN'].includes(roles)) {
const jwtToken: any = localStorage.getItem('jwt');
const { org_id }: any = jwt.decode(jwtToken);

Expand Down Expand Up @@ -123,6 +118,11 @@ class BusinessRules extends React.Component<
// loading: false
// });
// });
} else {
message.warn('You do not have access to this page');
this.props.history.push({
pathname: '/rewardx/customer_search',
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ class CustomerCare extends React.Component<

// hide everything from customer search except phone number
componentDidMount() {
console.log('CustomerCare cdm record', this.props.location.state.record);

if (
this.props.location.state.record.phoneNumber &&
this.props.location.state.loyaltyTransactionsData &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CustomerSearch extends React.Component<
this.props.client.cache['data']['data']['$ROOT_QUERY.auth']
.organizationId,
customer: null,
loading: true,
loading: false,
loadingTable: false,
phoneNumber: null,
customerId: null,
Expand All @@ -67,8 +67,14 @@ class CustomerSearch extends React.Component<
};
}

componentDidMount() {
this.setState({ loading: false });
UNSAFE_componentWillMount() {
const roles: any = localStorage.getItem('role');
if (!['ADMIN', 'Customer_Care'].includes(roles)) {
message.warn('You do not have access to this page');
this.props.history.push({
pathname: '/rewardx/reports',
});
}
}

getCustomerDetails() {
Expand Down
30 changes: 19 additions & 11 deletions src/walkin-rewardx/src/containers/Reports/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,26 @@ class Reports extends React.Component<ReportsProps, ReportsState> {

UNSAFE_componentWillMount() {
const jwtToken: any = localStorage.getItem('jwt');
const { org_id }: any = jwt.decode(jwtToken);
this.getReports(org_id, reportsResponse => {
this.setState({
initLoading: false,
organizationId: org_id,
reports: reportsResponse.data.reportConfigs,
// totalPages:
// reportsResponse.data.reportConfigs.paginationInfo.totalPages,
// totalItems:
// reportsResponse.data.reportConfigs.paginationInfo.totalItems,
const roles: any = localStorage.getItem('role');
if (['ADMIN', 'Finance'].includes(roles)) {
const { org_id }: any = jwt.decode(jwtToken);
this.getReports(org_id, reportsResponse => {
this.setState({
initLoading: false,
organizationId: org_id,
reports: reportsResponse.data.reportConfigs,
// totalPages:
// reportsResponse.data.reportConfigs.paginationInfo.totalPages,
// totalItems:
// reportsResponse.data.reportConfigs.paginationInfo.totalItems,
});
});
});
} else {
message.warn('You do not have access to this page');
this.props.history.push({
pathname: '/rewardx/customer_search',
});
}
}

getReports = (organizationId: string, callback) => {
Expand Down
55 changes: 25 additions & 30 deletions src/walkin-rewardx/src/containers/SidebarContent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,8 @@ class SidebarContent extends React.Component<
return '';
};

renderBusinessRules(roles: any) {
if (roles && roles === 'ADMIN') {
return (
<Menu.Item>
<Link to="/rewardx/business_rules">
<Icon type="profile" />
<span>Business Rules</span>
</Link>
</Menu.Item>
);
}

return null;
}

render() {
const roles: any = localStorage.getItem('role');
console.log('CustomerSearch roles', roles);

const { themeType, navStyle, pathname, match } = this.props;
const selectedKeys = pathname.substr(1);
const defaultOpenKeys = selectedKeys.split('/')[1];
Expand Down Expand Up @@ -105,19 +88,31 @@ class SidebarContent extends React.Component<
</Menu.Item>
*/}

<Menu.Item>
<Link to="/rewardx/customer_search">
<Icon type="profile" />
<span>Customer Care</span>
</Link>
</Menu.Item>
<Menu.Item key="reports">
<Link to="/rewardx/reports">
<Icon type="profile" />
<span>Reports</span>
</Link>
</Menu.Item>
{this.renderBusinessRules(roles)}
{['ADMIN', 'Customer_Care'].includes(roles) ? (
<Menu.Item key="customer_search">
<Link to="/rewardx/customer_search">
<Icon type="profile" />
<span>Customer Care</span>
</Link>
</Menu.Item>
) : null}

{['ADMIN'].includes(roles) ? (
<Menu.Item>
<Link to="/rewardx/business_rules">
<Icon type="profile" />
<span>Business Rules</span>
</Link>
</Menu.Item>
) : null}
{['ADMIN', 'Finance'].includes(roles) ? (
<Menu.Item key="reports">
<Link to="/rewardx/reports">
<Icon type="profile" />
<span>Reports</span>
</Link>
</Menu.Item>
) : null}
</Menu>
</div>
</Auxiliary>
Expand Down

0 comments on commit 308e341

Please sign in to comment.