Skip to content

Commit

Permalink
🛂 updates what a member can see and what not from dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
josemigallas committed Oct 7, 2024
1 parent c0ec886 commit 3e149ee
Show file tree
Hide file tree
Showing 21 changed files with 298 additions and 222 deletions.
2 changes: 1 addition & 1 deletion app/concerns/new_application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def accounts_presenter
end

def products_presenter
@products_presenter ||= Api::ServicesIndexPresenter.new(current_user: user)
@products_presenter ||= Api::ServicesIndexPresenter.new(user: user)
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,6 @@ def can_create?
end

def presenter
@presenter ||= Api::ServicesIndexPresenter.new(current_user: current_user, params: params)
@presenter ||= Api::ServicesIndexPresenter.new(user: current_user, params: params)
end
end
2 changes: 1 addition & 1 deletion app/controllers/provider/admin/backend_apis_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ def create_params
alias update_params backend_api_params

def presenter
@presenter ||= Provider::Admin::BackendApisIndexPresenter.new(current_account: current_account, params: params)
@presenter ||= Provider::Admin::BackendApisIndexPresenter.new(user: current_user, params: params)
end
end
13 changes: 5 additions & 8 deletions app/controllers/provider/admin/dashboards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Provider::Admin::DashboardsController < FrontendController
activate_menu :dashboard
layout 'provider'

helper_method :presenter

attr_reader :presenter

def show
@service = find_service
# Would be cool to
Expand All @@ -17,6 +21,7 @@ def show
@services = current_user.accessible_services
@messages_presenter = current_presenter
@unread_messages_presenter = unread_messages_presenter
@presenter = Provider::Admin::DashboardPresenter.new(user: current_user)
end

include DashboardTimeRange
Expand Down Expand Up @@ -44,14 +49,6 @@ def unread_messages_presenter
::Dashboard::UnreadMessagesPresenter.new(current_account.received_messages.not_system)
end

def backend_apis_presenter
Provider::Admin::BackendApisIndexPresenter.new(current_account: current_account, params: { per_page: 5 })
end

def products_presenter
Api::ServicesIndexPresenter.new(current_user: current_user, params: { per_page: 5 })
end

def quickstarts_flash
return unless flash[:first_login]

Expand Down
2 changes: 1 addition & 1 deletion app/helpers/menu_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def context_selector_props
menu_items = [{ title: 'Dashboard', href: provider_admin_dashboard_path, icon: :home, disabled: false }]
menu_items << { title: 'Audience', href: audience_link, icon: :bullseye, disabled: false } if audience_link.present?
menu_items << { title: 'Products', href: admin_services_path, icon: :cubes, disabled: false } if access_to_service_admin_sections
menu_items << { title: 'Backends', href: provider_admin_backend_apis_path, icon: :cube, disabled: false } if access_to_service_admin_sections
menu_items << { title: 'Backends', href: provider_admin_backend_apis_path, icon: :cube, disabled: false } if can?(:read, BackendApiConfig) || can?(:manage, :monitoring)
menu_items << { title: 'Account Settings', href: settings_link, icon: :cog, disabled: false }

{
Expand Down
14 changes: 8 additions & 6 deletions app/javascript/src/BackendApis/components/IndexPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { IActions } from '@patternfly/react-table'
import type { Backend } from 'BackendApis/types'

interface Props {
newBackendPath: string;
newBackendPath?: string;
backends: Backend[];
backendsCount: number;
}
Expand Down Expand Up @@ -75,11 +75,13 @@ const IndexPage: FunctionComponent<Props> = ({
<ToolbarItem spacer={{ default: 'spacerMd' }} variant="search-filter">
<ToolbarSearch placeholder="Find a backend" />
</ToolbarItem>
<ToolbarItem>
<Button component="a" href={newBackendPath} variant="primary">
Create a backend
</Button>
</ToolbarItem>
{newBackendPath && (
<ToolbarItem>
<Button component="a" href={newBackendPath} variant="primary">
Create a backend
</Button>
</ToolbarItem>
)}
<ToolbarItem alignment={{ default: 'alignRight' }} variant="pagination">
<Pagination itemCount={backendsCount} />
</ToolbarItem>
Expand Down
20 changes: 11 additions & 9 deletions app/javascript/src/Dashboard/components/BackendsWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { createReactWrapper } from 'utilities/createReactWrapper'
import type { FunctionComponent } from 'react'

interface Props {
newBackendPath: string;
newBackendPath?: string;
backendsPath: string;
backends: {
id: number;
Expand Down Expand Up @@ -45,14 +45,16 @@ const BackendsWidget: FunctionComponent<Props> = ({
Backends
</Title>
<CardActions>
<Button
component="a"
data-testid="dashboardCreateBackend-buttonLink"
href={newBackendPath}
variant="primary"
>
Create Backend
</Button>
{newBackendPath && (
<Button
component="a"
data-testid="dashboardCreateBackend-buttonLink"
href={newBackendPath}
variant="primary"
>
Create Backend
</Button>
)}
</CardActions>
</div>
<div className="dashboard-list-subtitle">
Expand Down
20 changes: 11 additions & 9 deletions app/javascript/src/Dashboard/components/ProductsWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { createReactWrapper } from 'utilities/createReactWrapper'
import type { FunctionComponent } from 'react'

interface Props {
newProductPath: string;
newProductPath?: string;
productsPath: string;
products: {
id: number;
Expand Down Expand Up @@ -45,14 +45,16 @@ const ProductsWidget: FunctionComponent<Props> = ({
Products
</Title>
<CardActions>
<Button
component="a"
data-testid="dashboardCreateProduct-buttonLink"
href={newProductPath}
variant="primary"
>
Create Product
</Button>
{newProductPath && (
<Button
component="a"
data-testid="dashboardCreateProduct-buttonLink"
href={newProductPath}
variant="primary"
>
Create Product
</Button>
)}
</CardActions>
</div>
<div className="dashboard-list-subtitle">
Expand Down
34 changes: 18 additions & 16 deletions app/javascript/src/Products/components/IndexPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import { Pagination } from 'Common/components/Pagination'
import { ToolbarSearch } from 'Common/components/ToolbarSearch'
import { createReactWrapper } from 'utilities/createReactWrapper'

import type { IActionsResolver } from '@patternfly/react-table'
import type { Product } from 'Products/types'

import './IndexPage.scss'

interface Props {
newProductPath: string;
newProductPath?: string;
products: Product[];
productsCount: number;
}
Expand All @@ -41,7 +42,7 @@ const IndexPage: React.FunctionComponent<Props> = ({

const tableRows = products.map(p => ({
cells: [
{ title: <Button isInline component="a" href={p.links[1].path} variant="link">{p.name}</Button> },
{ title: <Button isInline component="a" href={p.links[0].path} variant="link">{p.name}</Button> },
p.systemName,
<span key={p.systemName} className="api-table-timestamp">{p.updatedAt}</span>,
p.appsCount,
Expand All @@ -50,15 +51,14 @@ const IndexPage: React.FunctionComponent<Props> = ({
]
}))

const linkToPage = (rowId: number, actionNumber: number) => {
const { path } = products[rowId].links[actionNumber]
window.location.href = path
}
const actionResolver: IActionsResolver = (rowData, { rowIndex = 0 }) => {
const product = products[rowIndex]

const tableActions = ['Edit', 'Overview', 'Analytics', 'Applications', 'ActiveDocs', 'Integration'].map((title, i) => ({
title,
onClick: (_e: unknown, rowId: number) => { linkToPage(rowId, i) }
}))
return product.links.map(link => ({
title: link.name,
onClick: () => { window.location.href = link.path }
}))
}

return (
<>
Expand All @@ -77,17 +77,19 @@ const IndexPage: React.FunctionComponent<Props> = ({
<ToolbarItem spacer={{ default: 'spacerMd' }} variant="search-filter">
<ToolbarSearch placeholder="Find a product" />
</ToolbarItem>
<ToolbarItem>
<Button component="a" href={newProductPath} variant="primary">
Create a product
</Button>
</ToolbarItem>
{newProductPath && (
<ToolbarItem>
<Button component="a" href={newProductPath} variant="primary">
Create a product
</Button>
</ToolbarItem>
)}
<ToolbarItem alignment={{ default: 'alignRight' }} variant="pagination">
<Pagination itemCount={productsCount} />
</ToolbarItem>
</ToolbarContent>
</Toolbar>
<Table actions={tableActions} aria-label="Products Table" cells={tableColumns} rows={tableRows}>
<Table actionResolver={actionResolver} aria-label="Products Table" cells={tableColumns} rows={tableRows}>
<TableHeader />
<TableBody />
</Table>
Expand Down
64 changes: 39 additions & 25 deletions app/presenters/api/services_index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,61 @@
class Api::ServicesIndexPresenter
include System::UrlHelpers.system_url_helpers

def initialize(current_user:, params: {})
@current_user = current_user
@pagination_params = { page: params[:page] || 1, per_page: params[:per_page] || 20 }
@search = ThreeScale::Search.new(params[:search])
@sorting_params = "#{params[:sort].presence || 'updated_at'} #{params[:direction].presence || 'desc'}"
def initialize(user:, params: {})
@user = user
@ability = Ability.new(user)

pagination_params = { page: params[:page] || 1, per_page: params[:per_page] || 20 }
search = ThreeScale::Search.new(params[:search])
sorting_params = "#{params[:sort].presence || 'updated_at'} #{params[:direction].presence || 'desc'}"

@products = user.accessible_services
.order(sorting_params)
.scope_search(search)
.paginate(pagination_params)
end

attr_reader :current_user, :pagination_params, :search, :sorting_params

delegate :total_entries, to: :products

def products
@products ||= current_user.accessible_services
.order(sorting_params)
.scope_search(search)
.paginate(pagination_params)
end
attr_reader :products

alias paginated_products products

delegate :total_entries, to: :products

def data
{
'new-product-path': new_admin_service_path,
products: products.map { |p| ServicePresenter.new(p).index_data.as_json }.to_json,
'new-product-path': can_create_service ? new_admin_service_path : nil,
products: products.map do |product|
{
id: product.id,
name: product.name,
systemName: product.system_name,
updatedAt: product.updated_at.to_s(:long),
links: ServiceActionsPresenter.new(user).actions(product),
appsCount: product.cinstances.size,
backendsCount: product.backend_api_configs.size,
unreadAlertsCount: product.decorate.unread_alerts_count
}
end.to_json,
'products-count': total_entries
}
end

def dashboard_widget_data
{
products: products.map { |p| ServicePresenter.new(p).dashboard_widget_data.as_json },
newProductPath: new_admin_service_path,
productsPath: admin_services_path
}
end

# The JSON response of index endpoint is used to populate NewApplicationForm's BuyerSelect
def render_json
{
items: products.map { |p| ServicePresenter.new(p).new_application_data.as_json }.to_json,
count: total_entries
}
end

private

attr_reader :user, :ability

delegate :can?, to: :ability

# See https://github.com/3scale/porta/blob/27c0e3ab66e6d589412b2e87ee86e32c1f7f5390/app/controllers/api/services_controller.rb#L165
def can_create_service
user.admin? || can?(:create, Service)
end
end
39 changes: 0 additions & 39 deletions app/presenters/backend_api_presenter.rb

This file was deleted.

Loading

0 comments on commit 3e149ee

Please sign in to comment.