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

Adding an image for Posts with Cloudinary support #86

Open
StarAzure opened this issue Jul 23, 2020 · 1 comment
Open

Adding an image for Posts with Cloudinary support #86

StarAzure opened this issue Jul 23, 2020 · 1 comment

Comments

@StarAzure
Copy link

StarAzure commented Jul 23, 2020

I am working on trying to add an image for posts.

In Editor.jsx, I added

async function hanldeSubmit(e) {
      ..
      dealPic: e.currentTarget.dealPic.value,
    };

   <label htmlFor="dealPic">
            Post Pic
            <input
              type="file"
              id="dealPic"
              name="dealPic"
              accept="image/png, image/jpeg"
             // ref={dealPicRef}
            />
          </label>

And in api/posts/index.js

`handler.post(async (req, res) => {
  if (!req.user) {
    return res.status(401).send('unauthenticated');
  }

  const { title } = req.body;
  const { content } = req.body;
  const { dealPic } = req.body;

 ..
  if (!dealPic) return res.status(400).send('Please choose an image');

  const post = {
    _id: nanoid(),
    title,
    content,
    dealPic,
    createdAt: new Date(),
    creatorId: req.user._id,
  };`

This adds dealPic value as an image name in the database.

How to make this work as an image uploaded to cloudinary? I refered settings.jsx but could not see how this is done.

Appreciate the suggestions.

@StarAzure StarAzure changed the title Adding an image for Post with Cloudinary support Adding an image for Posts with Cloudinary support Jul 23, 2020
@StarAzure
Copy link
Author

I was able to implement this the same way user profile pic is done but I am using mutate. I hope using mutate for post wont cause any issues but its working.

export function usePoster() { const { data, mutate } = useSWR('/api/posts', fetcher); const posts = data?.posts; return [posts, { mutate }]; }

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

1 participant