Browse Source

setUrlQueryParams

redux
YuJian920 3 years ago
parent
commit
716f9b577b
  1. 26
      src/hook/useUrlQueryParams.ts
  2. 8
      src/pages/Home/index.tsx
  3. 7
      src/pages/Project/Kanban/index.tsx
  4. 7
      src/pages/Project/Task/index.tsx
  5. 26
      src/pages/Project/index.tsx
  6. 2
      src/pages/ProjectList/Search/index.tsx
  7. 3
      src/pages/ProjectList/index.tsx
  8. 6
      src/utils/index.ts

26
src/hook/useUrlQueryParams.ts

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
import { useMemo } from "react";
import { URLSearchParamsInit, useSearchParams } from "react-router-dom";
import { cleanObject } from "../utils";
const useUrlQueryParams = <K extends string>(keys: K[]) => {
const [searchParams, setSearchParams] = useSearchParams();
return [
useMemo(
() =>
keys.reduce(
(prev, key) => ({
...prev,
[key]: searchParams.get(key) || "",
}),
{} as { [key in K]: string }
),
[searchParams]
),
(params: Partial<{ [key in K]: unknown }>) => {
const newParams = cleanObject({ ...Object.fromEntries(searchParams), ...params }) as URLSearchParamsInit
setSearchParams(newParams)
},
] as const;
};
export default useUrlQueryParams;

8
src/pages/Home/index.tsx

@ -1,12 +1,13 @@ @@ -1,12 +1,13 @@
import React from "react";
import { Dropdown, Menu, Button } from "antd";
import { Navigate, Route, Routes } from "react-router";
import { BrowserRouter } from 'react-router-dom'
import { BrowserRouter } from "react-router-dom";
import ProjectList from "../ProjectList";
import Project from "../Project";
import { useAuth } from "../../context/auth-context";
import { ReactComponent as SoftwareLogo } from "../../assets/software-logo.svg";
import { Main, Container, Header, HeaderLeft, HeaderRight } from "./style";
import { resetRoute } from "../../utils";
const Home = () => {
return (
@ -17,6 +18,7 @@ const Home = () => { @@ -17,6 +18,7 @@ const Home = () => {
<Routes>
<Route path="/projects" element={<ProjectList />} />
<Route path="/projects/:projectId/*" element={<Project />} />
<Route path="*" element={<Navigate to="/projects" />} />
</Routes>
</BrowserRouter>
</Main>
@ -29,7 +31,9 @@ const PageHeader = () => { @@ -29,7 +31,9 @@ const PageHeader = () => {
return (
<Header between={true}>
<HeaderLeft gap={true}>
<SoftwareLogo width="18rem" color="rgb(38, 132, 255)" />
<Button type="link" onClick={resetRoute}>
<SoftwareLogo width="18rem" color="rgb(38, 132, 255)" />
</Button>
<h2></h2>
<h2></h2>
</HeaderLeft>

7
src/pages/Project/Kanban/index.tsx

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
import React from "react";
const Kanban = () => {
return <div>Kanban</div>;
};
export default Kanban;

7
src/pages/Project/Task/index.tsx

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
import React from "react";
const Task = () => {
return <div>Task</div>;
};
export default Task;

26
src/pages/Project/index.tsx

@ -1,7 +1,25 @@ @@ -1,7 +1,25 @@
import React from 'react'
import React from "react";
import { Routes, Route, Navigate } from "react-router";
import { Link } from "react-router-dom";
import Kanban from "./Kanban";
import Task from "./Task";
const Project = () => {
return <div>Project</div>
}
return (
<div>
<h1>Project</h1>
<Link to="kanban"></Link>
<Link to="task"></Link>
<Routes>
<Route path="/kanban" element={<Kanban />} />
<Route path="/task" element={<Task />} />
<Route
path="*"
element={<Navigate to={window.location.pathname + "/kanban"} />}
/>
</Routes>
</div>
);
};
export default Project
export default Project;

2
src/pages/ProjectList/Search/index.tsx

@ -39,7 +39,7 @@ const Search = ({ param, setParam, users }: SearchPanelProps) => { @@ -39,7 +39,7 @@ const Search = ({ param, setParam, users }: SearchPanelProps) => {
>
<Option value={""}></Option>
{users.map((user) => (
<Option value={user.id} key={user.id}>
<Option value={String(user.id)} key={user.id}>
{user.name}
</Option>
))}

3
src/pages/ProjectList/index.tsx

@ -7,6 +7,7 @@ import { Container } from "./style"; @@ -7,6 +7,7 @@ import { Container } from "./style";
import useProjects from "../../hook/useProjects";
import useUsers from "../../hook/useUsers";
import useDocumentTitle from "../../hook/useDocumentTitle";
import useUrlQueryParams from "../../hook/useUrlQueryParams";
interface Project {
id: string;
@ -18,7 +19,7 @@ interface Project { @@ -18,7 +19,7 @@ interface Project {
}
const ProjectList = () => {
const [param, setParam] = useState({ name: "", personId: "" });
const [param, setParam] = useUrlQueryParams(["name", "personId"]);
const debounceParams = useDebounce(param, 200);
const { isLoading, error, data: list } = useProjects(debounceParams);
const { data: users } = useUsers();

6
src/utils/index.ts

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
export const isFalsy = (value: unknown) => (value === 0 ? false : !value);
export const isVoid = (value: unknown) => value === undefined || value === null || value === '';
export const isVoid = (value: unknown) => value === undefined || value === null || value === "";
// 筛选对象空值
export const cleanObject = (object: { [key: string]: unknown }) => {
@ -12,4 +12,6 @@ export const cleanObject = (object: { [key: string]: unknown }) => { @@ -12,4 +12,6 @@ export const cleanObject = (object: { [key: string]: unknown }) => {
});
return result;
};
};
export const resetRoute = () => (window.location.href = window.location.origin);

Loading…
Cancel
Save