Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring session 2: Filters #36

Open
wants to merge 5 commits into
base: refactoring-session-2-base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion features/issues/components/filters/filters.styled.ts
@@ -1,5 +1,6 @@
import styled from "styled-components";
import { breakpoint } from "@styles/theme";
import { Select as UnstyledSelect, Input as UnstyledInput } from "@features/ui";

export const Container = styled.div`
display: flex;
Expand All @@ -12,7 +13,20 @@ export const Container = styled.div`
flex-direction: row;
justify-content: flex-end;
order: initial;
gap: 3rem;
flex-wrap: wrap;
}
`;

export const Select = styled(UnstyledSelect)`
width: 100%;
@media (min-width: ${breakpoint("desktop")}) {
width: 10rem;
}
`;

export const Input = styled(UnstyledInput)`
width: 100%;
@media (min-width: ${breakpoint("desktop")}) {
width: 17.5rem;
}
`;
128 changes: 34 additions & 94 deletions features/issues/components/filters/filters.tsx
@@ -1,45 +1,37 @@
import React, {
useState,
useEffect,
useCallback,
useRef,
useContext,
} from "react";
import { useRouter } from "next/router";
import { useWindowSize } from "react-use";
import { Select, Option, Input, NavigationContext } from "@features/ui";
import React, { useState } from "react";
import { useDebouncedCallback } from "use-debounce";
import { capitalize } from "lodash";
import { Option } from "@features/ui";
import { IssueFilters, IssueLevel, IssueStatus } from "@api/issues.types";
import { useFilters } from "../../hooks/use-filters";
import { IssueLevel, IssueStatus } from "@api/issues.types";
import { useProjects } from "@features/projects";
import * as S from "./filters.styled";

function getStatusDefaultValue(filters: IssueFilters) {
if (!filters.status) {
return "Status";
}
if (filters.status === IssueStatus.open) {
return "Unresolved";
}
return "Resolved";
}

function getLevelDefaultValue(filters: IssueFilters) {
if (!filters.level) {
return "Level";
}
return capitalize(filters.level);
}

export function Filters() {
const { handleFilters, filters } = useFilters();
const { data: projects } = useProjects();
const router = useRouter();
const routerQueryProjectName =
(router.query.projectName as string)?.toLowerCase() || undefined;
const [inputValue, setInputValue] = useState<string>("");
const projectNames = projects?.map((project) => project.name.toLowerCase());
const isFirst = useRef(true);
const { width } = useWindowSize();
const isMobileScreen = width <= 1023;
const { isMobileMenuOpen } = useContext(NavigationContext);
const handleChange = (input: string) => {
setInputValue(input);

if (inputValue?.length < 2) {
handleProjectName(undefined);
return;
}

const name = projectNames?.find((name) =>
name?.toLowerCase().includes(inputValue.toLowerCase())
);
const debouncedHandleFilters = useDebouncedCallback(handleFilters, 300);
const [inputValue, setInputValue] = useState(filters.project || "");

if (name) {
handleProjectName(name);
}
const handleChange = (project: string) => {
setInputValue(project);
debouncedHandleFilters({ project: project.toLowerCase() });
};

const handleLevel = (level?: string) => {
Expand All @@ -59,51 +51,11 @@ export function Filters() {
handleFilters({ status: status as IssueStatus });
};

const handleProjectName = useCallback(
(projectName?: string) =>
handleFilters({ project: projectName?.toLowerCase() }),
[handleFilters]
);

useEffect(() => {
const newObj: { [key: string]: string } = {
...filters,
};

Object.keys(newObj).forEach((key) => {
if (newObj[key] === undefined) {
delete newObj[key];
}
});

const url = {
pathname: router.pathname,
query: {
page: router.query.page || 1,
...newObj,
},
};

if (routerQueryProjectName && isFirst) {
handleProjectName(routerQueryProjectName);
setInputValue(routerQueryProjectName || "");
isFirst.current = false;
}

router.push(url, undefined, { shallow: false });
}, [filters.level, filters.status, filters.project, router.query.page]);

return (
<S.Container>
<Select
<S.Select
placeholder="Status"
defaultValue="Status"
width={isMobileScreen ? "97%" : "8rem"}
style={{
...(isMobileMenuOpen && {
opacity: 0,
}),
}}
defaultValue={getStatusDefaultValue(filters)}
>
<Option value={undefined} handleCallback={handleStatus}>
--None--
Expand All @@ -114,17 +66,11 @@ export function Filters() {
<Option value="Resolved" handleCallback={handleStatus}>
Resolved
</Option>
</Select>
</S.Select>

<Select
<S.Select
placeholder="Level"
defaultValue="Level"
width={isMobileScreen ? "97%" : "8rem"}
style={{
...(isMobileMenuOpen && {
opacity: 0,
}),
}}
defaultValue={getLevelDefaultValue(filters)}
>
<Option value={undefined} handleCallback={handleLevel}>
--None--
Expand All @@ -138,20 +84,14 @@ export function Filters() {
<Option value="Info" handleCallback={handleLevel}>
Info
</Option>
</Select>
</S.Select>

<Input
<S.Input
handleChange={handleChange}
value={inputValue}
label="project name"
placeholder="Project Name"
iconSrc="/icons/search-icon.svg"
style={{
...(isMobileScreen && { width: "94%", marginRight: "3rem" }),
...(isMobileMenuOpen && {
opacity: 0,
}),
}}
/>
</S.Container>
);
Expand Down
46 changes: 0 additions & 46 deletions features/issues/context/filters-context.tsx

This file was deleted.

1 change: 0 additions & 1 deletion features/issues/context/index.ts

This file was deleted.

21 changes: 18 additions & 3 deletions features/issues/hooks/use-filters.ts
@@ -1,4 +1,19 @@
import { useContext } from "react";
import { FiltersContext } from "../context/filters-context";
import { useRouter } from "next/router";
import { IssueFilters } from "@api/issues.types";

export const useFilters = () => useContext(FiltersContext);
export const useFilters = () => {
const router = useRouter();

const filters = {
status: router.query.status,
level: router.query.level,
project: router.query.project,
} as IssueFilters;

const handleFilters = (newFilters: IssueFilters) => {
const query = { ...router.query, ...newFilters };
router.push({ query });
};

return { filters, handleFilters };
};
1 change: 0 additions & 1 deletion features/issues/index.ts
@@ -1,3 +1,2 @@
export * from "./api";
export * from "./components/issue-list";
export * from "./context";
1 change: 1 addition & 0 deletions features/ui/input/input.styled.ts
Expand Up @@ -17,6 +17,7 @@ export const InputContainer = styled.input<{
border-radius: 7px;
width: calc(${space(20)} * 4 - ${space(6)});
padding: ${space(2, 3)};
box-sizing: border-box;
letter-spacing: 0.05rem;
color: ${color("gray", 900)};
${textFont("md", "regular")};
Expand Down
33 changes: 15 additions & 18 deletions features/ui/page-container/page-container.tsx
Expand Up @@ -3,7 +3,6 @@ import Head from "next/head";
import styled from "styled-components";
import { SidebarNavigation } from "../sidebar-navigation";
import { color, displayFont, textFont, space, breakpoint } from "@styles/theme";
import { FiltersProvider } from "@features/issues";

type PageContainerProps = {
children: React.ReactNode;
Expand Down Expand Up @@ -58,23 +57,21 @@ export function PageContainer({ children, title, info }: PageContainerProps) {
// "Warning: A title element received an array with more than 1 element as children."
const documentTitle = `ProLog - ${title}`;
return (
<FiltersProvider>
<Container>
<Head>
<title>{documentTitle}</title>
<meta name="description" content="Error monitoring" />
<link rel="icon" href="/favicon.ico" />
</Head>
<Container>
<Head>
<title>{documentTitle}</title>
<meta name="description" content="Error monitoring" />
<link rel="icon" href="/favicon.ico" />
</Head>

<SidebarNavigation />
<Main>
<ContentContainer>
<Title>{title}</Title>
<Info>{info}</Info>
{children}
</ContentContainer>
</Main>
</Container>
</FiltersProvider>
<SidebarNavigation />
<Main>
<ContentContainer>
<Title>{title}</Title>
<Info>{info}</Info>
{children}
</ContentContainer>
</Main>
</Container>
);
}
1 change: 1 addition & 0 deletions features/ui/sidebar-navigation/sidebar-navigation.tsx
Expand Up @@ -49,6 +49,7 @@ const Container = styled.div<{ isCollapsed: boolean }>`
const FixedContainer = styled.div`
${containerStyles}
position: fixed;
z-index: 1;
`;

const Header = styled.header`
Expand Down
20 changes: 19 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -30,7 +30,8 @@
"react-dom": "^18.2.0",
"react-use": "^17.4.0",
"styled-components": "^5.3.6",
"styled-normalize": "^8.0.7"
"styled-normalize": "^8.0.7",
"use-debounce": "^9.0.2"
},
"devDependencies": {
"@babel/core": "^7.17.5",
Expand Down