trithuc-mvc-react 1.6.4 → 1.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -82,6 +82,10 @@ function EditorForm({ fields, submitRef }) {
82
82
  if (data[field]) {
83
83
  data[field] = JSON.stringify(data[field]);
84
84
  }
85
+ } else if (type == "avatar") {
86
+ if (data[field]?.urlFile) {
87
+ data[field] = data[field].urlFile;
88
+ }
85
89
  }
86
90
  return data;
87
91
  }, data);
@@ -20,6 +20,7 @@ import { useCallback, useEffect, } from "react";
20
20
  import moment from "moment/moment";
21
21
  import { DEFAULT_DATE_FORMAT } from "../../constants";
22
22
  import UploadMultipleFile from "./upload/UploadMultipleFile";
23
+ import RhfUploadAvatar from "./upload/RhfUploadAvatar";
23
24
 
24
25
 
25
26
  FormField.propTypes = {
@@ -276,6 +277,20 @@ function FormField({
276
277
  }}
277
278
  />
278
279
  );
280
+ case "avatar":
281
+ return (
282
+ <Controller
283
+ name={name}
284
+ control={control}
285
+ render={({ field, fieldState: { error } }) => {
286
+ return (
287
+ <>
288
+ <RhfUploadAvatar name={"AnhDaiDien"} />
289
+ </>
290
+ );
291
+ }}
292
+ />
293
+ );
279
294
  }
280
295
  }
281
296
 
@@ -0,0 +1,65 @@
1
+ import { useCallback } from "react";
2
+ import { uploadFile } from "@/apis/index";
3
+ import UploadAvatar from './UploadAvatar'
4
+ import { FormHelperText, Typography } from "@mui/material/index";
5
+ import bytesToSize from "@/utils/bytesToSize";
6
+ import { Controller, useFormContext } from "react-hook-form";
7
+
8
+ const RhfUploadAvatar = ({ name }) => {
9
+ const { control, setValue } = useFormContext();
10
+ const handleDrop = useCallback(
11
+ async (acceptedFiles) => {
12
+ const file = acceptedFiles[0];
13
+
14
+ const formData = new FormData();
15
+ formData.append("file", file);
16
+ const data = await uploadFile(formData);
17
+
18
+ if (file) {
19
+ setValue(name, {
20
+ ...file,
21
+ preview: URL.createObjectURL(file),
22
+ urlFile: JSON.parse(data[0])?.urlFile
23
+ });
24
+ }
25
+ },
26
+ [setValue, name]
27
+ );
28
+ return (
29
+ <Controller
30
+ control={control}
31
+ name={name}
32
+ render={({ field: { value }, fieldState: { error } }) => (
33
+ <>
34
+ <UploadAvatar
35
+ accept="image/*"
36
+ file={value}
37
+ maxSize={3145728}
38
+ onDrop={handleDrop}
39
+ error={Boolean(error)}
40
+ helperText={error?.message}
41
+ caption={
42
+ <Typography
43
+ variant="caption"
44
+ sx={{
45
+ mt: 2,
46
+ mx: "auto",
47
+ display: "block",
48
+ textAlign: "center",
49
+ color: "text.secondary"
50
+ }}
51
+ >
52
+ cho phép *.jpeg, *.jpg, *.png, *.gif
53
+ <br /> max size of {bytesToSize(3145728)}
54
+ </Typography>
55
+ }
56
+ />
57
+ <FormHelperText error sx={{ px: 2, textAlign: "center" }}>
58
+ {error?.message}
59
+ </FormHelperText>
60
+ </>
61
+ )}
62
+ />
63
+ );
64
+ };
65
+ export default RhfUploadAvatar;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trithuc-mvc-react",
3
- "version": "1.6.4",
3
+ "version": "1.6.5",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"