Skip to content

Commit

Permalink
add a way to manage multiple producers as a holding user
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Nov 28, 2023
1 parent 6ea257c commit b814559
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
44 changes: 26 additions & 18 deletions components/ui/user-menu-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const UserMenuList = ({ className, listItemClassName, user, operators, notificat
size: '-auto'
});
}
const userOperators = operators.filter(o => user.operator_ids.includes(+o.id));

return (
<ul className={className}>
Expand All @@ -36,7 +37,7 @@ const UserMenuList = ({ className, listItemClassName, user, operators, notificat
<a>{intl.formatMessage({ id: 'My profile' })}</a>
</Link>
</li>
{(user.role === 'operator' || user.role === 'holding') && (
{user.role === 'operator' && (
<li className={listItemClassName}>
<Link
href="/operators/edit"
Expand All @@ -46,23 +47,30 @@ const UserMenuList = ({ className, listItemClassName, user, operators, notificat
</Link>
</li>
)}
{(user.role === 'operator' || user.role === 'holding') && uniq(user.operator_ids).map(id => {
const operator = operators.find(o => +o.id === id);
if (!operator) return null;

return (
<li key={`dropdown-operator-${id}`} className={listItemClassName}>
<Link
href={`/operators/${operator.slug}/documentation`}
prefetch={false}
>
<a>
{operator.name}
</a>
</Link>
</li>
)
})}
{user.role === 'holding' && userOperators.map(operator => (
<li className={listItemClassName}>
<Link
href={`/operators/edit/${operator.id}`}
prefetch={false}
>
<a>
{intl.formatMessage({ id: 'company.profile', defaultMessage: `${operator.name} profile` }, { company: operator.name })}
</a>
</Link>
</li>
))}
{(user.role === 'operator' || user.role === 'holding') && userOperators.map(operator => (
<li key={`dropdown-operator-docs-${operator.id}`} className={listItemClassName}>
<Link
href={`/operators/${operator.slug}/documentation`}
prefetch={false}
>
<a>
{operator.name}
</a>
</Link>
</li>
))}
{user.role === 'admin' && (
<li className={listItemClassName}>
<a href="/admin" >{intl.formatMessage({ id: 'logged_in.dropdown.admin' })}</a>
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,16 @@ app
server.get('/:locale?/profile', onlyAuthenticated);

// OPERATORS
server.get('/:locale?/operators/edit', onlyAuthenticated);
server.get('/:locale?/operators/edit/:id?', (req, res) => {
if (!req.session.user) return homeRedirect(req, res);

return app.render(
req,
res,
'/operators/edit',
Object.assign(req.params, req.query, localeParams(req))
);
});
server.get('/:locale?/operators/detail', notFound);
server.get('/:locale?/operators/new', (req, res) =>
app.render(
Expand Down
1 change: 1 addition & 0 deletions lang/zu.json
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@
"to": "to",
"My profile": "My profile",
"Producer profile": "Producer profile",
"company.profile": "{company} profile",
"Current Password": "Current Password",
"We need your current password to confirm your changes": "We need your current password to confirm your changes",
"User profile": "User profile",
Expand Down
17 changes: 11 additions & 6 deletions pages/operators/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ class OperatorsEdit extends React.Component {
static async getInitialProps({ store, url }) {
const { user } = store.getState();

if (user.operator_ids) {
await store.dispatch(getUserOperator(user.operator_ids[0]));
if (url.query.id && !user.operator_ids.includes(Number(url.query.id))) {
return { errorCode: 404 };
}
return { url };
const operatorId = Number(url.query.id) || user.operator_ids[0];
if (operatorId) {
await store.dispatch(getUserOperator(operatorId));
}
return { url, operatorId };
}

/**
Expand Down Expand Up @@ -54,13 +58,13 @@ class OperatorsEdit extends React.Component {

handleOperatorEditSubmit = () => {
this.props.getOperators();
this.props.getUserOperator(this.props.user.operator_ids[0]);
this.props.getUserOperator(this.props.operatorId);
}

render() {
const { url, user, userOperator } = this.props;
const { url, userOperator, operatorId } = this.props;

if (!user.operator_ids) {
if (!operatorId) {
return null;
}

Expand Down Expand Up @@ -93,6 +97,7 @@ class OperatorsEdit extends React.Component {
OperatorsEdit.propTypes = {
intl: PropTypes.object.isRequired,
url: PropTypes.object,
operatorId: PropTypes.number.isRequired,
user: PropTypes.object,
userOperator: PropTypes.object,
getOperators: PropTypes.func,
Expand Down

0 comments on commit b814559

Please sign in to comment.