Skip to content

Commit

Permalink
Merge pull request #1561 from pierotofy/bigfix
Browse files Browse the repository at this point in the history
Hide Add Project button when user doesn't have permissions
  • Loading branch information
pierotofy authored Sep 29, 2024
2 parents 249869e + b1e9f02 commit eaf0db9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
24 changes: 17 additions & 7 deletions app/static/app/js/Dashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import PropTypes from 'prop-types';
import './css/Dashboard.scss';
import ProjectList from './components/ProjectList';
import EditProjectDialog from './components/EditProjectDialog';
Expand All @@ -11,9 +12,16 @@ import $ from 'jquery';
import { _ } from './classes/gettext';

class Dashboard extends React.Component {
constructor(){
super();

static defaultProps = {
permissions: []
};
static propTypes = {
permissions: PropTypes.array.isRequired,
};

constructor(props){
super(props);

this.handleAddProject = this.handleAddProject.bind(this);
this.addNewProject = this.addNewProject.bind(this);
}
Expand Down Expand Up @@ -58,14 +66,15 @@ class Dashboard extends React.Component {
return (
<Router basename="/dashboard">
<div>
{this.props.permissions.indexOf("add_project") !== -1 ?
<div className="text-right add-button">
<button type="button"
<button type="button"
className="btn btn-primary btn-sm"
onClick={this.handleAddProject}>
<i className="glyphicon glyphicon-plus"></i>
{_("Add Project")}
</button>
</div>
</div> : ""}

<EditProjectDialog
saveAction={this.addNewProject}
Expand All @@ -80,10 +89,11 @@ class Dashboard extends React.Component {

$(function(){
$("[data-dashboard]").each(function(){
window.ReactDOM.render(<Dashboard/>, $(this).get(0));
let props = $(this).data();
delete(props.dashboard);
window.ReactDOM.render(<Dashboard {...props}/>, $(this).get(0));
});


// Warn users if there's any sort of work in progress before
// they press the back button on the browser
// Yes it's a hack. No we're not going to track state in React just
Expand Down
2 changes: 1 addition & 1 deletion app/static/app/js/tests/Dashboard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Dashboard from '../Dashboard';

describe('<Dashboard />', () => {
it('renders without exploding', () => {
const wrapper = shallow(<Dashboard />);
const wrapper = shallow(<Dashboard permissions={['add_project']} />);
expect(wrapper.exists()).toBe(true);
})
});
6 changes: 5 additions & 1 deletion app/templates/app/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ <h3>{% trans 'Welcome!' %} ☺</h3>
</p>
{% endif %}

<div id="dashboard-app" data-dashboard></div>
<div id="dashboard-app" data-dashboard
{% for key, value in params %}
data-{{key}}="{{value}}"
{% endfor %}
></div>

{% endif %}
{% endblock %}
11 changes: 9 additions & 2 deletions app/views/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ def dashboard(request):
no_tasks = Task.objects.filter(project__owner=request.user).count() == 0
no_projects = Project.objects.filter(owner=request.user).count() == 0

permissions = []
if request.user.has_perm('app.add_project'):
permissions.append('add_project')

# Create first project automatically
if no_projects and request.user.has_perm('app.add_project'):
if no_projects and 'add_project' in permissions:
Project.objects.create(owner=request.user, name=_("First Project"))

return render(request, 'app/dashboard.html', {'title': _('Dashboard'),
'no_processingnodes': no_processingnodes,
'no_tasks': no_tasks
'no_tasks': no_tasks,
'params': {
'permissions': json.dumps(permissions)
}.items()
})


Expand Down

0 comments on commit eaf0db9

Please sign in to comment.