-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductImages.tsx
140 lines (138 loc) · 4.21 KB
/
ProductImages.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { Button, Loading, Modal, Text } from "@nextui-org/react";
import { useAppDispatch, useAppSelector } from "../../../state/hooks";
import {
setProductData,
setShowStep1,
setShowStep2,
} from "../../../state/reducers/createProductSlice";
import { ChangeEvent, MutableRefObject, useRef, useState } from "react";
import { NewProduct } from "../../../state/interfaces";
import { useCreateProductMutation } from "../../../state/reducers/api";
import confetti from "canvas-confetti";
export default function ProductImages({ product }: { product: NewProduct }) {
const newProduct = useAppSelector((state) => state.newProduct);
const dispatch = useAppDispatch();
const [createProduct, { error, isLoading }] = useCreateProductMutation();
const closeHandler = () => {
setImages([]);
dispatch(setShowStep2(false));
};
const fileRef = useRef() as MutableRefObject<HTMLInputElement>;
const [images, setImages] = useState<File[]>([]);
const handleNewImage = (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.files) {
const imgs = [];
for (
let i = 0;
i < (e.target.files.length > 4 ? 4 : e.target.files.length);
i++
) {
imgs[i] = e.target.files[i];
}
setImages(imgs);
}
};
const handleCreate = async () => {
const productIcon = product.images ? product.images : [];
const productImages = [...productIcon, ...images];
dispatch(setProductData({ ...product, images: productImages }));
setSubmit(true);
const formData = new FormData();
formData.set("name", product.name);
formData.set("description", product.description);
formData.set("accessToken", product.accessToken);
formData.set("private", product.private.toString());
for (let i = 0; i < productImages.length; i++) {
formData.append(`image${i + 1}`, productImages[i]);
}
await createProduct(formData);
setSubmit(false);
dispatch(setShowStep2(false));
setTimeout(
() =>
confetti({
zIndex: 999,
particleCount: 500,
spread: 170,
origin: { x: 0.5, y: 0.9 },
}),
500,
);
confetti.reset();
};
const [submit, setSubmit] = useState(false);
return (
<div>
<Modal
closeButton
blur
width={"50vw"}
aria-labelledby="modal-title"
open={newProduct.showStep2}
onClose={closeHandler}
>
<Modal.Header>
<Text id="modal-title" size={18}>
Add up to 4 images of your product
<Text b size={18} css={{ paddingLeft: 7 }}>
Product!
</Text>
</Text>
</Modal.Header>
<Modal.Body>
<div
onClick={() => fileRef.current.click()}
className={"h-14 p-4 rounded-box bg-red-400 text-white flex justify-center items-center"}
>
Select images
</div>
{images &&
images.map((i, idx) => (
<div
key={idx}
className={"flex justify-center w-full"}
>
{" "}
<img
src={URL.createObjectURL(i)}
className={"max-w-[89%] max-h-[80vh] object-contain rounded-box"}
/>
</div>
))}
<input
type={"file"}
multiple={true}
ref={fileRef}
hidden
onChange={(e) => handleNewImage(e)}
/>
</Modal.Body>
<Modal.Footer>
<Button
auto
flat
onClick={() => {
dispatch(setShowStep2(false));
dispatch(setShowStep1(true));
}}
>
Go Back
</Button>
<Button auto flat color="error" onClick={closeHandler}>
Cancel
</Button>
<Button auto clickable={!isLoading} onClick={handleCreate}>
{isLoading ? <Loading color="white" size="sm" /> : (
"CreateProduct!"
)}
</Button>
</Modal.Footer>
{error && (
<p className={"text-red-400 mb-3"}>
{error.data ? error.data.message : "error"}
</p>
)}
</Modal>
</div>
);
}