Skip to content

Commit

Permalink
feat: ✨ create post on paste if authenticated user isn't author
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan6erbond committed May 7, 2023
1 parent 2cb990d commit 1b1a274
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/pages/posts/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { GetServerSideProps } from "next";
import { useRouter } from "next/router";
import { Record } from "pocketbase";
import { useCallback, useEffect, useState } from "react";
import { useCreatePost } from "../../hooks/useCreatePost";

interface PostProps {
title: string;
Expand Down Expand Up @@ -91,7 +92,35 @@ export default function Post(props: PostProps) {
setValues(record);
});

usePasteFiles({ acceptTypes: MEDIA_MIME_TYPE, onPaste: uploadFiles });
const { createPost: _createPost } = useCreatePost({
acceptTypes: MEDIA_MIME_TYPE,
});

const createPost = (files: File[]) =>
_createPost({
title: "",
author: user?.id!,
files: files.map((file) => ({
file: file,
name: file.name,
author: user?.id!,
description: "",
})),
}).then(async (post) => {
if (!post) {
return;
}

router.push("/posts/" + post.id);
});

usePasteFiles({
acceptTypes: MEDIA_MIME_TYPE,
onPaste: (files) =>
post?.author === user?.id
? uploadFiles(files)
: user?.id && createPost(files),
});

useEffect(() => {
setBlurred(files.map(() => nsfw));
Expand Down

0 comments on commit 1b1a274

Please sign in to comment.