|
1
|
import { useCallback } from "react" |
|
2
|
import { UseFormReturn } from "react-hook-form" |
|
3
|
import { z } from "@medusajs/framework/zod" |
|
4
|
import { MediaSchema } from "../routes/gift-cards/gift-card-products/components/gift-card-product-create-form/schema" |
|
5
|
import { |
|
6
|
EditProductMediaSchemaType, |
|
7
|
ProductCreateSchemaType, |
|
8
|
} from "../routes/gift-cards/gift-card-products/components/gift-card-product-create-form/types" |
|
9
|
import { FileType, FileUpload } from "./file-upload" |
|
10
|
import { Form } from "./form" |
|
11
|
|
|
12
|
type Media = z.infer<typeof MediaSchema> |
|
13
|
|
|
14
|
const SUPPORTED_FORMATS = [ |
|
15
|
"image/jpeg", |
|
16
|
"image/png", |
|
17
|
"image/gif", |
|
18
|
"image/webp", |
|
19
|
"image/heic", |
|
20
|
"image/svg+xml", |
|
21
|
] |
|
22
|
|
|
23
|
const SUPPORTED_FORMATS_FILE_EXTENSIONS = [ |
|
24
|
".jpeg", |
|
25
|
".png", |
|
26
|
".gif", |
|
27
|
".webp", |
|
28
|
".heic", |
|
29
|
".svg", |
|
30
|
] |
|
31
|
|
|
32
|
export const UploadMediaFormItem = ({ |
|
33
|
form, |
|
34
|
append, |
|
35
|
showHint = true, |
|
36
|
}: { |
|
37
|
form: |
|
38
|
| UseFormReturn<ProductCreateSchemaType> |
|
39
|
| UseFormReturn<EditProductMediaSchemaType> |
|
40
|
append: (value: Media) => void |
|
41
|
showHint?: boolean |
|
42
|
}) => { |
|
43
|
const hasInvalidFiles = useCallback( |
|
44
|
(fileList: FileType[]) => { |
|
45
|
const invalidFile = fileList.find( |
|
46
|
(f) => !SUPPORTED_FORMATS.includes(f.file.type) |
|
47
|
) |
|
48
|
|
|
49
|
if (invalidFile) { |
|
50
|
form.setError("media", { |
|
51
|
type: "invalid_file", |
|
52
|
message: `"'${ |
|
53
|
invalidFile.file.name |
|
54
|
}' is not a supported file type. Supported file types are: ${SUPPORTED_FORMATS_FILE_EXTENSIONS.join( |
|
55
|
", " |
|
56
|
)}.",`, |
|
57
|
}) |
|
58
|
|
|
59
|
return true |
|
60
|
} |
|
61
|
|
|
62
|
return false |
|
63
|
}, |
|
64
|
[form] |
|
65
|
) |
|
66
|
|
|
67
|
const onUploaded = useCallback( |
|
68
|
(files: FileType[]) => { |
|
69
|
form.clearErrors("media") |
|
70
|
if (hasInvalidFiles(files)) { |
|
71
|
return |
|
72
|
} |
|
73
|
|
|
74
|
files.forEach((f) => append({ ...f, isThumbnail: false })) |
|
75
|
}, |
|
76
|
[form, append, hasInvalidFiles] |
|
77
|
) |
|
78
|
|
|
79
|
return ( |
|
80
|
<Form.Field |
|
81
|
control={ |
|
82
|
form.control as UseFormReturn<EditProductMediaSchemaType>["control"] |
|
83
|
} |
|
84
|
name="media" |
|
85
|
render={() => { |
|
86
|
return ( |
|
87
|
<Form.Item> |
|
88
|
<div className="flex flex-col gap-y-2"> |
|
89
|
<div className="flex flex-col gap-y-1"> |
|
90
|
<Form.Label optional>Media</Form.Label> |
|
91
|
{showHint && ( |
|
92
|
<Form.Hint>Upload images for the gift card product</Form.Hint> |
|
93
|
)} |
|
94
|
</div> |
|
95
|
<Form.Control> |
|
96
|
<FileUpload |
|
97
|
label="Upload images" |
|
98
|
hint="Upload images for the gift card product" |
|
99
|
hasError={!!form.formState.errors.media} |
|
100
|
formats={SUPPORTED_FORMATS} |
|
101
|
onUploaded={onUploaded} |
|
102
|
/> |
|
103
|
</Form.Control> |
|
104
|
<Form.ErrorMessage /> |
|
105
|
</div> |
|
106
|
</Form.Item> |
|
107
|
) |
|
108
|
}} |
|
109
|
/> |
|
110
|
) |
|
111
|
} |