Advertisement
LagExpress

Client code

Feb 5th, 2023
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.91 KB | Source Code | 0 0
  1. // Client code || app/upload/page.tsx
  2. "use client"
  3. import React, { useState } from 'react'
  4.  
  5. const UploadPage: React.FC = () => {
  6.     const [file, setFile] = useState<File | null>(null)
  7.     const [company, setCompany] = useState("")
  8.     const [companies, setCompanies] = useState<string[]>(["Empresa 1", "Empresa 2", "Empresa 3"])
  9.     const [success, setSuccess] = useState(false)
  10.     const [error, setError] = useState<string | null>(null)
  11.     const [otherCompany, setOtherCompany] = useState('')
  12.  
  13.     const handleFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
  14.         setFile(e.target.files![0])
  15.     }
  16.  
  17.     const handleCompanyChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
  18.         setCompany(e.target.value)
  19.     }
  20.  
  21.     const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
  22.         e.preventDefault()
  23.         try {
  24.             // send the file and company to the API
  25.             const data = new FormData()
  26.             data.append("file", file!)
  27.             if (company === "other") {
  28.                 data.append("company", otherCompany)
  29.             } else {
  30.                 data.append("company", company)
  31.             }
  32.             const response = await fetch('/api/upload2', {
  33.                 method: 'POST',
  34.                 body: data,
  35.             })
  36.             if (!response.ok) throw new Error(await response.text())
  37.             setSuccess(true)
  38.         } catch (err) {
  39.             setError(err.message)
  40.         }
  41.     }
  42.  
  43.     return (
  44.         <div>
  45.             {success && <div>Upload Successful!</div>}
  46.             {error && <div>Error: {error}</div>}
  47.             <form onSubmit={handleSubmit}>
  48.                 <div>
  49.                     <input
  50.                         type="file"
  51.                         accept=".xml, .zip"
  52.                         onChange={handleFileUpload}
  53.                     />
  54.                 </div>
  55.                 <div>
  56.                     <select value={company} onChange={handleCompanyChange}>
  57.                         <option value="" disabled>
  58.                             Selecciona una empresa
  59.                         </option>
  60.                         {companies.map(c => (
  61.                             <option key={c} value={c}>
  62.                                 {c}
  63.                             </option>
  64.                         ))}
  65.                         <option value="other">Otra</option>
  66.                     </select>
  67.                     {company === "other" && (
  68.                         <input
  69.                             type="text"
  70.                             placeholder="Escribir empresa manualmente"
  71.                             value={otherCompany}
  72.                             onChange={e => setOtherCompany(e.target.value)}
  73.                         />
  74.                     )}
  75.                 </div>
  76.                 <button type="submit">Upload</button>
  77.             </form>
  78.         </div>
  79.     )
  80. }
  81.  
  82. export default UploadPage
Tags: Next.js
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement