Merge pull request #411 from automatisch/issue-383
feat: perform remote search on flows
This commit is contained in:
@@ -15,7 +15,7 @@ const getFlows = async (_parent: unknown, params: Params, context: Context) => {
|
|||||||
.withGraphFetched('steps.[connection]')
|
.withGraphFetched('steps.[connection]')
|
||||||
.where((builder) => {
|
.where((builder) => {
|
||||||
if (params.name) {
|
if (params.name) {
|
||||||
builder.where('flows.name', 'like', `%${params.name}%`);
|
builder.where('flows.name', 'ilike', `%${params.name}%`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.appKey) {
|
if (params.appKey) {
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
import { gql } from '@apollo/client';
|
import { gql } from '@apollo/client';
|
||||||
|
|
||||||
export const GET_FLOWS = gql`
|
export const GET_FLOWS = gql`
|
||||||
query GetFlows($limit: Int!, $offset: Int!, $appKey: String) {
|
query GetFlows($limit: Int!, $offset: Int!, $appKey: String, $name: String) {
|
||||||
getFlows(limit: $limit, offset: $offset, appKey: $appKey) {
|
getFlows(limit: $limit, offset: $offset, appKey: $appKey, name: $name) {
|
||||||
pageInfo {
|
pageInfo {
|
||||||
currentPage
|
currentPage
|
||||||
totalPages
|
totalPages
|
||||||
|
@@ -1,10 +1,12 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { Link, useSearchParams } from 'react-router-dom';
|
import { Link, useSearchParams } from 'react-router-dom';
|
||||||
import type { LinkProps } from 'react-router-dom';
|
import type { LinkProps } from 'react-router-dom';
|
||||||
import { useQuery } from '@apollo/client';
|
import { useLazyQuery } from '@apollo/client';
|
||||||
|
import debounce from 'lodash/debounce';
|
||||||
import Box from '@mui/material/Box';
|
import Box from '@mui/material/Box';
|
||||||
import Grid from '@mui/material/Grid';
|
import Grid from '@mui/material/Grid';
|
||||||
import AddIcon from '@mui/icons-material/Add';
|
import AddIcon from '@mui/icons-material/Add';
|
||||||
|
import CircularProgress from '@mui/material/CircularProgress';
|
||||||
import Pagination from '@mui/material/Pagination';
|
import Pagination from '@mui/material/Pagination';
|
||||||
import PaginationItem from '@mui/material/PaginationItem';
|
import PaginationItem from '@mui/material/PaginationItem';
|
||||||
import type { IFlow } from '@automatisch/types';
|
import type { IFlow } from '@automatisch/types';
|
||||||
@@ -30,17 +32,44 @@ export default function Flows(): React.ReactElement {
|
|||||||
const [searchParams, setSearchParams] = useSearchParams();
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
const page = parseInt(searchParams.get('page') || '', 10) || 1;
|
const page = parseInt(searchParams.get('page') || '', 10) || 1;
|
||||||
const [flowName, setFlowName] = React.useState('');
|
const [flowName, setFlowName] = React.useState('');
|
||||||
const { data } = useQuery(GET_FLOWS, {
|
const [loading, setLoading] = React.useState(false);
|
||||||
variables: getLimitAndOffset(page),
|
const [getFlows, { data }] = useLazyQuery(GET_FLOWS, {
|
||||||
fetchPolicy: 'cache-and-network',
|
onCompleted: () => { setLoading(false); },
|
||||||
});
|
});
|
||||||
|
|
||||||
const getFlows = data?.getFlows || {};
|
const fetchData = React.useMemo(
|
||||||
const { pageInfo, edges } = getFlows;
|
() => debounce((name) => getFlows(
|
||||||
|
{
|
||||||
|
variables: {
|
||||||
|
...getLimitAndOffset(page),
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
300
|
||||||
|
),
|
||||||
|
[page, getFlows]
|
||||||
|
);
|
||||||
|
|
||||||
const flows: IFlow[] = edges
|
React.useEffect(function fetchFlowsOnSearch() {
|
||||||
?.map(({ node }: { node: IFlow }) => node)
|
setLoading(true);
|
||||||
.filter((flow: IFlow) => flow.name?.toLowerCase().includes(flowName.toLowerCase()));
|
|
||||||
|
fetchData(flowName);
|
||||||
|
}, [fetchData, flowName]);
|
||||||
|
|
||||||
|
React.useEffect(function resetPageOnSearch() {
|
||||||
|
// reset search params which only consists of `page`
|
||||||
|
setSearchParams({})
|
||||||
|
}, [flowName]);
|
||||||
|
|
||||||
|
React.useEffect(function cancelDebounceOnUnmount() {
|
||||||
|
return () => {
|
||||||
|
fetchData.cancel();
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const { pageInfo, edges } = data?.getFlows || {};
|
||||||
|
|
||||||
|
const flows: IFlow[] = edges?.map(({ node }: { node: IFlow }) => node);
|
||||||
|
|
||||||
const onSearchChange = React.useCallback((event) => {
|
const onSearchChange = React.useCallback((event) => {
|
||||||
setFlowName(event.target.value);
|
setFlowName(event.target.value);
|
||||||
@@ -84,9 +113,11 @@ export default function Flows(): React.ReactElement {
|
|||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
{flows?.map((flow) => (<FlowRow key={flow.id} flow={flow} />))}
|
{loading && <CircularProgress sx={{ display: 'block', margin: '20px auto' }} />}
|
||||||
|
|
||||||
{pageInfo && pageInfo.totalPages > 1 && <Pagination
|
{!loading && flows?.map((flow) => (<FlowRow key={flow.id} flow={flow} />))}
|
||||||
|
|
||||||
|
{!loading && pageInfo && pageInfo.totalPages > 1 && <Pagination
|
||||||
sx={{ display: 'flex', justifyContent: 'center', mt: 3 }}
|
sx={{ display: 'flex', justifyContent: 'center', mt: 3 }}
|
||||||
page={pageInfo?.currentPage}
|
page={pageInfo?.currentPage}
|
||||||
count={pageInfo?.totalPages}
|
count={pageInfo?.totalPages}
|
||||||
|
Reference in New Issue
Block a user