Spaces:
Sleeping
Sleeping
File size: 12,851 Bytes
49d3082 |
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
import React, { useState, useEffect } from "react";
import { VoiceAssistAtom, rollsAtom } from "../Variables";
import { useAtom } from "jotai";
function AddRollPage() {
const [name, setName] = useState("");
const [client, setClient] = useState("");
const [stock, setStock] = useState("");
const [description, setDescription] = useState("");
const [image, setImage] = useState(null);
const [success, setSuccess] = useState(null);
const [error, setError] = useState(null);
const [voiceAssist, setVoiceAssist] = useAtom(VoiceAssistAtom);
const [rolls, setRolls] = useAtom(rollsAtom);
useEffect(() => {
voiceAssist.on(["roll number *", "role number * ", "roll no. *", "role no. *"], true).then((i, wildcard) => {
setName(parseInt(wildcard));
});
voiceAssist.on(["client *"], true).then((i, wildcard) => {
setClient(wildcard);
});
voiceAssist.on(["stock *"], true).then((i, wildcard) => {
setStock(parseInt(wildcard));
});
voiceAssist.on(["description *"], true).then((i, wildcard) => {
setDescription(wildcard);
});
voiceAssist.on(["submit"]).then((i) => {
submitBtn.click();
});
}, []);
return (
<div className="flex flex-col lg:flex-row gap-10 items-center justify-center w-full h-full">
<div className="max-w-80 w-[80%] mx-auto lg:mx-0 lg:w-auto">
<h4 className="font-semibold text-primary-900">Upload Image</h4>
<div className="relative overflow-hidden bg-primary-200 hover:bg-primary-300/75 group/upload cursor-pointer border-2 border-dashed transition-all border-primary-900 rounded-lg w-full lg:w-64 h-16 lg:h-64 relativ flex flex-col items-center justify-center">
<input
onChange={(event) => {
let dataUri = URL.createObjectURL(event.target.files[0]);
setImage(dataUri);
}}
id="sampleImage"
type="file"
allow=".png,.jpg,.jpeg"
className="w-full h-full absolute top-0 left-0 opacity-0"
/>
{image === null ? (
<div>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" className="w-32 h-32 text-primary-800 transition-all group-hover/upload:text-primary-700 " xmlSpace="preserve">
<path d="M22.414 23.586a2 2 0 1 1-2.828 2.828L18 24.828V29a2 2 0 0 1-4 0v-4.172l-1.586 1.586a2 2 0 1 1-2.828-2.828l5-5a2 2 0 0 1 2.828 0zM26 10A9 9 0 0 0 8.2 8.12 6 6 0 1 0 7 20h4.758l2.121-2.121A2.98 2.98 0 0 1 16 17c.802 0 1.555.312 2.121.879L20.242 20H26a5 5 0 0 0 0-10" fill="currentColor" />
</svg>
<h6 className=" font-semibold text-primary-900 group-hover/upload:text-primary-800 transition-all -mb-3">Upload Image</h6>
</div>
) : (
<img src={image} className="w-full h-full object-cover"></img>
)}
</div>
</div>
<div className="relative flex flex-col bg-clip-border rounded-xl bg-transparent text-primary-700 shadow-none">
<h4 className="text-xl font-semibold text-primary-900 lg:w-full w-[80%] lg:mx-0 mx-auto">Add New Roll</h4>
<p className="text-primary-700 mt-1 font-normal lg:w-full w-[80%] lg:mx-0 mx-auto">Please fill in the form to register a new roll.</p>
{success && (
<div role="alert" className="relative w-full text-base font-regular px-4 py-4 mt-3 rounded-none border-l-4 border-green-600 bg-green-600/10 font-medium text-green-600 flex" style={{ opacity: 1 }}>
<div className="shrink-0">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" className="h-6 w-6">
<path fillRule="evenodd" d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12zm13.36-1.814a.75.75 0 10-1.22-.872l-3.236 4.53L9.53 12.22a.75.75 0 00-1.06 1.06l2.25 2.25a.75.75 0 001.14-.094l3.75-5.25z" clipRule="evenodd" />
</svg>
</div>
<div className="ml-3 mr-12 truncate" id="successMsg">
{success}
</div>
</div>
)}
{error && (
<div role="alert" className="relative w-full text-base font-regular px-4 py-4 mt-3 rounded-none border-l-4 border-red-500 bg-red-500/10 font-medium text-red-500 flex" style={{ opacity: 1 }}>
<div className="shrink-0">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" className="h-6 w-6">
<path fillRule="evenodd" d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12zm13.36-1.814a.75.75 0 10-1.22-.872l-3.236 4.53L9.53 12.22a.75.75 0 00-1.06 1.06l2.25 2.25a.75.75 0 001.14-.094l3.75-5.25z" clipRule="evenodd" />
</svg>
</div>
<div className="ml-3 mr-12 truncate" id="errorMsg">
{error}
</div>
</div>
)}
<div className="mt-8 mb-2 max-w-80 lg:w-full w-[80%] lg:mx-0 mx-auto">
<div className="mb-1 flex flex-col gap-3">
<h6 className=" font-semibold text-primary-900">Roll No.</h6>
<input
onChange={(event) => {
setName(event.target.value);
}}
value={name}
placeholder={123}
type="number"
className="w-full h-11 bg-transparent text-primary-700 font-normal outline outline-0 focus:outline-0 disabled:bg-blue-gray-50 disabled:border-0 disabled:cursor-not-allowed transition-all placeholder-shown:border border focus:border-2 border-t-transparent focus:border-t-transparent text-sm px-3 py-3 rounded-md border-blue-gray-200 focus:border-gray-900 !border-t-blue-gray-200 focus:!border-t-gray-900"
/>
<h6 className=" font-semibold text-primary-900">Client</h6>
<input
onChange={(event) => {
setClient(event.target.value);
}}
value={client}
placeholder="Sahi"
className="w-full h-11 bg-transparent text-primary-700 font-normal outline outline-0 focus:outline-0 disabled:bg-blue-gray-50 disabled:border-0 disabled:cursor-not-allowed transition-all placeholder-shown:border border focus:border-2 border-t-transparent focus:border-t-transparent text-sm px-3 py-3 rounded-md border-blue-gray-200 focus:border-gray-900 !border-t-blue-gray-200 focus:!border-t-gray-900"
/>
<h6 className=" font-semibold text-primary-900">Stock</h6>
<input
onChange={(event) => {
setStock(event.target.value);
}}
value={stock}
placeholder={10}
type="number"
className="w-full h-11 bg-transparent text-primary-700 font-normal outline outline-0 focus:outline-0 disabled:bg-blue-gray-50 disabled:border-0 disabled:cursor-not-allowed transition-all placeholder-shown:border border focus:border-2 border-t-transparent focus:border-t-transparent text-sm px-3 py-3 rounded-md border-blue-gray-200 focus:border-gray-900 !border-t-blue-gray-200 focus:!border-t-gray-900"
/>
<h6 className=" font-semibold text-primary-900">Description</h6>
<input
onChange={(event) => {
setDescription(event.target.value);
}}
value={description}
placeholder="Plain white Fabric"
className="w-full h-11 bg-transparent text-primary-700 font-normal outline outline-0 focus:outline-0 disabled:bg-blue-gray-50 disabled:border-0 disabled:cursor-not-allowed transition-all placeholder-shown:border border focus:border-2 border-t-transparent focus:border-t-transparent text-sm px-3 py-3 rounded-md border-blue-gray-200 focus:border-gray-900 !border-t-blue-gray-200 focus:!border-t-gray-900"
/>
</div>
<button
id="submitBtn"
onClick={() => {
console.log(name, client, stock);
if (!name || !client || !stock || !description || !image) {
setError("Please fill all the fields");
setTimeout(() => {
setError(null);
}, 3000);
return;
}
let sampleImage = document.getElementById("sampleImage");
let imageFile = sampleImage.files[0];
let data = new FormData();
data.append("name", name);
data.append("client", client);
data.append("stock", stock);
data.append("description", description);
data.append("image", imageFile);
fetch("/api/rollEntry", {
method: "POST",
body: data,
})
.then((response) => response.json())
.then((data) => {
if (data.status === "success") {
setClient("");
setName("");
setStock("");
setDescription("");
setImage(null);
setSuccess(data.msg);
setTimeout(() => {
setSuccess(null);
}, 3000);
fetch("/api/rolls")
.then((res) => res.json())
.then((data) => {
setRolls(data["data"]);
})
.catch((err) => console.error(err));
} else {
setError(data.msg);
setTimeout(() => {
setError(null);
}, 3000);
voiceAssist.say(data.msg);
}
})
.catch((error) => console.error("Error:", error));
}}
className="align-middle select-none font-sans font-bold text-center uppercase transition-all disabled:opacity-50 disabled:shadow-none disabled:pointer-events-none text-xs py-3 px-6 rounded-lg bg-gradient-to-tr from-primary-950 to-primary-800 text-white shadow-md shadow-gray-900/10 hover:shadow-lg hover:shadow-gray-900/20 focus:opacity-[0.85] focus:shadow-none active:opacity-[0.85] active:shadow-none block w-full mt-6"
type="button"
>
Submit
</button>
</div>
</div>
</div>
);
}
export default AddRollPage;
|