From 7c48533571e9f9d3731a59433a56cc8d6e008123 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sat, 12 Feb 2022 10:59:19 +0100 Subject: Added search and filter options --- src/components/RequestList/RequestList.module.scss | 20 ++ src/components/RequestList/RequestList.tsx | 203 +++++++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 src/components/RequestList/RequestList.module.scss create mode 100644 src/components/RequestList/RequestList.tsx (limited to 'src/components/RequestList') diff --git a/src/components/RequestList/RequestList.module.scss b/src/components/RequestList/RequestList.module.scss new file mode 100644 index 0000000..47f36c8 --- /dev/null +++ b/src/components/RequestList/RequestList.module.scss @@ -0,0 +1,20 @@ +.noRequest { + width: 100%; + height: 100%; + display: flex; + justify-content: center; + align-items: center; +} + +.list { + //height: 100%; + overflow-y: auto; +} + +.listContainer { + height: 100%; + overflow-y: hidden; + display: flex; + flex-flow: column nowrap; + +} diff --git a/src/components/RequestList/RequestList.tsx b/src/components/RequestList/RequestList.tsx new file mode 100644 index 0000000..95c5b75 --- /dev/null +++ b/src/components/RequestList/RequestList.tsx @@ -0,0 +1,203 @@ +import { + Button, + Dropdown, + DropdownButton, + Form, + InputGroup, + ListGroup, + Offcanvas, +} from "react-bootstrap"; +import classNames from "classnames"; +import styles from "./RequestList.module.scss"; +import RequestSummary from "../RequestSummary/RequestSummary"; +import * as React from "react"; +import {useCallback, useContext, useMemo, useState} from "react"; +import {Method, RequestResponse} from "../../hooks/useRequests"; +import {DarkModeContext} from "../../contexts/DarkMode"; +import {Filter} from "../Icons/Filter"; + +interface ListProps { + requests: RequestResponse[], + selectedRequestIndex: number | null + setSelectedRequestIndex: (index: number) => void +} + +type EnabledMethods = { + [method in Method]: boolean +} + + +export default function RequestList({ requests, selectedRequestIndex, setSelectedRequestIndex }: ListProps) { + const {darkMode} = useContext(DarkModeContext) + const [showFilterOptions, setShowFilterOptions] = useState(false); + const [search, setSearch] = useState(''); + const [enableRegex, setEnableRegex] = useState(false); + + const [methods, setMethods] = useState({ + GET: true, + POST: true, + PUT: true, + PATCH: true, + DELETE: true, + }); + + const toggleMethods = useCallback((method: Method | null) => { + const enabled = method == null; + const methods = { + GET: enabled, + POST: enabled, + PUT: enabled, + PATCH: enabled, + DELETE: enabled, + } + + if (!enabled) { + methods[method] = true; + } + + setMethods(methods); + }, []); + + const enabledMethods: Method[] = useMemo(() => (Object + .entries(methods) + .filter(([method, enabled]) => enabled) + .map(([method]) => method) + ), [methods]); + + const methodLabel = useMemo(() => { + if (enabledMethods.length == 1) { + return enabledMethods[0] + } else if (enabledMethods.length === 5) { + return 'ANY' + } else { + return 'CUSTOM' + } + }, [enabledMethods]); + + + + const filteredRequests = useMemo(() => { + let searchRegex = new RegExp(''); + try { + searchRegex = new RegExp( + enableRegex + ? search + : search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), + 'i'); + } catch { + + } + + return ( + ( + requests + .map((request, index) => [index, request]) + .reverse() as [number, RequestResponse][] + ) + .filter(([index, request]) => ( + enabledMethods.length > 0 === null || enabledMethods.includes(request.request.method) + )) + .filter(([index, request]) => ( + search === '' || searchRegex.test(`${request.request.method} ${request.request.path}`) + )) + ) + }, [requests, search, enabledMethods, enableRegex]) + + return ( +
+ + + + + toggleMethods(null)}>ANY + + toggleMethods('GET')}>GET + toggleMethods('POST')}>POST + toggleMethods('PUT')}>PUT + toggleMethods('PATCH')}>PATCH + toggleMethods('DELETE')}>DELETE + + setSearch(target.value)}/> + + + + + + { + filteredRequests.length > 0 + ? ( + filteredRequests.map(([index, requestResponse]) => { + const selected = selectedRequestIndex === index; + return ( + setSelectedRequestIndex(index)} + key={`request-${index}`} + className={classNames({ + 'bg-primary': selected, + 'text-light': selected, + 'border-bottom': true + })} + > + + + ) + }) + ) + : ( +
+

No requests

+
+ ) + } +
+ setShowFilterOptions(false)}> + + + Filter Options + + + + + Search + setSearch(target.value)}/> + setEnableRegex(!enableRegex)} /> + + + Method + {Object.entries(methods).map(([method, enabled]) => ( + setMethods({...methods, [method]: !enabled})} /> + ))} + + + +
+ ) +} -- cgit v1.2.3