@@ -4,7 +4,10 @@ import { useController } from "react-hook-form";
4
4
import { useDropzone } from "react-dropzone" ;
5
5
import imageCompression from "browser-image-compression" ;
6
6
import Swal from "sweetalert2" ;
7
- import axios from "axios" ;
7
+
8
+ const validateImg = ( url : string ) => {
9
+ return url . match ( / ( h t t p s ? : \/ \/ .* \. (?: p n g | j p g | j p e g | g i f ) ) / ) != null ;
10
+ } ;
8
11
9
12
const DropZone = ( {
10
13
control,
@@ -14,8 +17,8 @@ const DropZone = ({
14
17
} : {
15
18
control : any ;
16
19
name : string ;
17
- rules : any ;
18
- defaultValue ?: any ;
20
+ rules ? : any ;
21
+ defaultValue ?: string ;
19
22
} ) => {
20
23
// yep ini buat nge hook ke react-hook-form
21
24
const {
@@ -25,17 +28,12 @@ const DropZone = ({
25
28
control,
26
29
name,
27
30
rules,
28
- defaultValue : defaultValue
29
- ? axios
30
- . get ( defaultValue )
31
- . then ( ( resp ) => resp . data )
32
- . catch ( ( err ) => defaultValue )
33
- : undefined ,
34
31
} ) ;
35
32
36
33
const { getRootProps, getInputProps } = useDropzone ( {
37
34
maxFiles : 1 ,
38
- accept : { "image/jpeg" : [ ] , "image/jpg" : [ ] , "image/png" : [ ] } ,
35
+ // "image/jpg": [".jpg"],
36
+ accept : { "image/jpeg" : [ ".jpeg" , ".jpg" ] , "image/png" : [ ".png" ] } ,
39
37
onDrop : ( acceptedFiles : File [ ] ) => {
40
38
if ( acceptedFiles . length ) {
41
39
imageCompression ( acceptedFiles [ 0 ] , {
@@ -88,6 +86,28 @@ const DropZone = ({
88
86
</ Box >
89
87
) ;
90
88
89
+ const before = defaultValue && validateImg ( defaultValue ) && (
90
+ < Box
91
+ display = { "inline-flex" }
92
+ borderRadius = { 4 }
93
+ border = { "2px solid #eaeaea" }
94
+ mt = { "16px" }
95
+ mx = { 1 }
96
+ w = { "auto" }
97
+ h = { "auto" }
98
+ p = { 1 }
99
+ boxSizing = { "border-box" }
100
+ >
101
+ < Box display = { "flex" } w = { "auto" } h = { "100%" } >
102
+ < Image
103
+ src = { defaultValue }
104
+ style = { { display : "block" , width : "auto" , height : "100%" } }
105
+ alt = { "before" }
106
+ />
107
+ </ Box >
108
+ </ Box >
109
+ ) ;
110
+
91
111
return (
92
112
< >
93
113
< Center
@@ -106,7 +126,7 @@ const DropZone = ({
106
126
</ Text >
107
127
</ Center >
108
128
< Box display = { "flex" } flexDirection = { "row" } flexWrap = { "wrap" } >
109
- { thumb }
129
+ { value ? thumb : before }
110
130
</ Box >
111
131
</ >
112
132
) ;
0 commit comments