2021-05-20 12:03:08 +00:00
|
|
|
import React, { useState, useRef, useEffect } from "react";
|
2021-12-02 05:56:41 +00:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2021-05-20 12:03:08 +00:00
|
|
|
|
2021-12-02 05:56:41 +00:00
|
|
|
import { updatePhoto, removePhoto, updatePhotoId } from "actions/userActions";
|
|
|
|
|
import { getCurrentUser } from "selectors/usersSelectors";
|
|
|
|
|
import { USER_PHOTO_ASSET_URL } from "constants/userConstants";
|
feat: import changes batch 2 (#15722)
* Remove treedropdown from ads
* Change Treedropdown imports
* Remove Notification Banner, change imports
* Remove Toggle from ads
* Change toggle imports
* explicitly declare function argument types
* Remove Menu from ads
* Change menu imports
* Remove Spinner from ads
* Change spinner imports
* Remove Radio, import changes
* test: updated flaky test under default meta (#15707)
* updated flaky test
* Updated tests
* updated tests
* updated the tests
* updated tests
* Update constants.ts
* add more typecasting
* Remove ListSegmentHeader, import changes
* Remove TagInputComponent, import changes
* Remove Switch, import changes
* Remove SearchInput, change imports
* Rename TagInputComponent to TagInput
* Remove ProgressiveImage, import changes
* import changes for SearchVariant
* Remove menu divider, import changes
* Remove TableDropdown, import changes
* Remove Switcher
* Remove StatusBar, import changes
* Remove showcase carousel
* Remove RectangularSwitcher, import change
* Add types to TableDropdown's args
* Remove MultiSwitch, import change
* Remove GifPlayerComponent, import change
* Remove DraggableList, import change
* Remove DisplayImageUpload, import change
* Remove DatePickerComponent, import change
* Remove CopyToClipBoard, import change
* Remove ColorSelector, import change
* Remove TabItemBackgroundFill, NumberedStep, ColorPickerComponent
* GifPlayerComponent -> GifPlayer
* change named import
* Remove FormFieldError, change imports
* Update to new version of Tree Dropdown
* Fix issue with ads/index.ts
* Test file fix
* Fix issue with merge?!?!??
* update design system to 1.0.18
* Bump ds version
* bump ds version
* bump ds version
Co-authored-by: NandanAnantharamu <67676905+NandanAnantharamu@users.noreply.github.com>
Co-authored-by: Albin <albin@appsmith.com>
2022-09-02 08:38:17 +00:00
|
|
|
import { DisplayImageUpload } from "design-system";
|
2021-05-20 12:03:08 +00:00
|
|
|
|
|
|
|
|
import Uppy from "@uppy/core";
|
|
|
|
|
|
|
|
|
|
function FormDisplayImage() {
|
|
|
|
|
const [file, setFile] = useState<any>();
|
|
|
|
|
const dispatch = useDispatch();
|
2021-12-02 05:56:41 +00:00
|
|
|
const user = useSelector(getCurrentUser);
|
2021-05-20 12:03:08 +00:00
|
|
|
const dispatchActionRef = useRef<(uppy: Uppy.Uppy) => void | null>();
|
|
|
|
|
|
2021-12-02 05:56:41 +00:00
|
|
|
const imageURL = user?.photoId
|
|
|
|
|
? `/api/${USER_PHOTO_ASSET_URL}/${user?.photoId}`
|
|
|
|
|
: "";
|
|
|
|
|
|
|
|
|
|
const onUploadComplete = (uppy: Uppy.Uppy, photoId: string) => {
|
2021-05-20 12:03:08 +00:00
|
|
|
uppy.reset();
|
2021-12-02 05:56:41 +00:00
|
|
|
dispatch(updatePhotoId({ photoId }));
|
2021-05-20 12:03:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSelectFile = (file: any) => {
|
|
|
|
|
setFile(file.data);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
dispatchActionRef.current = (uppy: Uppy.Uppy) => {
|
2021-12-02 05:56:41 +00:00
|
|
|
dispatch(
|
|
|
|
|
updatePhoto({
|
|
|
|
|
file,
|
|
|
|
|
callback: (photoId: string) => onUploadComplete(uppy, photoId),
|
|
|
|
|
}),
|
|
|
|
|
);
|
2021-05-20 12:03:08 +00:00
|
|
|
};
|
|
|
|
|
}, [file]);
|
|
|
|
|
|
|
|
|
|
const upload = (uppy: Uppy.Uppy) => {
|
|
|
|
|
if (typeof dispatchActionRef.current === "function")
|
|
|
|
|
dispatchActionRef.current(uppy);
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-16 18:36:34 +00:00
|
|
|
const removeProfileImage = () => {
|
|
|
|
|
dispatch(
|
2021-12-02 05:56:41 +00:00
|
|
|
removePhoto((photoId: string) => {
|
|
|
|
|
dispatch(updatePhotoId({ photoId }));
|
2021-06-16 18:36:34 +00:00
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
};
|
2021-05-20 12:03:08 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<DisplayImageUpload
|
|
|
|
|
onChange={onSelectFile}
|
2021-06-16 18:36:34 +00:00
|
|
|
onRemove={removeProfileImage}
|
2021-05-20 12:03:08 +00:00
|
|
|
submit={upload}
|
|
|
|
|
value={imageURL}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FormDisplayImage;
|