Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish the Post #8

Open
cca-Parth opened this issue Dec 12, 2023 · 3 comments
Open

Publish the Post #8

cca-Parth opened this issue Dec 12, 2023 · 3 comments

Comments

@cca-Parth
Copy link

There is no option to publish the Post, When I create a new post, the status is always in Draft, so how do I publish it?

@husleuujii
Copy link

Did u test your code?

@husleuujii
Copy link

husleuujii commented Mar 25, 2024

there is no code for publishing post to posts table. Please commit latest updates..

@raphaelandrews
Copy link

There's a comment here with a way to publish Posts, but it looks like it has been deleted.

You can drop the Drafts table and create an Action to publish posts:

"use server";

import * as z from "zod";
import { cookies } from "next/headers";

import { createClient } from "@/utils/supabase/server";
import { postPublishSchema } from "@/lib/validation/post";

export async function PublishPost(context: z.infer<typeof postPublishSchema>) {
  const cookieStore = cookies();
  const supabase = createClient(cookieStore);
  try {
    const post = postPublishSchema.parse(context);

    const { data, error } = await supabase
      .from("posts")
      .update({
        published: post.published,
      })
      .match({ id: post.id })
      .select()
      .single();

    if (error) {
      console.error(error);
      return null;
    }
    return data;
  } catch (error) {
    console.error(error);
    return null;
  }
}

And add this function to post-edit-button.tsx

  async function publishMyPost() {
    setShowLoadingAlert(true);
    if (id && session?.user.id) {
      const myPostData = {
        id: id,
        published: true,
      };
      const response = await PublishPost(myPostData);
      if (response) {
        setShowLoadingAlert(false);
        toast.success(protectedPostConfig.successPostPublished);
        router.refresh();
      } else {
        setShowLoadingAlert(false);
        toast.error(protectedPostConfig.errorUpdate);
      }
    } else {
      setShowLoadingAlert(false);
      toast.error(protectedPostConfig.errorUpdate);
    }
  }

Remember to change the code in the create-posts.ts to .from("posts")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants