-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from anuraghazra/develop
refactor: refactored Room, MessageInput Components
- Loading branch information
Showing
5 changed files
with
175 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useCallback } from "react"; | ||
import { useDropzone } from "react-dropzone"; | ||
|
||
import { toast } from "@convoy-ui"; | ||
import { useUploadImageMutation } from "graphql/generated/graphql"; | ||
|
||
interface IUseImageUpload { | ||
value: string; | ||
setValue: (value: React.SetStateAction<string>) => void; | ||
} | ||
const useImageUpload = ({ value, setValue }: IUseImageUpload) => { | ||
// Image uploading | ||
const [ | ||
uploadImage, | ||
{ loading: uploadImageInProgress }, | ||
] = useUploadImageMutation({ | ||
onCompleted(data) { | ||
const replacedPlaceholder = value.replace( | ||
"![Uplading image...](...please wait)", | ||
`![Alt Text](${data.uploadImage.url})` | ||
); | ||
setValue && setValue(replacedPlaceholder); | ||
}, | ||
onError(err) { | ||
toast.error("Something went wrong uploading image."); | ||
}, | ||
}); | ||
|
||
const handleOnDrop = useCallback( | ||
acceptedFiles => { | ||
uploadImage({ | ||
variables: { | ||
file: acceptedFiles[0], | ||
}, | ||
}); | ||
}, | ||
[uploadImage] | ||
); | ||
|
||
const { getRootProps, getInputProps, open, isDragActive } = useDropzone({ | ||
onDrop: handleOnDrop, | ||
onDropAccepted: () => { | ||
setValue && setValue(value + `\n\n![Uplading image...](...please wait)`); | ||
}, | ||
accept: "image/jpeg, image/png", | ||
multiple: false, | ||
noClick: true, | ||
noKeyboard: true, | ||
}); | ||
|
||
return { | ||
open, | ||
isDragActive, | ||
getRootProps, | ||
getInputProps, | ||
uploadImageInProgress, | ||
}; | ||
}; | ||
|
||
export default useImageUpload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.