Commit 60cb5ec1 authored by Alex Kakiris's avatar Alex Kakiris
Browse files

add user-scoped invokers page

parent 1086facb
Loading
Loading
Loading
Loading
+2 −2
Changes for capif_frontend/src/configs/acl.js: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -11,9 +11,9 @@ const defineRulesFor = (role, subject) => {
  const { can, cannot, rules } = new AbilityBuilder(AppAbility)
  if (role === 'admin') {
    can('manage', 'all')
    cannot('read', 'user-home')
    cannot('read', 'user-area')
  } else if (role === 'user') {
    can('read', 'user-home')
    can('read', 'user-area')
  } else if (role === 'client') {
    can(['read'], 'acl-page')
  }
+8 −1
Changes for capif_frontend/src/navigation/vertical/index.js: 8 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -5,7 +5,14 @@ const navigation = () => {
      icon: 'mdi:home-outline',
      path: '/home',
      action: 'read',
      subject: 'user-home'
      subject: 'user-area'
    },
    {
      title: 'Invokers',
      icon: 'mdi:baseline-people',
      path: '/me/invokers',
      action: 'read',
      subject: 'user-area'
    },
    {
      title: 'Dashboard',
+1 −1
Changes for capif_frontend/src/pages/home/index.js: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ const Home = () => {

Home.acl = {
    action: 'read',
    subject: 'user-home'
    subject: 'user-area'
}

export default Home
 No newline at end of file
+82 −0
Changes for capif_frontend/src/pages/me/invokers/index.js: 82 added lines, 0 removed lines.
Original line number Diff line number Diff line
import Box from '@mui/material/Box'
import Card from '@mui/material/Card'
import CardHeader from '@mui/material/CardHeader'
import CardContent from '@mui/material/CardContent'
import CircularProgress from '@mui/material/CircularProgress'
import Alert from '@mui/material/Alert'
import List from '@mui/material/List'
import ListItem from '@mui/material/ListItem'
import ListItemText from '@mui/material/ListItemText'
import Typography from '@mui/material/Typography'
import { useQuery } from '@tanstack/react-query'

import { getMyInvokers } from 'src/configs/apiService'

// Client errors (e.g. 403 no CAPIF credentials) won't change on retry; only retry server-side failures.
const retryServerErrorsOnly = (failureCount, error) => {
  const status = error?.response?.status
  if (status && status < 500) return false

  return failureCount < 3
}

const errorMessage = error => {
  if (error?.response?.status === 403) return 'This account has no CAPIF credentials.'

  return error?.response?.data?.error || 'Could not load your invokers.'
}

const InvokerList = () => {
  const { data, isPending, isError, error } = useQuery({
    queryKey: ['my-invokers'],
    queryFn: getMyInvokers,
    retry: retryServerErrorsOnly
  })

  // Derived during render: `data` is undefined until the first response arrives.
  const invokers = data?.invokers ?? []

  if (isPending) {
    return (
      <Box sx={{ display: 'flex', justifyContent: 'center', p: 4 }}>
        <CircularProgress />
      </Box>
    )
  }

  if (isError) {
    return <Alert severity='error'>{errorMessage(error)}</Alert>
  }

  if (invokers.length === 0) {
    return <Typography>{"You don't have an invoker yet."}</Typography>
  }

  return (
    <List>
      {invokers.map(id => (
        <ListItem key={id} disableGutters>
          <ListItemText primary={id} />
        </ListItem>
      ))}
    </List>
  )
}

const MyInvokersPage = () => (
  <Box sx={{ p: 6 }}>
    <Card>
      <CardHeader title='Your invokers' />
      <CardContent>
        <InvokerList />
      </CardContent>
    </Card>
  </Box>
)

MyInvokersPage.acl = {
  action: 'read',
  subject: 'user-area'
}

export default MyInvokersPage
 No newline at end of file