Skip to content

Commit

Permalink
route filtering
Browse files Browse the repository at this point in the history
- add the same kind of arguments to the route resource
- move some buttons to a drop down menu as the list was getting crowded
-
  • Loading branch information
TurtIeSocks committed Apr 24, 2023
1 parent 5df69fe commit 3a95683
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 33 deletions.
43 changes: 43 additions & 0 deletions client/src/pages/admin/actions/Extras.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as React from 'react'
import { DeleteWithUndoButton } from 'react-admin'
import { IconButton, Menu, MenuItem } from '@mui/material'
import MoreVertIcon from '@mui/icons-material/MoreVert'

import { ExportButton } from './Export'
import { PushToProd } from './PushToApi'

export function ExtraMenuActions({ resource }: { resource: string }) {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null)

const handleClose = (e: React.MouseEvent) => {
e.stopPropagation()
setAnchorEl(null)
}

return (
<>
<IconButton
onClick={(e) => {
e.stopPropagation()
setAnchorEl(e.currentTarget)
}}
>
<MoreVertIcon />
</IconButton>
<Menu open={!!anchorEl} anchorEl={anchorEl} onClose={handleClose}>
<MenuItem onClick={handleClose}>
<DeleteWithUndoButton />
</MenuItem>
<MenuItem onClick={handleClose}>
<ExportButton resource={resource} />
</MenuItem>
<MenuItem
onClick={handleClose}
sx={{ display: { xs: 'flex', sm: 'none' } }}
>
<PushToProd resource={resource} />
</MenuItem>
</Menu>
</>
)
}
12 changes: 10 additions & 2 deletions client/src/pages/admin/actions/PushToApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@ import {
import { useMutation } from 'react-query'

import type { BasicKojiEntry } from '@assets/types'
import { capitalize } from '@mui/material'
import { SxProps, capitalize } from '@mui/material'
import { fetchWrapper } from '@services/fetches'

export function BaseButton({
onClick,
sx,
}: {
onClick: React.MouseEventHandler<HTMLButtonElement> | undefined
sx?: SxProps
}) {
return (
<Button label="Sync" size="small" onClick={onClick}>
<Button label="Sync" size="small" onClick={onClick} sx={sx}>
<SyncIcon />
</Button>
)
}

export function PushToProd<T extends BasicKojiEntry>({
resource,
sx,
}: {
resource: string
sx?: SxProps
}) {
const record = useRecordContext<T>()
const notify = useNotify()
Expand All @@ -52,6 +56,7 @@ export function PushToProd<T extends BasicKojiEntry>({

return (
<BaseButton
sx={sx}
onClick={(event) => {
event.stopPropagation()
sync.mutate()
Expand All @@ -62,8 +67,10 @@ export function PushToProd<T extends BasicKojiEntry>({

export function BulkPushToProd<T extends BasicKojiEntry>({
resource,
sx,
}: {
resource: string
sx?: SxProps
}) {
const { selectedIds } = useListContext<T>()
const unselectAll = useUnselectAll(resource)
Expand Down Expand Up @@ -95,6 +102,7 @@ export function BulkPushToProd<T extends BasicKojiEntry>({

return (
<BaseButton
sx={sx}
onClick={(event) => {
event.stopPropagation()
unselectAll()
Expand Down
10 changes: 3 additions & 7 deletions client/src/pages/admin/geofence/GeofenceFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function GeofenceFilter() {
),
)
return (
<Card sx={{ order: -1, mt: 9, width: 200 }}>
<Card sx={{ order: -1, width: 200 }}>
<CardContent>
{/* <SavedQueriesList /> */}
<FilterLiveSearch />
Expand All @@ -51,12 +51,8 @@ export function GeofenceFilter() {
))}
</FilterList>
<FilterList label="Geography Type" icon={<MapIcon />}>
{['Polygon', 'MultiPolygon'].map((geo_type) => (
<FilterListItem
key={geo_type}
label={geo_type}
value={{ geo_type }}
/>
{['Polygon', 'MultiPolygon'].map((geotype) => (
<FilterListItem key={geotype} label={geotype} value={{ geotype }} />
))}
</FilterList>
<FilterList label="Mode" icon={<AutoModeIcon />}>
Expand Down
7 changes: 3 additions & 4 deletions client/src/pages/admin/geofence/GeofenceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from 'react'
import {
BulkDeleteWithUndoButton,
Datagrid,
DeleteWithUndoButton,
EditButton,
List,
Pagination,
Expand All @@ -15,10 +14,11 @@ import {
import { ExportPolygon } from '@components/dialogs/Polygon'
import { GeofenceFilter } from './GeofenceFilter'
import { BulkAssignButton } from '../actions/AssignProjectFence'
import { BulkExportButton, ExportButton } from '../actions/Export'
import { BulkExportButton } from '../actions/Export'
import { BulkPushToProd, PushToProd } from '../actions/PushToApi'
import { GeofenceExpand } from './GeofenceExpand'
import { BulkAssignFenceButton } from '../actions/AssignParentFence'
import { ExtraMenuActions } from '../actions/Extras'

function ListActions() {
return (
Expand Down Expand Up @@ -61,9 +61,8 @@ export default function GeofenceList() {
<TextField source="mode" />
<TextField source="geo_type" />
<EditButton />
<DeleteWithUndoButton />
<PushToProd resource="geofence" />
<ExportButton resource="geofence" />
<ExtraMenuActions resource="geofence" />
</Datagrid>
</List>
<ExportPolygon />
Expand Down
53 changes: 53 additions & 0 deletions client/src/pages/admin/route/RouteFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable import/no-extraneous-dependencies */
import * as React from 'react'
import {
// SavedQueriesList,
FilterLiveSearch,
FilterList,
FilterListItem,
} from 'react-admin'
import { useQuery } from 'react-query'
import { Card, CardContent } from '@mui/material'
import AutoModeIcon from '@mui/icons-material/AutoMode'
import SupervisedUserCircleIcon from '@mui/icons-material/SupervisedUserCircle'

import { useStatic } from '@hooks/useStatic'
import { RDM_ROUTES, UNOWN_ROUTES } from '@assets/constants'
import { BasicKojiEntry, KojiResponse } from '@assets/types'
import { fetchWrapper } from '@services/fetches'

export function RouteFilter() {
const { scannerType } = useStatic.getState()
const { data } = useQuery('unique_geofences', () =>
fetchWrapper<KojiResponse<BasicKojiEntry[]>>(
'/internal/admin/route/parent',
),
)
return (
<Card sx={{ order: -1, width: 225 }}>
<CardContent>
{/* <SavedQueriesList /> */}
<FilterLiveSearch />
<FilterList label="Mode" icon={<AutoModeIcon />}>
{[
...(scannerType === 'rdm' ? RDM_ROUTES : UNOWN_ROUTES),
'unset',
].map((mode) => (
<FilterListItem key={mode} label={mode} value={{ mode }} />
))}
</FilterList>
<FilterList label="Geofence" icon={<SupervisedUserCircleIcon />}>
<div style={{ maxHeight: 400, overflow: 'auto' }}>
{(data?.data || []).map((fence) => (
<FilterListItem
key={fence.id}
label={fence.name}
value={{ geofenceid: fence.id }}
/>
))}
</div>
</FilterList>
</CardContent>
</Card>
)
}
18 changes: 10 additions & 8 deletions client/src/pages/admin/route/RouteList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from 'react'
import {
BulkDeleteWithUndoButton,
Datagrid,
DeleteWithUndoButton,
EditButton,
List,
NumberField,
Expand All @@ -11,12 +10,13 @@ import {
TopToolbar,
CreateButton,
ReferenceField,
SearchInput,
} from 'react-admin'
import { ExportPolygon } from '@components/dialogs/Polygon'

import { BulkExportButton, ExportButton } from '../actions/Export'
import { BulkExportButton } from '../actions/Export'
import { BulkPushToProd, PushToProd } from '../actions/PushToApi'
import { RouteFilter } from './RouteFilter'
import { ExtraMenuActions } from '../actions/Extras'

function ListActions() {
return (
Expand All @@ -29,8 +29,8 @@ function ListActions() {
function BulkActions() {
return (
<>
<BulkDeleteWithUndoButton resource="route" />
<BulkPushToProd resource="route" />
<BulkDeleteWithUndoButton resource="route" size="small" />
<BulkExportButton resource="route" />
</>
)
Expand All @@ -40,7 +40,7 @@ export default function RouteList() {
return (
<>
<List
filters={[<SearchInput source="q" alwaysOn />]}
aside={<RouteFilter />}
pagination={<Pagination rowsPerPageOptions={[25, 50, 100]} />}
title="Routes"
perPage={25}
Expand All @@ -54,9 +54,11 @@ export default function RouteList() {
<ReferenceField source="geofence_id" reference="geofence" />
<NumberField source="hops" label="Hops" sortable={false} />
<EditButton />
<DeleteWithUndoButton />
<PushToProd resource="route" />
<ExportButton resource="route" />
<PushToProd
resource="route"
sx={{ display: { xs: 'none', sm: 'flex' } }}
/>
<ExtraMenuActions resource="route" />
</Datagrid>
</List>
<ExportPolygon />
Expand Down
18 changes: 13 additions & 5 deletions server/api/src/private/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ async fn paginate(
}))
}

#[get("/geofence/parent")]
async fn parent_list(db: web::Data<KojiDb>) -> Result<HttpResponse, Error> {
let results = db::geofence::Query::unique_parents(&db.koji_db)
.await
.map_err(actix_web::error::ErrorInternalServerError)?;
#[get("/{resource}/parent")]
async fn parent_list(
db: web::Data<KojiDb>,
path: actix_web::web::Path<String>,
) -> Result<HttpResponse, Error> {
let resource = path.into_inner();

let results = match resource.to_lowercase().as_str() {
"geofence" => db::geofence::Query::unique_parents(&db.koji_db).await,
"route" => db::route::Query::unique_geofence(&db.koji_db).await,
_ => Err(ModelError::Custom("Invalid Resource".to_string())),
}
.map_err(actix_web::error::ErrorInternalServerError)?;

Ok(HttpResponse::Ok().json(Response {
data: Some(json!(results)),
Expand Down
9 changes: 6 additions & 3 deletions server/model/src/api/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,11 @@ pub struct AdminReq {
pub sort_by: Option<String>,
pub order: Option<String>,
pub search: Option<String>,
pub geo_type: Option<String>,
pub geotype: Option<String>,
pub project: Option<u32>,
pub mode: Option<String>,
pub parent: Option<u32>,
pub geofenceid: Option<u32>,
}

#[derive(Debug, Deserialize)]
Expand All @@ -617,10 +618,11 @@ impl AdminReq {
per_page: self.per_page.unwrap_or(25),
sort_by: self.sort_by.unwrap_or("id".to_string()),
q: self.search.unwrap_or("".to_string()),
geo_type: self.geo_type,
geotype: self.geotype,
project: self.project,
mode: self.mode,
parent: self.parent,
geofenceid: self.geofenceid,
}
}
}
Expand All @@ -631,8 +633,9 @@ pub struct AdminReqParsed {
pub sort_by: String,
pub order: String,
pub q: String,
pub geo_type: Option<String>,
pub geotype: Option<String>,
pub project: Option<u32>,
pub mode: Option<String>,
pub parent: Option<u32>,
pub geofenceid: Option<u32>,
}
3 changes: 2 additions & 1 deletion server/model/src/db/geofence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ impl Query {
if let Some(parent) = args.parent {
paginator = paginator.filter(Column::Parent.eq(parent));
}
if let Some(geo_type) = args.geo_type {

if let Some(geo_type) = args.geotype {
paginator = paginator.filter(Column::GeoType.eq(geo_type));
}
if let Some(mode) = args.mode {
Expand Down
Loading

0 comments on commit 3a95683

Please sign in to comment.