Skip to content

Commit

Permalink
feat: API 연결하여 노트 생성 및 이동 (#161)
Browse files Browse the repository at this point in the history
* feat: subspace node 추가

* refactor: autofit 로직 별도 훅으로 분리

* feat: 노드 생성 시 노트 생성 기능 구현

* feat: 노트 노드 클릭 시 해당 노트로 이동 기능 구현

* fix: 누락된 prop 전달 값 추가

---------

Co-authored-by: Hogyun Jeon <[email protected]>
  • Loading branch information
heegenie and hoqn authored Nov 28, 2024
1 parent 31d870b commit b6e586f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 17 deletions.
19 changes: 19 additions & 0 deletions packages/frontend/src/api/note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { API_V1_URL } from "./constants";
import http from "./http";

type CreateNoteRequestBody = {
userId: string;
noteName: string;
};

type CreateNoteResponseBody = {
urlPath: [string];
};

export async function createNote(body: CreateNoteRequestBody) {
const response = await http.post<CreateNoteResponseBody>(
`${API_V1_URL}/note`,
{ body: JSON.stringify(body) },
);
return response.data;
}
5 changes: 3 additions & 2 deletions packages/frontend/src/components/Node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ export type NoteNodeProps = {
name: string;
} & NodeHandlers;

export function NoteNode({ x, y, name, ...rest }: NoteNodeProps) {
export function NoteNode({ x, y, name, src, ...rest }: NoteNodeProps) {
// TODO: src 적용 필요
const navigate = useNavigate();
const radius = 64;
return (
<Node x={x} y={y} {...rest}>
<Node x={x} y={y} onClick={() => navigate(`/note/${src}`)} {...rest}>
<Node.Circle radius={radius} fill="#FFF2CB" />
<Node.Text fontSize={16} content={name} />
</Node>
Expand Down
31 changes: 17 additions & 14 deletions packages/frontend/src/components/space/SpaceView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Html } from "react-konva-utils";
import Konva from "konva";
import type { Node } from "shared/types";

import { createNote } from "@/api/note";
import { createSpace } from "@/api/space";
import Edge from "@/components/Edge";
import { HeadNode, NoteNode, SubspaceNode } from "@/components/Node";
Expand Down Expand Up @@ -38,18 +39,21 @@ export default function SpaceView({ spaceId, autofitTo }: SpaceViewProps) {
const { drag, dropPosition, handlePaletteSelect } = useDragNode(nodesArray, {
createNode: (type, parentNode, position, name = "New Note") => {
if (type === "note") {
let src = "";
// FIXME: note 생성 후 id 입력
defineNode(
{
type,
x: position.x,
y: position.y,
name,
src,
},
parentNode.id,
);
createNote({
userId: "honeyflow",
noteName: name,
}).then((res) => {
defineNode(
{
type,
x: position.x,
y: position.y,
name,
src: res.urlPath.toString(),
},
parentNode.id,
);
});

return;
}
Expand Down Expand Up @@ -87,7 +91,6 @@ export default function SpaceView({ spaceId, autofitTo }: SpaceViewProps) {
parentNode.id,
);
},

createEdge: (fromNode, toNode) => {
defineEdge(fromNode.id, toNode.id);
},
Expand All @@ -113,7 +116,7 @@ export default function SpaceView({ spaceId, autofitTo }: SpaceViewProps) {
x={node.x}
y={node.y}
name={node.name}
src=""
src={node.src}
onDragStart={() => handlers.onDragStart(node)}
onDragMove={handlers.onDragMove}
onDragEnd={handlers.onDragEnd}
Expand Down
24 changes: 24 additions & 0 deletions packages/frontend/src/components/space/YjsSpaceView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { RefObject } from "react";

import useYjsConnection from "@/hooks/yjs/useYjsConnection";
import { YjsStoreProvider } from "@/store/yjs";

import SpaceView from "./SpaceView";

type YjsSpaceViewProps = {
spaceId: string;
autofitTo?: Element | RefObject<Element>;
};

export default function YjsSpaceView({
spaceId,
autofitTo,
}: YjsSpaceViewProps) {
const { yDoc, yProvider, setYDoc, setYProvider } = useYjsConnection(spaceId);

return (
<YjsStoreProvider value={{ yDoc, yProvider, setYDoc, setYProvider }}>
<SpaceView spaceId={spaceId} autofitTo={autofitTo} />
</YjsStoreProvider>
);
}
2 changes: 1 addition & 1 deletion packages/frontend/src/pages/Space.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function SpacePage() {
return (
<YjsStoreProvider value={{ yDoc, yProvider, setYDoc, setYProvider }}>
<div className="w-full h-full" ref={containerRef}>
<SpaceView autofitTo={containerRef} />
<SpaceView spaceId={spaceId} autofitTo={containerRef} />
<SpacePageHeader />
</div>
</YjsStoreProvider>
Expand Down

0 comments on commit b6e586f

Please sign in to comment.