diff --git a/apps/cs/middleware.ts b/apps/cs/middleware.ts index e7b03dddc..4d6d816a7 100644 --- a/apps/cs/middleware.ts +++ b/apps/cs/middleware.ts @@ -5,10 +5,13 @@ const serverApiUrl = export function middleware(req: NextRequest) { const pathname = req.nextUrl.pathname; + const searchParams = req.nextUrl.searchParams; + if (pathname.startsWith("/server")) { - return NextResponse.rewrite( - `${serverApiUrl}${pathname.replace("/server", "")}`, + const newUrl = new URL( + `${serverApiUrl}${pathname.replace("/server", "")}?${searchParams.toString() ?? ""}`, ); + return NextResponse.rewrite(newUrl); } return NextResponse.next(); diff --git a/apps/cs/src/pages/site/api/domain-service.ts b/apps/cs/src/pages/site/api/domain-service.ts new file mode 100644 index 000000000..7cc36363f --- /dev/null +++ b/apps/cs/src/pages/site/api/domain-service.ts @@ -0,0 +1,28 @@ +import { client } from "@/shared/api"; +import { ENDPOINT } from "@/shared/config"; +import type { + Domain, + DomainService, + DomainList, + IDomainParams, +} from "../model/domain-interface"; + +export class DomainApiService implements DomainService { + public async getDomainList(params: IDomainParams): Promise { + const response = await client.get( + `${ENDPOINT.CMS_SERVICE.DOMAINS}`, + { params }, + ); + return response.data; + } + + public async saveDomainList(body: Domain[]): Promise { + const response = await client.post( + `${ENDPOINT.CMS_SERVICE.DOMAINS}`, + body, + ); + return response.data; + } +} + +export const DomainApi = new DomainApiService(); diff --git a/apps/cs/src/pages/site/api/ip-service.ts b/apps/cs/src/pages/site/api/ip-service.ts index 2bfad0934..748676b21 100644 --- a/apps/cs/src/pages/site/api/ip-service.ts +++ b/apps/cs/src/pages/site/api/ip-service.ts @@ -1,10 +1,12 @@ import { client } from "@/shared/api"; import { ENDPOINT } from "@/shared/config"; -import type { IP, IpList, IpService } from "../model/ip-interface"; +import type { IIpParams, IP, IpList, IpService } from "../model/ip-interface"; export class IpApiService implements IpService { - public async getIpList(): Promise { - const response = await client.get(`${ENDPOINT.CMS_SERVICE.IPS}`); + public async getIpList(params: IIpParams): Promise { + const response = await client.get(`${ENDPOINT.CMS_SERVICE.IPS}`, { + params, + }); return response.data; } diff --git a/apps/cs/src/pages/site/api/site-service.ts b/apps/cs/src/pages/site/api/site-service.ts index 22363de68..629bf5f01 100644 --- a/apps/cs/src/pages/site/api/site-service.ts +++ b/apps/cs/src/pages/site/api/site-service.ts @@ -2,16 +2,16 @@ import { client } from "@/shared/api"; import { ENDPOINT } from "@/shared/config"; import { siteFormSchema, - type ISiteInfo, type SiteService, + type SiteList, type ISiteDetail, type ISiteParams, type SiteFormData, } from "../model/site-interface"; export class SiteApiService implements SiteService { - public async siteInfo(params: ISiteParams): Promise { - const response = await client.get( + public async getSiteList(params: ISiteParams): Promise { + const response = await client.get( `${ENDPOINT.CMS_SERVICE.SITES}`, { params }, ); diff --git a/apps/cs/src/pages/site/model/domain-interface.ts b/apps/cs/src/pages/site/model/domain-interface.ts new file mode 100644 index 000000000..5f1076275 --- /dev/null +++ b/apps/cs/src/pages/site/model/domain-interface.ts @@ -0,0 +1,31 @@ +import { z } from "zod"; +import type { IPageableParams, PageableList } from "@/shared/model"; + +export interface IDomainParams extends IPageableParams { + keywordType?: string; + keyword?: string; + siteId?: string; + useYn?: string; +} + +export const domainSchema = z.object({ + siteDmnNo: z.number().nullish(), + siteId: z.string(), + dmnAddr: z.string().nonempty(), + useYn: z.string(), + rprsDmnYn: z.string(), + mode: z.string(), +}); + +export const domainFormSchema = z.object({ + domains: z.array(domainSchema), +}); + +export type Domain = z.infer; +export type DomainFormData = z.infer; +export type DomainList = PageableList; + +export interface DomainService { + getDomainList(params: IDomainParams): Promise; + saveDomainList(body: Domain[]): Promise; +} diff --git a/apps/cs/src/pages/site/model/ip-interface.ts b/apps/cs/src/pages/site/model/ip-interface.ts index f8cf9d5ea..db1d393f1 100644 --- a/apps/cs/src/pages/site/model/ip-interface.ts +++ b/apps/cs/src/pages/site/model/ip-interface.ts @@ -1,4 +1,11 @@ import { z } from "zod"; +import type { IPageableParams, PageableList } from "@/shared/model"; + +export interface IIpParams extends IPageableParams { + siteId: string; + ipAddr?: string; + prmYn?: string; +} export const ipSchema = z.object({ mngNo: z.number().nullish(), @@ -14,37 +21,9 @@ export const ipFormSchema = z.object({ export type IP = z.infer; export type IPFormData = z.infer; - -export interface IpList { - content: IP[]; - pageable: { - sort: { - empty: boolean; - sorted: boolean; - unsorted: boolean; - }; - offset: number; - pageNumber: number; - pageSize: number; - paged: boolean; - unpaged: boolean; - }; - last: boolean; - totalPages: number; - totalElements: number; - number: number; - sort: { - empty: boolean; - sorted: boolean; - unsorted: boolean; - }; - size: number; - first: boolean; - numberOfElements: number; - empty: boolean; -} +export type IpList = PageableList; export interface IpService { - getIpList(): Promise; + getIpList(params: IIpParams): Promise; saveIpList(body: IP[]): Promise; } diff --git a/apps/cs/src/pages/site/model/site-interface.ts b/apps/cs/src/pages/site/model/site-interface.ts index 4d03e2c8b..6edc1e69e 100644 --- a/apps/cs/src/pages/site/model/site-interface.ts +++ b/apps/cs/src/pages/site/model/site-interface.ts @@ -1,12 +1,10 @@ import { z } from "zod"; +import type { IPageableParams, PageableList } from "@/shared/model"; -export interface ISiteParams { - siteNm: string; - siteKndCd: string; - useYn: string; - page: number; - size: number; - sort: string[]; +export interface ISiteParams extends IPageableParams { + siteNm?: string; + siteKndCd?: string; + useYn?: string; } export interface ISite { @@ -20,35 +18,6 @@ export interface ISite { rgTrNm: string; } -export interface ISiteInfo { - totalPages: number; - totalElements: number; - number: number; - sort: { - empty: boolean; - sorted: boolean; - unsorted: boolean; - }; - size: number; - content: ISite[]; - pageable: { - sort: { - empty: boolean; - sorted: boolean; - unsorted: boolean; - }; - offset: number; - pageNumber: number; - pageSize: number; - paged: boolean; - unpaged: boolean; - }; - numberOfElements: number; - first: boolean; - last: boolean; - empty: boolean; -} - export interface ISiteDetail extends SiteFormData { siteId: string; siteKindNm: string; @@ -65,22 +34,23 @@ export interface ISiteDetail extends SiteFormData { export const siteFormSchema = z.object({ siteId: z.string().max(2).nonempty(), siteNm: z.string().max(100).nonempty(), - siteExpln: z.string().max(1000).optional(), + siteExpln: z.string().max(1000).nullable(), siteKndCd: z.string().max(10).nonempty(), - siteAddr: z.string().max(2000).optional(), - faxNo: z.string().max(100).optional(), - telNo: z.string().max(1000).optional(), - lwndCn: z.string().max(2000).optional(), + siteAddr: z.string().max(2000).nullable(), + faxNo: z.string().max(100).nullable(), + telNo: z.string().max(1000).nullable(), + lwndCn: z.string().max(2000).nullable(), bscSiteYn: z.string(), - bkmkIcon: z.string().max(100).optional(), + bkmkIcon: z.string().max(100).nullable(), useYn: z.string(), - siteSkn: z.string().max(10).optional(), + siteSkn: z.string().max(10).nullable(), }); export type SiteFormData = z.infer; +export type SiteList = PageableList; export interface SiteService { - siteInfo(query: ISiteParams): Promise; + getSiteList(params: ISiteParams): Promise; siteDetail(siteId: string): Promise; modifySite(siteId: string, data: SiteFormData): Promise; createSite(data: SiteFormData): Promise; diff --git a/apps/cs/src/pages/site/ui/domain-management-dialog.tsx b/apps/cs/src/pages/site/ui/domain-management-dialog.tsx index c675f92d9..4a7e854ee 100644 --- a/apps/cs/src/pages/site/ui/domain-management-dialog.tsx +++ b/apps/cs/src/pages/site/ui/domain-management-dialog.tsx @@ -1,4 +1,8 @@ import { useState } from "react"; +import { useMutation, useQuery } from "@tanstack/react-query"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useConfirm } from "@frontend-opensource/use-react-hooks"; +import { useForm, useFieldArray } from "react-hook-form"; import { Dialog, DialogTrigger, @@ -13,197 +17,304 @@ import { TableHeader, TableRow, Input, - Checkbox, - Select, - SelectContent, - SelectItem, - SelectTrigger, - SelectValue, + RadioGroup, + RadioGroupItem, DialogFooter, DialogClose, + Checkbox, } from "@common/components"; - -export type Domain = { - id: number; - domainUrl: string; - isRepresentative: boolean; - isActive: "true" | "false"; -}; +import { + Form, + FormControl, + FormField, + FormItem, + FormMessage, +} from "@/shared/ui/form"; +import { ENDPOINT } from "@/shared/config"; +import { DomainApi } from "../api/domain-service"; +import { + domainFormSchema, + type Domain, + type DomainFormData, +} from "../model/domain-interface"; +import { ConfirmDialog } from "@/shared/ui/confirm-dialog"; +import { useAlertStore } from "@/shared/lib/use-alert-store"; type DomainManagementDialogProps = { - triggerDisabled?: boolean; - handleSave: (rows: Domain[]) => void; + siteId: string; + open?: boolean; + onOpenChange?: (open: boolean) => void; + handleSaveDomain?: (rows: Domain[]) => void; }; function DomainManagementDialog({ - triggerDisabled, - handleSave, + siteId, + handleSaveDomain, + ...props }: DomainManagementDialogProps) { - const [rows, setRows] = useState([]); - const [rowSelection, setRowSelection] = useState([]); + const { confirm } = useConfirm(); + const { setMessage: alert } = useAlertStore((state) => state); - const onSave = () => { - handleSave(rows); + const { data: domainList } = useQuery({ + queryKey: [ENDPOINT.CMS_SERVICE.DOMAINS, props.open], + queryFn: () => DomainApi.getDomainList({ siteId }), + select: (data) => { + const domains = data.content.map((item) => ({ + ...item, + mode: "U", + })); + + initialForm(); + return domains; + }, + }); + + const { mutateAsync } = useMutation({ + mutationFn: (body: Domain[]) => DomainApi.saveDomainList(body), + }); + + const form = useForm({ + resolver: zodResolver(domainFormSchema), + values: { + domains: domainList ?? [], + }, + }); + const { fields, append, remove, update } = useFieldArray({ + control: form.control, + name: "domains", + }); + + const [onConfirm, setOnConfirm] = useState(false); + const [selectedRows, setSelectedRows] = useState([]); + + const initialForm = () => { + form.reset(); + setSelectedRows([]); + }; + + const onSubmit = async (data: DomainFormData) => { + setOnConfirm(true); + if (await confirm("저장하시겠습니까?")) { + setOnConfirm(false); + await mutateAsync(data.domains); + handleSaveDomain?.(data.domains); + alert("저장되었습니다."); + } + setOnConfirm(false); }; const handleAddDomain = () => { - const newDomain: Domain = { - id: Date.now(), - domainUrl: "", - isRepresentative: rows.length === 0, - isActive: "true", - }; - setRows([...rows, newDomain]); - }; - - const handleDeleteDomain = () => { - const filteredRows = rows.filter((row) => !rowSelection.includes(row.id)); - const hasRepresentative = filteredRows.some((row) => row.isRepresentative); - - let updatedRows = filteredRows; - if (!hasRepresentative && filteredRows.length > 0) { - updatedRows = filteredRows.map((row, index) => - index === 0 ? { ...row, isRepresentative: true } : row, - ); - } - - setRows(updatedRows); - setRowSelection([]); - }; - - const handleDomainUrlChange = (id: number, value: string) => { - setRows( - rows.map((row) => (row.id === id ? { ...row, domainUrl: value } : row)), - ); - }; - - const handleRepresentativeChange = (id: number) => { - setRows( - rows.map((row) => ({ - ...row, - isRepresentative: row.id === id, - })), - ); - }; - - const handleActiveChange = (id: number, value: "true" | "false") => { - setRows( - rows.map((row) => (row.id === id ? { ...row, isActive: value } : row)), - ); - }; - - const toggleRowSelection = (id: number) => { - setRowSelection((prev) => { - if (prev.includes(id)) { - return prev.filter((item) => item !== id); - } else { - return [...prev, id]; - } + append({ + siteDmnNo: null, + siteId, + dmnAddr: "", + useYn: "Y", + rprsDmnYn: "N", + mode: "C", }); }; + const handleSelectAll = () => { + if (selectedRows.length === fields.length) { + setSelectedRows([]); + } else { + setSelectedRows(fields.map((_, index) => index)); + } + }; + + const handleSelectRow = (i: number) => { + setSelectedRows((prev) => + prev.includes(i) ? prev.filter((j) => j !== i) : [...prev, i], + ); + }; + + const handleDeleteDomain = async () => { + if (selectedRows.length === 0) return; + + const sortedIndices = [...selectedRows].sort((a, b) => b - a); + sortedIndices.forEach((i) => { + const field = fields[i]; + if (field?.siteDmnNo) { + update(i, { + ...field, + mode: "D", + }); + } else { + remove(i); + } + }); + + setSelectedRows([]); + }; + + const handleChangeRprsDmnYn = (i: number) => { + fields.forEach((_, j) => { + if (j === i) return; + form.setValue(`domains.${j}.rprsDmnYn`, "N"); + }); + }; + + const isAllSelected = + fields.length > 0 && selectedRows.length === fields.length; + return ( - - - - - - - 도메인 관리 - -
- - -
-
- - - - - - - - - - - NO - 선택 - 도메인URL - 대표URL - 사용여부 - - - - {rows.map((row, idx) => ( - - {idx + 1} - - toggleRowSelection(row.id)} - className="cursor-pointer" - /> - - - - handleDomainUrlChange(row.id, e.target.value) - } - className="!text-[1.3rem]" - /> - - - handleRepresentativeChange(row.id)} - className="cursor-pointer" - /> - - - - - - ))} - -
-
- - - - - - - - -
-
+ <> + + + + + + + 도메인 관리 + +
+ { + e.preventDefault(); + e.stopPropagation(); + form.handleSubmit(onSubmit)(e); + }} + > +
+ + +
+
+ + + + + + + + + + + + + 도메인 주소 + 대표 URL + 사용여부 + + + + {fields.map((field, i) => { + if (field.mode === "D") return null; + return ( + + + handleSelectRow(i)} + /> + + + ( + + + + + + + )} + /> + + + ( + + + { + field.onChange(checked ? "Y" : "N"); + handleChangeRprsDmnYn(i); + }} + /> + + + + )} + /> + + + ( + + + +
+ + +
+
+ + +
+
+
+ +
+ )} + /> +
+
+ ); + })} +
+
+
+ + + + + + +
+ +
+
+ + ); } diff --git a/apps/cs/src/pages/site/ui/ip-management-dialog.tsx b/apps/cs/src/pages/site/ui/ip-management-dialog.tsx index cbf1625ad..97721a6d6 100644 --- a/apps/cs/src/pages/site/ui/ip-management-dialog.tsx +++ b/apps/cs/src/pages/site/ui/ip-management-dialog.tsx @@ -2,7 +2,7 @@ import { useState } from "react"; import { useMutation, useQuery } from "@tanstack/react-query"; import { zodResolver } from "@hookform/resolvers/zod"; import { useConfirm } from "@frontend-opensource/use-react-hooks"; -import { useForm, useFieldArray, type FieldArrayWithId } from "react-hook-form"; +import { useForm, useFieldArray } from "react-hook-form"; import { Dialog, DialogTrigger, @@ -30,37 +30,38 @@ import { FormItem, FormMessage, } from "@/shared/ui/form"; -import { ENDPOINT, SITE_ID } from "@/shared/config"; +import { ENDPOINT } from "@/shared/config"; import { IpApi } from "../api/ip-service"; import { ipFormSchema, type IP, type IPFormData } from "../model/ip-interface"; import { ConfirmDialog } from "@/shared/ui/confirm-dialog"; import { useAlertStore } from "@/shared/lib/use-alert-store"; type IPManagementDialogProps = { + siteId: string; triggerDisabled?: boolean; open?: boolean; onOpenChange?: (open: boolean) => void; }; function IPManagementDialog({ + siteId, triggerDisabled, ...props }: IPManagementDialogProps) { const { confirm } = useConfirm(); const { setMessage: alert } = useAlertStore((state) => state); - useQuery({ + const { data: ipList } = useQuery({ queryKey: [ENDPOINT.CMS_SERVICE.IPS, props.open], - queryFn: () => IpApi.getIpList(), + queryFn: () => IpApi.getIpList({ siteId, sort: [] }), select: (data) => { const ips = data.content.map((item) => ({ ...item, mode: "U", })); - form.reset(); - replace(ips); - return data; + initialForm(); + return ips; }, }); @@ -70,8 +71,11 @@ function IPManagementDialog({ const form = useForm({ resolver: zodResolver(ipFormSchema), + values: { + ips: ipList ?? [], + }, }); - const { fields, append, remove, update, replace } = useFieldArray({ + const { fields, append, remove, update } = useFieldArray({ control: form.control, name: "ips", }); @@ -79,6 +83,11 @@ function IPManagementDialog({ const [onConfirm, setOnConfirm] = useState(false); const [selectedRows, setSelectedRows] = useState([]); + const initialForm = () => { + form.reset(); + setSelectedRows([]); + }; + const onSubmit = async (data: IPFormData) => { setOnConfirm(true); if (await confirm("저장하시겠습니까?")) { @@ -91,24 +100,34 @@ function IPManagementDialog({ const handleAddIP = () => { append({ mngNo: null, - siteId: SITE_ID, + siteId, ipAddr: "", prmYn: "Y", mode: "C", }); }; + const handleSelectAll = () => { + if (selectedRows.length === fields.length) { + setSelectedRows([]); + } else { + setSelectedRows(fields.map((_, index) => index)); + } + }; + const handleSelectRow = (i: number) => { setSelectedRows((prev) => prev.includes(i) ? prev.filter((j) => j !== i) : [...prev, i], ); }; - const handleDeleteIP = () => { - selectedRows.forEach((i) => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { id, ...field } = fields[i] as FieldArrayWithId; - if (field.mngNo) { + const handleDeleteIP = async () => { + if (selectedRows.length === 0) return; + + const sortedIndices = [...selectedRows].sort((a, b) => b - a); + sortedIndices.forEach((i) => { + const field = fields[i]; + if (field?.mngNo) { update(i, { ...field, mode: "D", @@ -117,9 +136,13 @@ function IPManagementDialog({ remove(i); } }); + setSelectedRows([]); }; + const isAllSelected = + fields.length > 0 && selectedRows.length === fields.length; + return ( <> @@ -161,7 +184,12 @@ function IPManagementDialog({ - 선택 + + + IP 주소 허용여부 diff --git a/apps/cs/src/pages/site/ui/site-form.tsx b/apps/cs/src/pages/site/ui/site-form.tsx index 4922031f4..7716d393e 100644 --- a/apps/cs/src/pages/site/ui/site-form.tsx +++ b/apps/cs/src/pages/site/ui/site-form.tsx @@ -23,12 +23,10 @@ import { FormMessage, } from "@/shared/ui/form"; import { ConfirmDialog } from "@/shared/ui/confirm-dialog"; -import { type SiteFormData } from "../model/site-interface"; +import type { SiteFormData } from "../model/site-interface"; +import type { Domain } from "../model/domain-interface"; import { IPManagementDialog } from "./ip-management-dialog"; -import { - DomainManagementDialog, - type Domain, -} from "./domain-management-dialog"; +import { DomainManagementDialog } from "./domain-management-dialog"; type SiteFormProps = { form: UseFormReturn; @@ -44,12 +42,11 @@ function SiteForm({ form, siteId, handleSave, handleDelete }: SiteFormProps) { const { confirm } = useConfirm(); const [onConfirm, setOnConfirm] = useState(false); const [isIpManagementOpen, setIsIpManagementOpen] = useState(false); - const [representativeDomain, setRepresentativeDomain] = useState(""); + const [isDomainManagementOpen, setIsDomainManagementOpen] = useState(false); + const [domainUrl, setDomainUrl] = useState(""); - const handleDomainSave = (rows: Domain[]) => { - setRepresentativeDomain( - rows.find((row) => row.isRepresentative)?.domainUrl || "", - ); + const handleSaveDomain = (rows: Domain[]) => { + setDomainUrl(rows.find((row) => row.rprsDmnYn === "Y")?.dmnAddr || ""); }; const onSubmit = async (data: SiteFormData) => { @@ -132,6 +129,7 @@ function SiteForm({ form, siteId, handleSave, handleDelete }: SiteFormProps) { /> {isModifyMode && ( @@ -193,6 +192,7 @@ function SiteForm({ form, siteId, handleSave, handleDelete }: SiteFormProps) { @@ -209,10 +209,13 @@ function SiteForm({ form, siteId, handleSave, handleDelete }: SiteFormProps) {
- {representativeDomain} + {domainUrl}
@@ -229,6 +232,7 @@ function SiteForm({ form, siteId, handleSave, handleDelete }: SiteFormProps) { @@ -250,6 +254,7 @@ function SiteForm({ form, siteId, handleSave, handleDelete }: SiteFormProps) { @@ -271,6 +276,7 @@ function SiteForm({ form, siteId, handleSave, handleDelete }: SiteFormProps) { @@ -292,6 +298,7 @@ function SiteForm({ form, siteId, handleSave, handleDelete }: SiteFormProps) { @@ -313,6 +320,7 @@ function SiteForm({ form, siteId, handleSave, handleDelete }: SiteFormProps) { diff --git a/apps/cs/src/pages/site/ui/site-page.tsx b/apps/cs/src/pages/site/ui/site-page.tsx index feace6285..8a2bd5264 100644 --- a/apps/cs/src/pages/site/ui/site-page.tsx +++ b/apps/cs/src/pages/site/ui/site-page.tsx @@ -7,7 +7,7 @@ import { useEffect, useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { getCoreRowModel, useReactTable } from "@tanstack/react-table"; import { Button, DataTable, Input } from "@common/components"; -import { ENDPOINT } from "@/shared/config/api"; +import { ENDPOINT } from "@/shared/config"; import { SiteApi } from "../api/site-service"; import { columns } from "../model/table-columns"; import { type ISite, type ISiteParams } from "../model/site-interface"; @@ -15,23 +15,18 @@ import { SitePageLayout } from "./layout"; function SitePage() { const router = useRouter(); - const { data: siteInfo, refetch } = useQuery({ + const { data: siteList, refetch } = useQuery({ queryKey: [ENDPOINT.CMS_SERVICE.SITES], - queryFn: () => SiteApi.siteInfo(siteQuery), + queryFn: () => SiteApi.getSiteList(siteQuery), enabled: false, }); const [siteQuery, setSiteQuery] = useState({ siteNm: "", - siteKndCd: "", - useYn: "", - page: 0, - size: 0, - sort: [], }); const table = useReactTable({ - data: siteInfo?.content ?? [], + data: siteList?.content ?? [], columns, getCoreRowModel: getCoreRowModel(), }); @@ -61,9 +56,7 @@ function SitePage() { - setSiteQuery({ ...siteQuery, siteNm: e.target.value }) - } + onChange={(e) => setSiteQuery({ siteNm: e.target.value })} />