|
@@ -1,4 +1,4 @@
|
|
|
-import React from 'react';
|
|
|
|
|
|
|
+import React, { useState, useEffect } from 'react';
|
|
|
import { useForm } from 'react-hook-form';
|
|
import { useForm } from 'react-hook-form';
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
import { Button } from '@/client/components/ui/button';
|
|
import { Button } from '@/client/components/ui/button';
|
|
@@ -10,6 +10,8 @@ import type { CreateAreaInput, UpdateAreaInput } from '@d8d/server/modules/areas
|
|
|
import { AreaLevel } from '@d8d/server/modules/areas/area.entity';
|
|
import { AreaLevel } from '@d8d/server/modules/areas/area.entity';
|
|
|
import { DisabledStatus } from '@/share/types';
|
|
import { DisabledStatus } from '@/share/types';
|
|
|
import { AreaSelect } from './AreaSelect';
|
|
import { AreaSelect } from './AreaSelect';
|
|
|
|
|
+import { useQuery } from '@tanstack/react-query';
|
|
|
|
|
+import { areaClient } from '@/client/api';
|
|
|
|
|
|
|
|
interface AreaFormProps {
|
|
interface AreaFormProps {
|
|
|
area?: UpdateAreaInput & { id?: number };
|
|
area?: UpdateAreaInput & { id?: number };
|
|
@@ -25,6 +27,54 @@ export const AreaForm: React.FC<AreaFormProps> = ({
|
|
|
isLoading = false
|
|
isLoading = false
|
|
|
}) => {
|
|
}) => {
|
|
|
const isEditing = !!area;
|
|
const isEditing = !!area;
|
|
|
|
|
+ const [parentAreaInfo, setParentAreaInfo] = useState<{
|
|
|
|
|
+ provinceId?: number;
|
|
|
|
|
+ cityId?: number;
|
|
|
|
|
+ }>({});
|
|
|
|
|
+
|
|
|
|
|
+ // 查询父级区域的完整层级信息
|
|
|
|
|
+ const { data: parentAreaData } = useQuery({
|
|
|
|
|
+ queryKey: ['area', 'parent', area?.parentId],
|
|
|
|
|
+ queryFn: async () => {
|
|
|
|
|
+ if (!area?.parentId || area.parentId === 0) return null;
|
|
|
|
|
+ const res = await areaClient[':id'].$get({
|
|
|
|
|
+ param: { id: area.parentId }
|
|
|
|
|
+ });
|
|
|
|
|
+ if (res.status !== 200) throw new Error('获取父级区域信息失败');
|
|
|
|
|
+ return await res.json();
|
|
|
|
|
+ },
|
|
|
|
|
+ enabled: isEditing && !!area?.parentId && area.parentId > 0,
|
|
|
|
|
+ staleTime: 5 * 60 * 1000,
|
|
|
|
|
+ gcTime: 10 * 60 * 1000,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 根据父级区域信息设置层级关系
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ if (parentAreaData) {
|
|
|
|
|
+ const parentArea = parentAreaData;
|
|
|
|
|
+ if (parentArea.level === AreaLevel.PROVINCE) {
|
|
|
|
|
+ // 父级是省份,当前区域是城市
|
|
|
|
|
+ setParentAreaInfo({
|
|
|
|
|
+ provinceId: parentArea.id
|
|
|
|
|
+ });
|
|
|
|
|
+ } else if (parentArea.level === AreaLevel.CITY) {
|
|
|
|
|
+ // 父级是城市,当前区域是区县,需要查询城市的父级省份
|
|
|
|
|
+ const fetchProvinceId = async () => {
|
|
|
|
|
+ const res = await areaClient[':id'].$get({
|
|
|
|
|
+ param: { id: parentArea.parentId! }
|
|
|
|
|
+ });
|
|
|
|
|
+ if (res.status === 200) {
|
|
|
|
|
+ const provinceResult = await res.json();
|
|
|
|
|
+ setParentAreaInfo({
|
|
|
|
|
+ provinceId: provinceResult.id,
|
|
|
|
|
+ cityId: parentArea.id
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ fetchProvinceId();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [parentAreaData]);
|
|
|
|
|
|
|
|
const form = useForm<CreateAreaInput | UpdateAreaInput>({
|
|
const form = useForm<CreateAreaInput | UpdateAreaInput>({
|
|
|
resolver: zodResolver(isEditing ? updateAreaSchema : createAreaSchema),
|
|
resolver: zodResolver(isEditing ? updateAreaSchema : createAreaSchema),
|
|
@@ -93,6 +143,25 @@ export const AreaForm: React.FC<AreaFormProps> = ({
|
|
|
const level = form.watch('level');
|
|
const level = form.watch('level');
|
|
|
const showParentSelect = level !== AreaLevel.PROVINCE;
|
|
const showParentSelect = level !== AreaLevel.PROVINCE;
|
|
|
|
|
|
|
|
|
|
+ // 根据当前层级和编辑状态设置AreaSelect的值
|
|
|
|
|
+ const getAreaSelectValue = () => {
|
|
|
|
|
+ if (!isEditing || !area) {
|
|
|
|
|
+ return {};
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (level === AreaLevel.CITY) {
|
|
|
|
|
+ // 城市级别:需要省份ID
|
|
|
|
|
+ return { provinceId: parentAreaInfo.provinceId };
|
|
|
|
|
+ } else if (level === AreaLevel.DISTRICT) {
|
|
|
|
|
+ // 区县级别:需要省份ID和城市ID
|
|
|
|
|
+ return {
|
|
|
|
|
+ provinceId: parentAreaInfo.provinceId,
|
|
|
|
|
+ cityId: parentAreaInfo.cityId
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ return {};
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
return (
|
|
return (
|
|
|
<FormItem>
|
|
<FormItem>
|
|
|
<FormLabel>父级区域</FormLabel>
|
|
<FormLabel>父级区域</FormLabel>
|
|
@@ -100,9 +169,7 @@ export const AreaForm: React.FC<AreaFormProps> = ({
|
|
|
<>
|
|
<>
|
|
|
<FormControl>
|
|
<FormControl>
|
|
|
<AreaSelect
|
|
<AreaSelect
|
|
|
- value={{
|
|
|
|
|
- districtId: level === AreaLevel.DISTRICT ? field.value || undefined : undefined
|
|
|
|
|
- }}
|
|
|
|
|
|
|
+ value={getAreaSelectValue()}
|
|
|
onChange={(value) => {
|
|
onChange={(value) => {
|
|
|
// 根据层级设置父级ID
|
|
// 根据层级设置父级ID
|
|
|
if (level === AreaLevel.CITY) {
|
|
if (level === AreaLevel.CITY) {
|