Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Simplify image upload for admin in products #74

Conversation

s-vamshi
Copy link
Contributor

@s-vamshi s-vamshi commented Oct 5, 2022

Simplifying product image upload process for admins by using multer-storage-cloudinary npm package

@SyedMuzamilM
Copy link

Code looks ok. Can you share a demo video so that we can see how is it working. Try to upload an image and then show the product page for which you uploaded this image.

@s-vamshi

This comment was marked as resolved.

@roopeshsn
Copy link
Owner

Thanks for bearing with me @s-vamshi I'll summarize what you've done. If I am wrong correct me.

  • The first step is getting the image from the user and immediately uploading it to the Cloudinary storage.
  • Then the app is waiting to receive the image URL from the Cloudinary.
  • Once the URL is received then along with other data like name, price, etc. will be saved to the DB.

Am I right?

I have some questions to address.

  • If the user not selecting the image then what will be the message shown to the user?
  • What if Cloudinary service is down? How the app handles it?

You have used the try-catch block. It will be nice if you simulate an error and add a screenshot if possible.

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 7, 2022

@roopeshsn
No problem.
Yes you are right.

  1. If the user is not selecting the image this message is shown -
    image
  2. Tbh I don't know how the app will respond when cloudinary service is down, I need to check if there is any way to simulate if service is down.
  3. Yes I will simulate and add a screenshot

@roopeshsn
Copy link
Owner

roopeshsn commented Oct 7, 2022

No simulating Cloudinary error is not required. Is there any way to change the error message meaningful?

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 7, 2022

Yes i am working on it.

@roopeshsn
Copy link
Owner

Also, have you taken care of when the admin wants to edit the product image?

@roopeshsn
Copy link
Owner

roopeshsn commented Oct 7, 2022

@roopeshsn No problem. Yes you are right.

  1. If the user is not selecting the image this message is shown -
    image
  2. Tbh I don't know how the app will respond when cloudinary service is down, I need to check if there is any way to simulate if service is down.
  3. Yes I will simulate and add a screenshot

No, I am asking about what will be the case if the user selects a video or a text file.

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 7, 2022

Also, have you taken care of when the admin wants to edit the product image?

NO
Thanks for pointing this out, I will start working on product edit page also.

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 7, 2022

@roopeshsn No problem. Yes you are right.

  1. If the user is not selecting the image this message is shown -
    image
  2. Tbh I don't know how the app will respond when cloudinary service is down, I need to check if there is any way to simulate if service is down.
  3. Yes I will simulate and add a screenshot

No, I am asking about what will be the case if the user selects a video or a text file.

It will not upload to cloud hence image Src state will be an empty string
And this is how it looks without any error msg we need to display an error message right?
tem11

@roopeshsn
Copy link
Owner

@roopeshsn No problem. Yes you are right.

  1. If the user is not selecting the image this message is shown -
    image
  2. Tbh I don't know how the app will respond when cloudinary service is down, I need to check if there is any way to simulate if service is down.
  3. Yes I will simulate and add a screenshot

No, I am asking about what will be the case if the user selects a video or a text file.

It will not upload to cloud hence image Src state will be an empty string And this is how it looks without any error msg we need to display an error message right? tem11

Yes, we need to take care of all the edge cases.

@roopeshsn
Copy link
Owner

As of now, I will merge the PR up to what you've done. Y After that you can work on the edit product route. What's your thought?

@@ -1,20 +1,25 @@
const path = require('path')
const express = require('express')
const multer = require('multer')
const cloudinary = require("cloudinary").v2
const { CloudinaryStorage } = require("multer-storage-cloudinary")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did this package do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of storage to local system using multer we are using a similar package called multer storage cloudinary to store images directly in cloud.
And to upload images we need api keys so we need to use cloudinary package.

null,
`${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`,
)
cloudinary.config({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a config folder in the backend. Is it possible to change the config code there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yes can i make all this changes tomorrow?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Take your own time. I highly encourage you to contribute in your free time!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roopeshsn I have moved cloud config to db/config.

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 8, 2022

No simulating Cloudinary error is not required. Is there any way to change the error message meaningful?
@roopeshsn
Modified all error messages while submitting form
tem12
temp11

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 8, 2022

No, I am asking about what will be the case if the user selects a video or a text file.

Made changes now when user uploads other files displays this msg -
image

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 8, 2022

Also, have you taken care of when the admin wants to edit the product image?

@roopeshsn This is the change i have made in edit product page. Is it ok?
image

@roopeshsn
Copy link
Owner

Also, have you taken care of when the admin wants to edit the product image?

@roopeshsn This is the change i have made in edit product page. Is it ok? image

There is no need to render the previous product image.

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 8, 2022

There is no need to render the previous product image.

@roopeshsn removed previous product image.
But found a bug which is not related to current issue or commits.
So the bug is when we are in admin/productlist route, when we click on edit button and update a product then we will get again redirected to admin/productlist. Here starts the problem now when we want to edit any other product we will get an error Uncaught TypeError: Cannot read properties of undefined (reading 'name') at ProductEditScreen.js:47:1

@roopeshsn
Copy link
Owner

There is no need to render the previous product image.

@roopeshsn removed previous product image. But found a bug which is not related to current issue or commits. So the bug is when we are in admin/productlist route, when we click on edit button and update a product then we will get again redirected to admin/productlist. Here starts the problem now when we want to edit any other product we will get an error Uncaught TypeError: Cannot read properties of undefined (reading 'name') at ProductEditScreen.js:47:1

Yes, this bug persists. If possible could you create an issue for the same?

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 8, 2022

Ok. I will create an issue and can you review my PR ?

@roopeshsn
Copy link
Owner

How we can handle this? If I am a product is getting updated with a new image and then the old image should get deleted in Cloudinary. Would like to know your thoughts.

@roopeshsn
Copy link
Owner

Ok. I will create an issue and can you review my PR ?

So updating a product with a new image is working fine right?

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 8, 2022

How we can handle this? If I am a product is getting updated with a new image and then the old image should get deleted in Cloudinary. Would like to know your thoughts.

Ya this is a great idea I will start working on it.

@s-vamshi
Copy link
Contributor Author

s-vamshi commented Oct 8, 2022

Ok. I will create an issue and can you review my PR ?

So updating a product with a new image is working fine right?

Yes

@roopeshsn
Copy link
Owner

roopeshsn commented Oct 8, 2022

Wait, I will merge this PR then you can create a new issue and work on that. The code is not formatted properly. We have a prettier command to format recursively. Check the package.json file.

@roopeshsn roopeshsn merged commit 819541d into roopeshsn:master Oct 8, 2022
@roopeshsn
Copy link
Owner

Appreciate your work @s-vamshi

@roopeshsn roopeshsn added the hacktoberfest-accepted Accepted PR for hactoberfest label Oct 8, 2022
@s-vamshi s-vamshi deleted the simplify-image-upload-for-admin-in-products branch October 9, 2022 03:41
@s-vamshi s-vamshi restored the simplify-image-upload-for-admin-in-products branch October 9, 2022 05:06
@s-vamshi
Copy link
Contributor Author

How we can handle this? If I am a product is getting updated with a new image and then the old image should get deleted in Cloudinary. Would like to know your thoughts.

@roopeshsn should i start working on this feature or should i wait until 404 page feature gets merged?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
hacktoberfest-accepted Accepted PR for hactoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants