331 lines
11 KiB
TypeScript
331 lines
11 KiB
TypeScript
"use client";
|
|
|
|
import dayjs from "dayjs";
|
|
import { useState } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
import { useRouter } from "next/navigation";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import {
|
|
getCoreRowModel,
|
|
useReactTable,
|
|
type PaginationState,
|
|
} from "@tanstack/react-table";
|
|
import {
|
|
Button,
|
|
Checkbox,
|
|
DataTable,
|
|
DateRangePicker,
|
|
Input,
|
|
Label,
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@common/components";
|
|
import { CodeApi } from "@/entities/code";
|
|
import { UserApi, type UserSearchParams } from "@/entities/user";
|
|
import { ENDPOINT } from "@/shared/config";
|
|
import { columns } from "../model/table-columns";
|
|
|
|
type DateRange = {
|
|
from: Date | undefined;
|
|
to?: Date | undefined;
|
|
};
|
|
|
|
function UsersPage() {
|
|
const router = useRouter();
|
|
|
|
const [pagination, setPagination] = useState<PaginationState>({
|
|
pageIndex: 0,
|
|
pageSize: 10,
|
|
});
|
|
const [dateRange, setDateRange] = useState<DateRange>();
|
|
|
|
const { register, watch, setValue, getValues } = useForm<UserSearchParams>({
|
|
defaultValues: {
|
|
keywordType: "all",
|
|
keyword: "",
|
|
userStateCode: "",
|
|
aprvYn: "",
|
|
grpId: "",
|
|
frDateYmd: "",
|
|
toDateYmd: "",
|
|
sort: ["aprvYmd,desc"],
|
|
},
|
|
});
|
|
|
|
const { data: userSttsStats } = useQuery({
|
|
queryKey: [ENDPOINT.USER_SERVICE.USERS],
|
|
queryFn: () => UserApi.selectUserSttsStats(),
|
|
});
|
|
console.log("userSttsStats: ", userSttsStats);
|
|
|
|
const { data: users, refetch } = useQuery({
|
|
queryKey: [ENDPOINT.USER_SERVICE.USERS, pagination, watch("sort")],
|
|
queryFn: () =>
|
|
UserApi.findPageUser({
|
|
...getValues(),
|
|
page: pagination.pageIndex,
|
|
size: pagination.pageSize,
|
|
}),
|
|
});
|
|
|
|
const { data: userStateCode } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.CODE],
|
|
queryFn: () => CodeApi.getCode("USERSTTS"),
|
|
});
|
|
|
|
const table = useReactTable({
|
|
data: users?.content ?? [],
|
|
columns,
|
|
state: {
|
|
pagination,
|
|
},
|
|
rowCount: users?.totalElements ?? 0,
|
|
manualPagination: true,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
onPaginationChange: setPagination,
|
|
});
|
|
|
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
refetch();
|
|
};
|
|
|
|
const handleRowClick = (row: unknown) => {
|
|
console.log(row);
|
|
};
|
|
|
|
const handlePresetDateRange = (value: string) => {
|
|
let presetDateRange: DateRange | undefined;
|
|
switch (value) {
|
|
case "1M":
|
|
presetDateRange = {
|
|
from: dayjs().subtract(1, "month").toDate(),
|
|
to: dayjs().toDate(),
|
|
};
|
|
break;
|
|
case "3M":
|
|
presetDateRange = {
|
|
from: dayjs().subtract(3, "month").toDate(),
|
|
to: dayjs().toDate(),
|
|
};
|
|
break;
|
|
case "6M":
|
|
presetDateRange = {
|
|
from: dayjs().subtract(6, "month").toDate(),
|
|
to: dayjs().toDate(),
|
|
};
|
|
break;
|
|
default:
|
|
presetDateRange = undefined;
|
|
}
|
|
setDateRange(presetDateRange);
|
|
setValue("frDateYmd", dayjs(presetDateRange?.from).format("YYYYMMDD"));
|
|
setValue("toDateYmd", dayjs(presetDateRange?.to).format("YYYYMMDD"));
|
|
};
|
|
|
|
const getUserSttsStatsCnt = (code: string): number => {
|
|
return userSttsStats?.find((item) => item.sttsCd === code)?.cnt ?? 0;
|
|
};
|
|
|
|
return (
|
|
<div className="card card-border !mt-6">
|
|
<div className="card card-border flex items-center justify-center gap-10 !bg-accent !py-6">
|
|
<div className="flex items-center gap-4">
|
|
<div className="flex size-12 items-center justify-center rounded-full bg-[rgba(153,153,153,1)]">
|
|
<i className="diveicon di-person text-xl text-white" />
|
|
</div>
|
|
<span className="text-2xl font-medium text-label">
|
|
승인대기{" "}
|
|
<em className="not-italic text-primary">
|
|
{getUserSttsStatsCnt("00")}
|
|
</em>
|
|
명
|
|
</span>
|
|
</div>
|
|
<div className="flex items-center gap-4">
|
|
<div className="flex size-12 items-center justify-center rounded-full bg-primary">
|
|
<i className="diveicon di-person-fill text-xl text-white" />
|
|
</div>
|
|
<span className="text-2xl font-medium text-label">
|
|
전체{" "}
|
|
<em className="not-italic text-primary">
|
|
{getUserSttsStatsCnt("TOTAL")}
|
|
</em>
|
|
명
|
|
</span>
|
|
</div>
|
|
<div className="flex items-center gap-4">
|
|
<div className="flex size-12 items-center justify-center rounded-full bg-[rgba(255,149,0,1)]">
|
|
<i className="diveicon di-lock text-xl text-white" />
|
|
</div>
|
|
<span className="text-2xl font-medium text-label">
|
|
잠금{" "}
|
|
<em className="not-italic text-primary">
|
|
{getUserSttsStatsCnt("07")}
|
|
</em>
|
|
명
|
|
</span>
|
|
</div>
|
|
<div className="flex items-center gap-4">
|
|
<div className="flex size-12 items-center justify-center rounded-full bg-destructive">
|
|
<i className="diveicon di-exclamation-triangle-fill text-xl text-white" />
|
|
</div>
|
|
<span className="text-2xl font-medium text-label">
|
|
탈퇴{" "}
|
|
<em className="not-italic text-primary">
|
|
{getUserSttsStatsCnt("08")}
|
|
</em>
|
|
명
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div className="card card-border">
|
|
<form onSubmit={handleSubmit} className="flex flex-col gap-6 p-2">
|
|
<div className="flex items-center gap-6">
|
|
<div className="flex items-center gap-2">
|
|
<Label className="mr-2 whitespace-nowrap text-label">
|
|
회원정보
|
|
</Label>
|
|
<Select
|
|
{...register("keywordType")}
|
|
onValueChange={(value) => setValue("keywordType", value)}
|
|
>
|
|
<SelectTrigger className="w-[18rem]">
|
|
<SelectValue placeholder="선택" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="all">전체</SelectItem>
|
|
<SelectItem value="userId">아이디</SelectItem>
|
|
<SelectItem value="userName">이름</SelectItem>
|
|
<SelectItem value="email">이메일</SelectItem>
|
|
<SelectItem value="mphn">휴대폰번호</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
<Input
|
|
{...register("keyword")}
|
|
className="!text-[1.3rem]"
|
|
placeholder="검색어를 입력하세요"
|
|
/>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<Label className="mr-2 text-label">그룹</Label>
|
|
{/* Select */}
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<Label className="mr-2 text-label">기간</Label>
|
|
<DateRangePicker
|
|
value={dateRange}
|
|
onValueChange={(value) => {
|
|
setDateRange(value);
|
|
setValue("frDateYmd", dayjs(value?.from).format("YYYYMMDD"));
|
|
setValue("toDateYmd", dayjs(value?.to).format("YYYYMMDD"));
|
|
}}
|
|
>
|
|
<div className="card card-border flex items-center gap-2">
|
|
<Select onValueChange={handlePresetDateRange}>
|
|
<SelectTrigger>
|
|
<SelectValue
|
|
placeholder="기간 선택"
|
|
className="w-[24rem]"
|
|
/>
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="1M">1개월</SelectItem>
|
|
<SelectItem value="3M">3개월</SelectItem>
|
|
<SelectItem value="6M">6개월</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</DateRangePicker>
|
|
</div>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<Label className="mr-2 text-label">회원상태</Label>
|
|
<div className="flex gap-4">
|
|
{userStateCode?.map((item) => (
|
|
<div key={item.lwpoCd} className="flex items-center gap-2">
|
|
<Checkbox
|
|
id={item.lwpoCd}
|
|
value={item.lwpoCd}
|
|
checked={watch("userStateCode")?.includes(item.lwpoCd)}
|
|
onCheckedChange={(checked) => {
|
|
setValue(
|
|
"userStateCode",
|
|
checked
|
|
? getValues("userStateCode")
|
|
? `${getValues("userStateCode")},${item.lwpoCd}`
|
|
: item.lwpoCd
|
|
: getValues("userStateCode")
|
|
?.split(",")
|
|
.filter((code) => code !== item.lwpoCd)
|
|
.join(","),
|
|
);
|
|
}}
|
|
/>
|
|
<Label htmlFor={item.lwpoCd}>{item.lwpoCdNm}</Label>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
<hr />
|
|
<Button type="submit" className="mx-auto w-[16rem]">
|
|
<i className="diveicon di-search" />
|
|
검색
|
|
</Button>
|
|
</form>
|
|
</div>
|
|
<div className="card card-border">
|
|
<div className="card-header">
|
|
<div className="flex w-full items-center justify-between">
|
|
<h3 className="text-2xl font-medium text-label">회원 목록</h3>
|
|
<div className="flex gap-2">
|
|
<Select
|
|
defaultValue="aprvYmd,desc"
|
|
onValueChange={(value) => {
|
|
setValue("sort", [value]);
|
|
}}
|
|
>
|
|
<SelectTrigger>
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="aprvYmd,desc">승인일순</SelectItem>
|
|
<SelectItem value="regDt,desc">등록일순</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
<Select
|
|
value={table.getState().pagination.pageSize.toString()}
|
|
onValueChange={(value) => {
|
|
table.setPageSize(parseInt(value));
|
|
}}
|
|
>
|
|
<SelectTrigger className="h-[3.2rem] w-[12rem]">
|
|
<SelectValue placeholder="페이지 크기" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{[10, 20, 30, 40, 50].map((pageSize) => (
|
|
<SelectItem key={pageSize} value={pageSize.toString()}>
|
|
{pageSize}개씩 보기
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
<Button onClick={() => router.push("/users/register")}>
|
|
등록
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="card !m-[1.2rem] !p-0">
|
|
<DataTable table={table} onRowClick={handleRowClick} viewPagination />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export { UsersPage };
|