dashboard.jsx

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.7 kB
0
Indexable
Never
import './Dashboard.css'
import React from 'react';
import { useNavigate } from 'react-router-dom';
import { Link } from 'react-router-dom';
import imageCompression from 'browser-image-compression';

const Dashboard = () => {
  const navi = useNavigate();

  const handleImageChange = async (e) => {
    const files = e.target.files;
    const newImages = Array.from(files);

    try {
      const compressedImages = await Promise.all(
        newImages.map(async (image) => {
          if (image.size > 2 * 1024 * 1024) {
            console.log("Yes the greater image is compressed")
            // Compress image if size exceeds 2 MB
            const compressedImage = await imageCompression(image, {
              maxSizeMB: 2,
              useWebWorker: true,
            });
            console.log("size after compressing ",compressedImage.size)
            const x=compressedImage.size;
            if(x<2*1024*1024){
              console.log("True");
            }
            // Convert compressed image to base64 string
            return new Promise((resolve, reject) => {
              const reader = new FileReader();
              reader.readAsDataURL(compressedImage);
              reader.onloadend = () => {
                resolve(reader.result);
              };
              reader.onerror = reject;
            });
          } else {
            // Convert image to base64 string directly
            return new Promise((resolve) => {
              const reader = new FileReader();
              reader.readAsDataURL(image);
              reader.onloadend = () => {
                resolve(reader.result);
              };
            });
          }
        })
      );
      
      // Store base64 strings in local storage
      localStorage.setItem('uploadedImages', JSON.stringify(compressedImages));
    } catch (err) {
      console.log(err);
      // Handle error, maybe show a notification to the user
    }
  };

  return (
    <>
      <header class="dash">
      
        <nav>
          <div className="nav-left">
            
          </div>
          <div className="nav-right">
            <p>Welcome !!!!! {localStorage.getItem('name')}</p>
            <Link to="/Dashboard">Dashboard</Link>
            <br />
            <input type="file" onChange={handleImageChange} multiple />
            <br />
            <br />
            <Link to="/FetchImage">Images</Link>
          </div>
        </nav>
      </header>
      <h1>Weather App</h1>
      <br />
      <br />

      <button  onClick={() => navi('/Weatherdata')}>Weather Search</button>
    </>
  );
};

export default Dashboard;
Leave a Comment