Skip to content

Commit 344d4c5

Browse files
committed
feat: 작성한 글 저장
1 parent 8b5f76a commit 344d4c5

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

components/write/Editor.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import dynamic from 'next/dynamic';
44
import { useState } from 'react';
55
import type { MDEditorProps } from '@uiw/react-md-editor';
66
import SaveButton from '@/components/write/SaveButton';
7+
import { createPost } from '@/services/write/post.service';
8+
import { auth } from '@/lib/firebase';
79

810
const MDEditor = dynamic<MDEditorProps>(() => import('@uiw/react-md-editor'), {
911
ssr: false,
@@ -16,7 +18,26 @@ const Editor = () => {
1618
setValue('');
1719
};
1820

19-
const onClickSave = () => {};
21+
const onClickSave = async () => {
22+
const user = auth.currentUser;
23+
if (!user) {
24+
alert('로그인이 필요합니다');
25+
return;
26+
}
27+
if (!value.trim()) {
28+
alert('내용을 입력하세요');
29+
return;
30+
}
31+
32+
try {
33+
const id = await createPost(user.uid, value);
34+
alert('저장 완료!');
35+
console.log('postId:', id);
36+
} catch (e) {
37+
console.error(e);
38+
alert('저장 실패');
39+
}
40+
};
2041

2142
return (
2243
<div

services/write/post.service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { collection, addDoc, serverTimestamp } from 'firebase/firestore';
2+
import { db } from '@/lib/firebase';
3+
4+
export async function createPost(uid: string, content: string, title?: string) {
5+
const postsCol = collection(db, 'users', uid, 'posts');
6+
const docRef = await addDoc(postsCol, {
7+
title: title ?? '',
8+
content,
9+
createdAt: serverTimestamp(),
10+
updatedAt: serverTimestamp(),
11+
});
12+
return docRef.id;
13+
}

0 commit comments

Comments
 (0)