diff --git a/common/post.ts b/common/post.ts index d2a4e539..05eab685 100644 --- a/common/post.ts +++ b/common/post.ts @@ -2,11 +2,11 @@ import { JSONContent } from '@tiptap/core' export type Post = { id: string - name: string + title: string content: JSONContent creatorId: string // User id createdTime: number slug: string } -export const MAX_POST_NAME_LENGTH = 480 +export const MAX_POST_TITLE_LENGTH = 480 diff --git a/functions/src/create-post.ts b/functions/src/create-post.ts index b15348c7..40d39bba 100644 --- a/functions/src/create-post.ts +++ b/functions/src/create-post.ts @@ -3,7 +3,7 @@ import * as admin from 'firebase-admin' import { getUser } from './utils' import { slugify } from '../../common/util/slugify' import { randomString } from '../../common/util/random' -import { Post, MAX_POST_NAME_LENGTH } from '../../common/post' +import { Post, MAX_POST_TITLE_LENGTH } from '../../common/post' import { APIError, newEndpoint, validate } from './api' import { JSONContent } from '@tiptap/core' import { z } from 'zod' @@ -32,21 +32,21 @@ const contentSchema: z.ZodType = z.lazy(() => ) const postSchema = z.object({ - name: z.string().min(1).max(MAX_POST_NAME_LENGTH), + title: z.string().min(1).max(MAX_POST_TITLE_LENGTH), content: contentSchema, }) export const createpost = newEndpoint({}, async (req, auth) => { const firestore = admin.firestore() - const { name, content } = validate(postSchema, req.body) + const { title, content } = validate(postSchema, req.body) const creator = await getUser(auth.uid) if (!creator) throw new APIError(400, 'No user exists with the authenticated user ID.') - console.log('creating post owned by', creator.username, 'named', name) + console.log('creating post owned by', creator.username, 'titled', title) - const slug = await getSlug(name) + const slug = await getSlug(title) const postRef = firestore.collection('posts').doc() @@ -54,18 +54,18 @@ export const createpost = newEndpoint({}, async (req, auth) => { id: postRef.id, creatorId: creator.id, slug, - name, + title, createdTime: Date.now(), content: content, } await postRef.create(post) - return { status: 'success', post: post } + return { status: 'success', post } }) -export const getSlug = async (name: string) => { - const proposedSlug = slugify(name) +export const getSlug = async (title: string) => { + const proposedSlug = slugify(title) const preexistingPost = await getPostFromSlug(proposedSlug) diff --git a/web/lib/firebase/api.ts b/web/lib/firebase/api.ts index 0364a7cc..6b1b43d8 100644 --- a/web/lib/firebase/api.ts +++ b/web/lib/firebase/api.ts @@ -1,5 +1,6 @@ import { auth } from './users' import { APIError, getFunctionUrl } from 'common/api' +import { JSONContent } from '@tiptap/core' export { APIError } from 'common/api' export async function call(url: string, method: string, params: any) { @@ -89,6 +90,6 @@ export function getCurrentUser(params: any) { return call(getFunctionUrl('getcurrentuser'), 'GET', params) } -export function createPost(params: any) { +export function createPost(params: { title: string; content: JSONContent }) { return call(getFunctionUrl('createpost'), 'POST', params) } diff --git a/web/pages/create-post.tsx b/web/pages/create-post.tsx index 0b7e6eb1..f88f56a5 100644 --- a/web/pages/create-post.tsx +++ b/web/pages/create-post.tsx @@ -7,26 +7,25 @@ import Textarea from 'react-expanding-textarea' import { TextEditor, useTextEditor } from 'web/components/editor' import { createPost } from 'web/lib/firebase/api' import clsx from 'clsx' -import { useRouter } from 'next/router' -import { Post, MAX_POST_NAME_LENGTH } from 'common/post' +import Router from 'next/router' +import { MAX_POST_TITLE_LENGTH } from 'common/post' import { postPath } from 'web/lib/firebase/posts' export default function CreatePost() { - const [name, setName] = useState('') + const [title, setTitle] = useState('') const [error, setError] = useState('') const [isSubmitting, setIsSubmitting] = useState(false) - const router = useRouter() const { editor, upload } = useTextEditor({ disabled: isSubmitting, }) - const isValid = editor && name.length > 0 && editor.isEmpty === false + const isValid = editor && title.length > 0 && editor.isEmpty === false - async function savePost(name: string) { + async function savePost(title: string) { if (!editor) return const newPost = { - name: name, + title: title, content: editor.getJSON(), } @@ -36,7 +35,7 @@ export default function CreatePost() { return e }) if (result.post) { - await router.push(postPath((result.post as Post).slug)) + await Router.push(postPath(result.post.slug)) } } @@ -49,21 +48,21 @@ export default function CreatePost() {