Feat: 레이블 저장 시 상태 자동 fefetch

This commit is contained in:
jhynsoo 2024-09-26 20:54:36 +09:00
parent 0182f6d0d0
commit 6a47bbbc70
5 changed files with 37 additions and 8 deletions

View File

@ -9,14 +9,15 @@ import LabelPolygon from './LabelPolygon';
import CanvasControlBar from '../CanvasControlBar'; import CanvasControlBar from '../CanvasControlBar';
import { Label } from '@/types'; import { Label } from '@/types';
import useLabelJson from '@/hooks/useLabelJson'; import useLabelJson from '@/hooks/useLabelJson';
import { saveImageLabels } from '@/api/lablingApi';
import useProjectStore from '@/stores/useProjectStore'; import useProjectStore from '@/stores/useProjectStore';
import { LABEL_CATEGORY } from '@/constants'; import { LABEL_CATEGORY } from '@/constants';
import { useQueryClient } from '@tanstack/react-query';
import useSaveImageLabelsQuery from '@/queries/projects/useSaveImageLabelsQuery';
export default function ImageCanvas() { export default function ImageCanvas() {
const project = useProjectStore((state) => state.project)!; const { project, folderId } = useProjectStore();
const { id: imageId, imagePath, dataPath } = useCanvasStore((state) => state.image)!; const { id: imageId, imagePath, dataPath } = useCanvasStore((state) => state.image)!;
const { data: labelData } = useLabelJson(dataPath, project); const { data: labelData } = useLabelJson(dataPath, project!);
const { labels, drawState, setDrawState, addLabel, setLabels, selectedLabelId, setSelectedLabelId, sidebarSize } = const { labels, drawState, setDrawState, addLabel, setLabels, selectedLabelId, setSelectedLabelId, sidebarSize } =
useCanvasStore(); useCanvasStore();
const { shapes } = labelData || []; const { shapes } = labelData || [];
@ -28,6 +29,8 @@ export default function ImageCanvas() {
const [image] = useImage(imagePath); const [image] = useImage(imagePath);
const [rectPoints, setRectPoints] = useState<[number, number][]>([]); const [rectPoints, setRectPoints] = useState<[number, number][]>([]);
const [polygonPoints, setPolygonPoints] = useState<[number, number][]>([]); const [polygonPoints, setPolygonPoints] = useState<[number, number][]>([]);
const saveImageLabelsMutation = useSaveImageLabelsQuery();
const queryClient = useQueryClient();
useEffect(() => { useEffect(() => {
setLabels( setLabels(
@ -65,9 +68,17 @@ export default function ImageCanvas() {
imageHeight: image!.height, imageHeight: image!.height,
}); });
saveImageLabels(project.id, imageId, { data: json }).catch(() => { saveImageLabelsMutation.mutate(
{ projectId: project!.id, imageId: imageId, data: { data: json } },
{
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['folder', project!.id.toString(), folderId] });
},
onError: () => {
alert('레이블 데이터 저장 실패'); alert('레이블 데이터 저장 실패');
}); },
}
);
}; };
const startDrawRect = () => { const startDrawRect = () => {
const { x, y } = stageRef.current!.getRelativePointerPosition()!; const { x, y } = stageRef.current!.getRelativePointerPosition()!;
@ -342,7 +353,7 @@ export default function ImageCanvas() {
</Stage> </Stage>
<CanvasControlBar <CanvasControlBar
saveJson={saveJson} saveJson={saveJson}
projectType={project.type} projectType={project!.type}
/> />
</div> </div>
) : ( ) : (

View File

@ -56,6 +56,7 @@ export default function ProjectDirectoryItem({
key={`${projectId}-${item.imageTitle}`} key={`${projectId}-${item.imageTitle}`}
item={item} item={item}
depth={depth + 1} depth={depth + 1}
folderId={folderData.id}
selected={image?.id === item.id} selected={image?.id === item.id}
/> />
))} ))}

View File

@ -2,24 +2,28 @@ import { cn } from '@/lib/utils';
import { ImageResponse } from '@/types'; import { ImageResponse } from '@/types';
import { ArrowDownToLine, Check, Image, Loader, Minus, Send } from 'lucide-react'; import { ArrowDownToLine, Check, Image, Loader, Minus, Send } from 'lucide-react';
import useCanvasStore from '@/stores/useCanvasStore'; import useCanvasStore from '@/stores/useCanvasStore';
import useProjectStore from '@/stores/useProjectStore';
export default function ProjectFileItem({ export default function ProjectFileItem({
className = '', className = '',
item, item,
folderId = 0,
depth = 0, depth = 0,
selected, selected,
}: { }: {
className?: string; className?: string;
item: ImageResponse; item: ImageResponse;
folderId?: number;
depth?: number; depth?: number;
selected: boolean; selected: boolean;
}) { }) {
const paddingLeft = depth * 12; const paddingLeft = depth * 12;
// const changeImage = useCanvasStore((state) => state.changeImage);
const setImage = useCanvasStore((state) => state.setImage); const setImage = useCanvasStore((state) => state.setImage);
const setFolderId = useProjectStore((state) => state.setFolderId);
const handleClick = () => { const handleClick = () => {
setImage(item); setImage(item);
setFolderId(folderId);
}; };
return ( return (

View File

@ -0,0 +1,9 @@
import { saveImageLabels } from '@/api/lablingApi';
import { useMutation } from '@tanstack/react-query';
export default function useSaveImageLabelsQuery() {
return useMutation({
mutationFn: ({ projectId, imageId, data }: { projectId: number; imageId: number; data: { data: string } }) =>
saveImageLabels(projectId, imageId, data),
});
}

View File

@ -4,11 +4,15 @@ import { Project } from '@/types';
interface ProjectState { interface ProjectState {
project: Project | null; project: Project | null;
setProject: (project: Project | null) => void; setProject: (project: Project | null) => void;
folderId: number;
setFolderId: (folderId: number) => void;
} }
const useProjectStore = create<ProjectState>((set) => ({ const useProjectStore = create<ProjectState>((set) => ({
project: null, project: null,
setProject: (project) => set({ project }), setProject: (project) => set({ project }),
folderId: 0,
setFolderId: (folderId) => set({ folderId }),
})); }));
export default useProjectStore; export default useProjectStore;