diff --git a/src/components/route-creation/AuthoritiesSelect.tsx b/src/components/route-creation/AuthoritiesSelect.tsx new file mode 100644 index 0000000..933874f --- /dev/null +++ b/src/components/route-creation/AuthoritiesSelect.tsx @@ -0,0 +1,57 @@ +import i18n from '@dhis2/d2-i18n' +import { Field, MultiSelect, MultiSelectOption } from '@dhis2/ui' +import * as React from 'react' +import { ApiRouteData, Authority } from '../../types/RouteInfo' + +type AuthoritiesSelectProps = { + // eslint-disable-next-line @typescript-eslint/no-empty-object-type + route: ApiRouteData | {} + authorities: Authority[] + onSelect: React.Dispatch> +} + +const AuthoritiesSelect: React.FC = ({ + route, + authorities, + onSelect, +}) => { + const [selectedAuthorities, setSelectedAuthorities] = React.useState< + string[] + >(() => route.authorities ?? []) + + const onChange = ({ selected }) => { + setSelectedAuthorities(selected) + } + const onBlur = () => { + onSelect(selectedAuthorities) + } + return ( + + + {authorities?.map((authority) => { + return ( + + ) + })} + + + ) +} + +export default AuthoritiesSelect diff --git a/src/components/route-creation/UpsertRoute.tsx b/src/components/route-creation/UpsertRoute.tsx index b689138..4fbdbd2 100644 --- a/src/components/route-creation/UpsertRoute.tsx +++ b/src/components/route-creation/UpsertRoute.tsx @@ -14,8 +14,10 @@ import classes from '../../App.module.css' import { ApiRouteCreationPayload, ApiRouteData, + Authority, RouteAuthConfig, } from '../../types/RouteInfo' +import AuthoritiesSelect from './AuthoritiesSelect' import RouteAuthAdmin from './RouteAuthAdmin' const createRouteMutation = { @@ -38,12 +40,14 @@ const updateRouteMutation = { } type UpsertRouteProps = { + authorities: Authority[] route: ApiRouteData closeModal: VoidFunction onSave: VoidFunction } const UpsertRoute: React.FC = ({ + authorities: allAuthorities = [], route = {}, closeModal = () => {}, onSave = () => {}, @@ -54,8 +58,8 @@ const UpsertRoute: React.FC = ({ const [authConfig, setAuthConfig] = useState>( route.auth ) - const [authorities, setAuthorities] = useState(() => - route.authorities?.join(',') + const [selectedAuthorities, setSelectedAuthorities] = useState( + () => route.authorities ?? [] ) const [loading, setLoading] = useState(false) @@ -122,8 +126,8 @@ const UpsertRoute: React.FC = ({ data.auth = authConfig as RouteAuthConfig } - if (authorities) { - data.authorities = authorities?.split(/[\s,]/) + if (selectedAuthorities) { + data.authorities = selectedAuthorities } if (route?.id) { @@ -141,7 +145,6 @@ const UpsertRoute: React.FC = ({ onSave() } } catch (err) { - console.log('>>>eee', err) show({ type: 'error', message: err.message }) } finally { setLoading(false) @@ -194,16 +197,10 @@ const UpsertRoute: React.FC = ({ authConfig={authConfig} updateAuthConfig={updateAuthConfig} /> - setAuthorities(value)} - placeholder={i18n.t( - 'comma separated list of authorities i.e. MY_AUTHORITY_1,MY_AUTHORITY_2' - )} - label={i18n.t('Authorities')} - helpText={i18n.t( - 'Restrict access to cerain authorities' - )} + diff --git a/src/components/route-list/RoutesList.tsx b/src/components/route-list/RoutesList.tsx index 8aff345..3718a9b 100644 --- a/src/components/route-list/RoutesList.tsx +++ b/src/components/route-list/RoutesList.tsx @@ -27,6 +27,18 @@ const listRoutesQuery = { pageSize: 50, }, }, + authorities: { + resource: 'authorities', + params: { + fields: '*', + pageSize: -1, + }, + }, +} + +type Authority = { + id: string + name: string } const RoutesList = () => { @@ -58,6 +70,7 @@ const RoutesList = () => { critical: true, } ) + const updateFailAlert = useAlert( ({ error }) => i18n.t(`Failed to update route {{message}}`, { @@ -76,10 +89,13 @@ const RoutesList = () => { const engine = useDataEngine() - const { data: allRoutesList, refetch: refetchRoutesList } = - useDataQuery>( - listRoutesQuery - ) + const { data, refetch: refetchRoutesList } = useDataQuery< + WrapQueryResponse & + WrapQueryResponse + >(listRoutesQuery) + + const routes = data?.routes?.routes + const authorities = data?.authorities.systemAuthorities const handleDeleteRoute = async (routeCode: string) => { confirmDeleteAlert.show({ id: routeCode }) @@ -156,6 +172,7 @@ const RoutesList = () => { route={activeRoute} closeModal={onCloseCreateRouteModal} onSave={onSave} + authorities={authorities} /> )} @@ -180,7 +197,7 @@ const RoutesList = () => { = ({ )} - {route.authorities?.length ? ( -
-                                        {JSON.stringify(route.authorities)}
-                                    
- ) : ( - 'n/a' - )} +
    + {route.authorities?.map((auth) => { + return
  • {auth}
  • + })} +
& { id?: string } -export type WrapQueryResponse = { +export type Authority = { + id: string + name: string +} + +export type WrapQueryResponse< + T, + S extends string = 'result', + R extends string = S +> = { [K in S]: { - [K in S]: T + [K in R]: T } }