线上课列表、学员列表数据存储到url上

This commit is contained in:
unknown 2023-09-22 15:04:22 +08:00
parent 6715d8cf50
commit f580bc5ac7
2 changed files with 124 additions and 33 deletions

View File

@ -20,7 +20,7 @@ import {
import type { MenuProps } from "antd"; import type { MenuProps } from "antd";
import type { ColumnsType } from "antd/es/table"; import type { ColumnsType } from "antd/es/table";
import { dateFormat } from "../../utils/index"; import { dateFormat } from "../../utils/index";
import { useNavigate, useLocation } from "react-router-dom"; import { useNavigate, useLocation, useSearchParams } from "react-router-dom";
import { TreeDepartment, TreeCategory, PerButton } from "../../compenents"; import { TreeDepartment, TreeCategory, PerButton } from "../../compenents";
import type { TabsProps } from "antd"; import type { TabsProps } from "antd";
import { CourseCreate } from "./compenents/create"; import { CourseCreate } from "./compenents/create";
@ -42,17 +42,30 @@ interface DataType {
title: string; title: string;
} }
interface LocalSearchParamsInterface {
page?: number;
size?: number;
title?: string;
}
const CoursePage = () => { const CoursePage = () => {
const result = new URLSearchParams(useLocation().search); const result = new URLSearchParams(useLocation().search);
const [searchParams, setSearchParams] = useSearchParams({
page: "1",
size: "10",
title: "",
});
const page = parseInt(searchParams.get("page") || "1");
const size = parseInt(searchParams.get("size") || "10");
const title = searchParams.get("title");
const navigate = useNavigate(); const navigate = useNavigate();
const [list, setList] = useState<DataType[]>([]); const [list, setList] = useState<DataType[]>([]);
const [refresh, setRefresh] = useState(false);
const [page, setPage] = useState(1);
const [size, setSize] = useState(10);
const [total, setTotal] = useState(0); const [total, setTotal] = useState(0);
const [refresh, setRefresh] = useState(false);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [category_ids, setCategoryIds] = useState<number[]>([]); const [category_ids, setCategoryIds] = useState<number[]>([]);
const [title, setTitle] = useState("");
const [dep_ids, setDepIds] = useState<number[]>([]); const [dep_ids, setDepIds] = useState<number[]>([]);
const [selLabel, setLabel] = useState<string>( const [selLabel, setLabel] = useState<string>(
result.get("label") ? String(result.get("label")) : "全部分类" result.get("label") ? String(result.get("label")) : "全部分类"
@ -105,7 +118,9 @@ const CoursePage = () => {
type="" type=""
text={"分类"} text={"分类"}
onUpdate={(keys: any, title: any) => { onUpdate={(keys: any, title: any) => {
setPage(1); resetLocalSearchParams({
page: 1,
});
setCategoryIds(keys); setCategoryIds(keys);
if (typeof title === "string") { if (typeof title === "string") {
setLabel(title); setLabel(title);
@ -129,7 +144,9 @@ const CoursePage = () => {
type="no-course" type="no-course"
text={"部门"} text={"部门"}
onUpdate={(keys: any, title: any) => { onUpdate={(keys: any, title: any) => {
setPage(1); resetLocalSearchParams({
page: 1,
});
setDepIds(keys); setDepIds(keys);
setDepLabel(title); setDepLabel(title);
}} }}
@ -346,7 +363,7 @@ const CoursePage = () => {
depIds = dep_ids.join(","); depIds = dep_ids.join(",");
} }
course course
.courseList(page, size, "", "", title, depIds, categoryIds) .courseList(page, size, "", "", title ? title : "", depIds, categoryIds)
.then((res: any) => { .then((res: any) => {
setTotal(res.data.total); setTotal(res.data.total);
setList(res.data.data); setList(res.data.data);
@ -362,10 +379,12 @@ const CoursePage = () => {
}; };
// 重置列表 // 重置列表
const resetList = () => { const resetList = () => {
setPage(1); resetLocalSearchParams({
setSize(10); page: 1,
size: 10,
title: "",
});
setList([]); setList([]);
setTitle("");
setRefresh(!refresh); setRefresh(!refresh);
}; };
@ -379,8 +398,28 @@ const CoursePage = () => {
}; };
const handlePageChange = (page: number, pageSize: number) => { const handlePageChange = (page: number, pageSize: number) => {
setPage(page); resetLocalSearchParams({
setSize(pageSize); page: page,
size: pageSize,
});
};
const resetLocalSearchParams = (params: LocalSearchParamsInterface) => {
setSearchParams(
(prev) => {
if (typeof params.title !== "undefined") {
prev.set("title", params.title);
}
if (typeof params.page !== "undefined") {
prev.set("page", params.page + "");
}
if (typeof params.size !== "undefined") {
prev.set("size", params.size + "");
}
return prev;
},
{ replace: true }
);
}; };
const onChange = (key: string) => { const onChange = (key: string) => {
@ -419,9 +458,11 @@ const CoursePage = () => {
<div className="d-flex mr-24"> <div className="d-flex mr-24">
<Typography.Text></Typography.Text> <Typography.Text></Typography.Text>
<Input <Input
value={title} value={title || ""}
onChange={(e) => { onChange={(e) => {
setTitle(e.target.value); resetLocalSearchParams({
title: e.target.value,
});
}} }}
allowClear allowClear
style={{ width: 160 }} style={{ width: 160 }}
@ -435,7 +476,9 @@ const CoursePage = () => {
<Button <Button
type="primary" type="primary"
onClick={() => { onClick={() => {
setPage(1); resetLocalSearchParams({
page: 1,
});
setRefresh(!refresh); setRefresh(!refresh);
}} }}
> >

View File

@ -20,7 +20,7 @@ import {
} from "@ant-design/icons"; } from "@ant-design/icons";
import { user } from "../../api/index"; import { user } from "../../api/index";
import { dateFormat } from "../../utils/index"; import { dateFormat } from "../../utils/index";
import { Link, Navigate, useLocation } from "react-router-dom"; import { Link, useLocation, useSearchParams } from "react-router-dom";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { TreeDepartment, PerButton } from "../../compenents"; import { TreeDepartment, PerButton } from "../../compenents";
import { MemberCreate } from "./compenents/create"; import { MemberCreate } from "./compenents/create";
@ -46,17 +46,32 @@ interface DataType {
verify_at?: string; verify_at?: string;
} }
interface LocalSearchParamsInterface {
page?: number;
size?: number;
nickname?: string;
email?: string;
}
const MemberPage = () => { const MemberPage = () => {
const result = new URLSearchParams(useLocation().search); const result = new URLSearchParams(useLocation().search);
const [searchParams, setSearchParams] = useSearchParams({
page: "1",
size: "10",
nickname: "",
email: "",
});
const page = parseInt(searchParams.get("page") || "1");
const size = parseInt(searchParams.get("size") || "10");
const nickname = searchParams.get("nickname");
const email = searchParams.get("email");
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [page, setPage] = useState(1);
const [size, setSize] = useState(10);
const [list, setList] = useState<DataType[]>([]); const [list, setList] = useState<DataType[]>([]);
const [total, setTotal] = useState(0); const [total, setTotal] = useState(0);
const [refresh, setRefresh] = useState(false); const [refresh, setRefresh] = useState(false);
const [nickname, setNickname] = useState("");
const [email, setEmail] = useState("");
const [dep_ids, setDepIds] = useState<number[]>([]); const [dep_ids, setDepIds] = useState<number[]>([]);
const [selLabel, setLabel] = useState<string>( const [selLabel, setLabel] = useState<string>(
result.get("label") ? String(result.get("label")) : "全部部门" result.get("label") ? String(result.get("label")) : "全部部门"
@ -231,10 +246,12 @@ const MemberPage = () => {
}; };
const resetData = () => { const resetData = () => {
setNickname(""); resetLocalSearchParams({
setEmail(""); page: 1,
setPage(1); size: 10,
setSize(10); nickname: "",
email: "",
});
setList([]); setList([]);
setRefresh(!refresh); setRefresh(!refresh);
}; };
@ -249,8 +266,31 @@ const MemberPage = () => {
}; };
const handlePageChange = (page: number, pageSize: number) => { const handlePageChange = (page: number, pageSize: number) => {
setPage(page); resetLocalSearchParams({
setSize(pageSize); page: page,
size: pageSize,
});
};
const resetLocalSearchParams = (params: LocalSearchParamsInterface) => {
setSearchParams(
(prev) => {
if (typeof params.nickname !== "undefined") {
prev.set("nickname", params.nickname);
}
if (typeof params.email !== "undefined") {
prev.set("email", params.email);
}
if (typeof params.page !== "undefined") {
prev.set("page", params.page + "");
}
if (typeof params.size !== "undefined") {
prev.set("size", params.size + "");
}
return prev;
},
{ replace: true }
);
}; };
const delUser = (id: number) => { const delUser = (id: number) => {
@ -287,7 +327,9 @@ const MemberPage = () => {
type="" type=""
text={"部门"} text={"部门"}
onUpdate={(keys: any, title: any) => { onUpdate={(keys: any, title: any) => {
setPage(1); resetLocalSearchParams({
page: 1,
});
setDepIds(keys); setDepIds(keys);
var index = title.indexOf("("); var index = title.indexOf("(");
if (index !== -1) { if (index !== -1) {
@ -352,9 +394,11 @@ const MemberPage = () => {
<div className="d-flex mr-24"> <div className="d-flex mr-24">
<Typography.Text></Typography.Text> <Typography.Text></Typography.Text>
<Input <Input
value={nickname} value={nickname || ""}
onChange={(e) => { onChange={(e) => {
setNickname(e.target.value); resetLocalSearchParams({
nickname: e.target.value,
});
}} }}
style={{ width: 160 }} style={{ width: 160 }}
placeholder="请输入姓名关键字" placeholder="请输入姓名关键字"
@ -364,9 +408,11 @@ const MemberPage = () => {
<div className="d-flex mr-24"> <div className="d-flex mr-24">
<Typography.Text></Typography.Text> <Typography.Text></Typography.Text>
<Input <Input
value={email} value={email || ""}
onChange={(e) => { onChange={(e) => {
setEmail(e.target.value); resetLocalSearchParams({
email: e.target.value,
});
}} }}
style={{ width: 160 }} style={{ width: 160 }}
placeholder="请输入邮箱账号" placeholder="请输入邮箱账号"
@ -380,7 +426,9 @@ const MemberPage = () => {
<Button <Button
type="primary" type="primary"
onClick={() => { onClick={() => {
setPage(1); resetLocalSearchParams({
page: 1,
});
setRefresh(!refresh); setRefresh(!refresh);
}} }}
> >