|
| 1 | +import React, { useState } from 'react'; |
| 2 | + |
| 3 | +export default function EngineerCheck() { |
| 4 | + const [engineerId, setEngineerId] = useState(''); |
| 5 | + const [warehouseId, setWarehouseId] = useState(''); |
| 6 | + const [location, setLocation] = useState(''); |
| 7 | + const [rawMaterial, setRawMaterial] = useState(''); |
| 8 | + const [quantity, setQuantity] = useState<number | ''>(''); |
| 9 | + const [quality, setQuality] = useState('Good'); |
| 10 | + const [vendorName, setVendorName] = useState(''); |
| 11 | + const [vendorContact, setVendorContact] = useState(''); |
| 12 | + const [temperature, setTemperature] = useState<number | ''>(''); |
| 13 | + const [humidity, setHumidity] = useState<number | ''>(''); |
| 14 | + const [weight, setWeight] = useState<number | ''>(''); |
| 15 | + const [notes, setNotes] = useState(''); |
| 16 | + const [photos, setPhotos] = useState<FileList | null>(null); |
| 17 | + const [videos, setVideos] = useState<FileList | null>(null); |
| 18 | + const [status, setStatus] = useState<string | null>(null); |
| 19 | + const [blockchainHash, setBlockchainHash] = useState<string | null>(null); |
| 20 | + const inputStyle = { |
| 21 | + color: '#000000e8', |
| 22 | + backgroundColor: '#d13c3c' |
| 23 | +}; |
| 24 | + |
| 25 | + async function filesToDataURLs(files: FileList | null) { |
| 26 | + if (!files) return []; |
| 27 | + const arr = Array.from(files); |
| 28 | + const results = await Promise.all( |
| 29 | + arr.map( |
| 30 | + (f) => |
| 31 | + new Promise((res) => { |
| 32 | + const reader = new FileReader(); |
| 33 | + reader.onload = () => res({ name: f.name, type: f.type, data: reader.result }); |
| 34 | + reader.onerror = () => res(null); |
| 35 | + reader.readAsDataURL(f); |
| 36 | + }) |
| 37 | + ) |
| 38 | + ); |
| 39 | + return results.filter(Boolean); |
| 40 | + } |
| 41 | +async function generateHash(data: any) { |
| 42 | + const encoder = new TextEncoder(); |
| 43 | + const encoded = encoder.encode(JSON.stringify(data)); |
| 44 | + const hashBuffer = await crypto.subtle.digest("SHA-256", encoded); |
| 45 | + const hashArray = Array.from(new Uint8Array(hashBuffer)); |
| 46 | + return hashArray.map(b => b.toString(16).padStart(2, "0")).join(""); |
| 47 | +} |
| 48 | + |
| 49 | + async function handleSubmit(e: React.FormEvent) { |
| 50 | + e.preventDefault(); |
| 51 | + setStatus('Uploading...'); |
| 52 | + |
| 53 | + const photoData = await filesToDataURLs(photos); |
| 54 | + const videoData = await filesToDataURLs(videos); |
| 55 | + |
| 56 | + const payload = { |
| 57 | + engineerId, |
| 58 | + warehouseId, |
| 59 | + location, |
| 60 | + sensorData: { |
| 61 | + temperature: temperature === '' ? null : Number(temperature), |
| 62 | + humidity: humidity === '' ? null : Number(humidity), |
| 63 | + weight: weight === '' ? null : Number(weight) |
| 64 | + }, |
| 65 | + result: { |
| 66 | + rawMaterial, |
| 67 | + quantity: quantity === '' ? null : Number(quantity), |
| 68 | + quality, |
| 69 | + vendor: { name: vendorName, contact: vendorContact }, |
| 70 | + notes, |
| 71 | + media: { photos: photoData, videos: videoData } |
| 72 | + } |
| 73 | + }; |
| 74 | +const hash = await generateHash(payload); |
| 75 | +setBlockchainHash(hash); |
| 76 | + |
| 77 | + try { |
| 78 | + const res = await fetch('/api/inspection/create', { |
| 79 | + method: 'POST', |
| 80 | + headers: { 'Content-Type': 'application/json' }, |
| 81 | + body: JSON.stringify(payload) |
| 82 | + }); |
| 83 | + |
| 84 | + const json = await res.json(); |
| 85 | + if (!res.ok) throw new Error(json.error || 'Upload failed'); |
| 86 | + setStatus('Submitted ✓'); |
| 87 | + } catch (err: any) { |
| 88 | + setStatus('Error: ' + (err.message || err)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return ( |
| 93 | + <div style={{ padding: 24, maxWidth: 900, margin: '0 auto' }}> |
| 94 | + <h1>Engineer Inspection — Submit Check</h1> |
| 95 | + <form onSubmit={handleSubmit}> |
| 96 | + <label>Engineer ID<br /> |
| 97 | + <input value={engineerId} onChange={(e) => setEngineerId(e.target.value)} required /> |
| 98 | + </label> |
| 99 | + |
| 100 | + <br /> |
| 101 | + <label>Warehouse ID<br /> |
| 102 | + <input value={warehouseId} onChange={(e) => setWarehouseId(e.target.value)} required /> |
| 103 | + </label> |
| 104 | + |
| 105 | + <br /> |
| 106 | + <label>Location<br /> |
| 107 | + <input value={location} onChange={(e) => setLocation(e.target.value)} required /> |
| 108 | + </label> |
| 109 | + |
| 110 | + <br /> |
| 111 | + <label>Raw material type<br /> |
| 112 | + <input value={rawMaterial} onChange={(e) => setRawMaterial(e.target.value)} /> |
| 113 | + </label> |
| 114 | + |
| 115 | + <br /> |
| 116 | + <label>Quantity<br /> |
| 117 | + <input type="number" value={quantity as any} onChange={(e) => setQuantity(e.target.value === '' ? '' : Number(e.target.value))} /> |
| 118 | + </label> |
| 119 | + |
| 120 | + <br /> |
| 121 | + <label>Quality<br /> |
| 122 | + <select value={quality} onChange={(e) => setQuality(e.target.value)}> |
| 123 | + <option>Good</option> |
| 124 | + <option>Acceptable</option> |
| 125 | + <option>Poor</option> |
| 126 | + </select> |
| 127 | + </label> |
| 128 | + |
| 129 | + <br /> |
| 130 | + <fieldset style={{ marginTop: 8 }}> |
| 131 | + <legend>Vendor</legend> |
| 132 | + <label>Name<br /> |
| 133 | + <input value={vendorName} onChange={(e) => setVendorName(e.target.value)} /> |
| 134 | + </label> |
| 135 | + <br /> |
| 136 | + <label>Contact<br /> |
| 137 | + <input value={vendorContact} onChange={(e) => setVendorContact(e.target.value)} /> |
| 138 | + </label> |
| 139 | + </fieldset> |
| 140 | + |
| 141 | + <br /> |
| 142 | + <fieldset> |
| 143 | + <legend>Sensor data (truck / storage)</legend> |
| 144 | + <label>Temperature (°C)<br /> |
| 145 | + <input type="number" value={temperature as any} onChange={(e) => setTemperature(e.target.value === '' ? '' : Number(e.target.value))} /> |
| 146 | + </label> |
| 147 | + <br /> |
| 148 | + <label>Humidity (%)<br /> |
| 149 | + <input type="number" value={humidity as any} onChange={(e) => setHumidity(e.target.value === '' ? '' : Number(e.target.value))} /> |
| 150 | + </label> |
| 151 | + <br /> |
| 152 | + <label>Weight (kg)<br /> |
| 153 | + <input type="number" value={weight as any} onChange={(e) => setWeight(e.target.value === '' ? '' : Number(e.target.value))} /> |
| 154 | + </label> |
| 155 | + </fieldset> |
| 156 | + |
| 157 | + <br /> |
| 158 | + <label>Notes<br /> |
| 159 | + <textarea value={notes} onChange={(e) => setNotes(e.target.value)} rows={4} /> |
| 160 | + </label> |
| 161 | + |
| 162 | + <br /> |
| 163 | + <label>Photos (multiple)<br /> |
| 164 | + <input type="file" accept="image/*" multiple onChange={(e) => setPhotos(e.target.files)} /> |
| 165 | + </label> |
| 166 | + |
| 167 | + <br /> |
| 168 | + <label>Videos (optional)<br /> |
| 169 | + <input type="file" accept="video/*" multiple onChange={(e) => setVideos(e.target.files)} /> |
| 170 | + </label> |
| 171 | + |
| 172 | + <br /> |
| 173 | + <button type="submit">Submit Inspection</button> |
| 174 | + </form> |
| 175 | + |
| 176 | + {status && <p style={{ marginTop: 12 }}>{status}</p>} |
| 177 | + |
| 178 | + |
| 179 | +{blockchainHash && ( |
| 180 | + <div |
| 181 | + style={{ |
| 182 | + marginTop: 16, |
| 183 | + padding: 12, |
| 184 | + borderRadius: 8, |
| 185 | + border: '1px solid #22c55e', |
| 186 | + background: 'rgba(23, 211, 92, 0.1)' |
| 187 | + }} |
| 188 | + > |
| 189 | + <p style={{ fontWeight: 600, color: '#22c55e' }}> |
| 190 | + ✅ Inspection Stored on Blockchain (Demo) |
| 191 | + </p> |
| 192 | + <p style={{ fontSize: 12, wordBreak: 'break-all' }}> |
| 193 | + Hash: {blockchainHash} |
| 194 | + </p> |
| 195 | + <p style={{ fontSize: 12 }}> |
| 196 | + Network: Polygon (Demo) |
| 197 | + </p> |
| 198 | + </div> |
| 199 | +)} |
| 200 | + |
| 201 | +</div> |
| 202 | +); |
| 203 | +} |
0 commit comments