fix: image upload (#642)

This commit is contained in:
Aaryan Khandelwal 2023-03-31 04:24:57 +05:30 committed by GitHub
parent a8f125cfa8
commit b2c15125fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 9 deletions

View File

@ -57,9 +57,17 @@ export const ImageUploadModal: React.FC<Props> = ({
.uploadUserFile(formData) .uploadUserFile(formData)
.then((res) => { .then((res) => {
const imageUrl = res.asset; const imageUrl = res.asset;
onSuccess(imageUrl); onSuccess(imageUrl);
setIsImageUploading(false); setIsImageUploading(false);
setImage(null); setImage(null);
if (value) {
const index = value.indexOf(".com");
const asset = value.substring(index + 5);
fileServices.deleteUserFile(asset);
}
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
@ -72,6 +80,13 @@ export const ImageUploadModal: React.FC<Props> = ({
onSuccess(imageUrl); onSuccess(imageUrl);
setIsImageUploading(false); setIsImageUploading(false);
setImage(null); setImage(null);
if (value) {
const index = value.indexOf(".com");
const asset = value.substring(index + 5);
fileServices.deleteFile(asset);
}
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
@ -157,7 +172,11 @@ export const ImageUploadModal: React.FC<Props> = ({
</div> </div>
<div className="mt-5 sm:mt-6 sm:grid sm:grid-flow-row-dense sm:grid-cols-2 sm:gap-3"> <div className="mt-5 sm:mt-6 sm:grid sm:grid-flow-row-dense sm:grid-cols-2 sm:gap-3">
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton> <SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
<PrimaryButton onClick={handleSubmit} loading={isImageUploading || !image}> <PrimaryButton
onClick={handleSubmit}
disabled={!image}
loading={isImageUploading}
>
{isImageUploading ? "Uploading..." : "Upload & Save"} {isImageUploading ? "Uploading..." : "Upload & Save"}
</PrimaryButton> </PrimaryButton>
</div> </div>

View File

@ -79,13 +79,13 @@ const Profile: NextPage = () => {
message: "Profile updated successfully.", message: "Profile updated successfully.",
}); });
}) })
.catch(() => { .catch(() =>
setToastAlert({ setToastAlert({
type: "error", type: "error",
title: "Error!", title: "Error!",
message: "There was some error in updating your profile. Please try again.", message: "There was some error in updating your profile. Please try again.",
}); })
}); );
}; };
const handleDelete = (url: string | null | undefined, updateUser: boolean = false) => { const handleDelete = (url: string | null | undefined, updateUser: boolean = false) => {
@ -101,7 +101,6 @@ const Profile: NextPage = () => {
userService userService
.updateUser({ avatar: "" }) .updateUser({ avatar: "" })
.then((res) => { .then((res) => {
setIsRemoving(false);
setToastAlert({ setToastAlert({
type: "success", type: "success",
title: "Success!", title: "Success!",
@ -113,13 +112,13 @@ const Profile: NextPage = () => {
}, false); }, false);
}) })
.catch(() => { .catch(() => {
setIsRemoving(false);
setToastAlert({ setToastAlert({
type: "error", type: "error",
title: "Error!", title: "Error!",
message: "There was some error in deleting your profile picture. Please try again.", message: "There was some error in deleting your profile picture. Please try again.",
}); });
}); })
.finally(() => setIsRemoving(false));
}); });
}; };
@ -140,7 +139,6 @@ const Profile: NextPage = () => {
isOpen={isImageUploadModalOpen} isOpen={isImageUploadModalOpen}
onClose={() => setIsImageUploadModalOpen(false)} onClose={() => setIsImageUploadModalOpen(false)}
onSuccess={(url) => { onSuccess={(url) => {
handleDelete(myProfile?.avatar);
setValue("avatar", url); setValue("avatar", url);
handleSubmit(onSubmit)(); handleSubmit(onSubmit)();
setIsImageUploadModalOpen(false); setIsImageUploadModalOpen(false);

View File

@ -124,7 +124,6 @@ const WorkspaceSettings: NextPage<UserAuth> = (props) => {
onClose={() => setIsImageUploadModalOpen(false)} onClose={() => setIsImageUploadModalOpen(false)}
onSuccess={(imageUrl) => { onSuccess={(imageUrl) => {
setIsImageUploading(true); setIsImageUploading(true);
handleDelete(activeWorkspace?.logo);
setValue("logo", imageUrl); setValue("logo", imageUrl);
setIsImageUploadModalOpen(false); setIsImageUploadModalOpen(false);
handleSubmit(onSubmit)().then(() => setIsImageUploading(false)); handleSubmit(onSubmit)().then(() => setIsImageUploading(false));