import React, { useEffect, useMemo, useState } from "react"; import { USER_DATA } from "./data"; var idb; const insertDataInIndexedDb = () => { if(typeof window !== 'undefined'){ idb = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB; } //check for support if (!idb) { console.log("This browser doesn't support IndexedDB"); return; } const request = idb.open("test-db", 1); request.onerror = function (event) { console.error("An error occurred with IndexedDB"); console.error(event); }; request.onupgradeneeded = function (event) { console.log(event); const db = request.result; if (!db.objectStoreNames.contains("userData")) { const objectStore = db.createObjectStore("userData", { keyPath: "id" }); objectStore.createIndex("age", "age", { unique: false, }); } }; request.onsuccess = function () { console.log("Database opened successfully"); const db = request.result; var tx = db.transaction("userData", "readwrite"); var userData = tx.objectStore("userData"); USER_DATA.forEach((item) => userData.add(item)); return tx.complete; }; }; const App = () => { const [allUsers, setAllUsers] = useState([]); const [addUser, setAddUser] = useState(false); const [editUser, setEditUser] = useState(false); const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const [email, setEmail] = useState(""); const [selectedUser, setSelectedUser] = useState({}); const [age, setAge] = useState(''); const [minAge, setMinAge] = useState(''); const [maxAge, setMaxAge] = useState(''); useEffect(() => { insertDataInIndexedDb(); getAllData(); // getAgeWiseData(); }, []); const getAgeWiseData = () => { try { const dbPromise = idb.open("test-db", 1); const filteredRecords = [] // const keyRangeValue = IDBKeyRange.lowerBound(12); // const keyRangeValue = IDBKeyRange.upperBound(12); const keyRangeValue = IDBKeyRange.bound(parseInt(minAge), parseInt(maxAge), false, false); // If true then these values are not included other wise these will be included. dbPromise.onsuccess = function () { const db = dbPromise.result; if (db.objectStoreNames.contains('userData')) { const transaction = db.transaction('userData', "readonly"); const objectStore = transaction.objectStore('userData'); const dataIdIndex = objectStore.index("age"); dataIdIndex.openCursor(keyRangeValue).onsuccess = function (event) { const cursor = event.target.result; if (cursor) { if (cursor.value) { if (parseInt(cursor.value.age) > 0) { console.log(cursor.value); filteredRecords.push(cursor.value) } } cursor.continue(); } }; transaction.oncomplete = function (event) { setAllUsers(filteredRecords) db.close(); }; } }; } catch (error) { console.log(error); } } const getAllData = () => { const dbPromise = idb.open("test-db", 1); dbPromise.onsuccess = () => { const db = dbPromise.result; var tx = db.transaction("userData", "readonly"); var userData = tx.objectStore("userData"); const users = userData.getAll(); users.onsuccess = (query) => { setAllUsers(query.srcElement.result); }; tx.oncomplete = function () { db.close(); }; }; }; const handleSubmit = (event) => { const dbPromise = idb.open("test-db", 1); console.log(addUser, editUser); if (firstName && lastName && email) { dbPromise.onsuccess = () => { const db = dbPromise.result; var tx = db.transaction("userData", "readwrite"); var userData = tx.objectStore("userData"); console.log(addUser, editUser); console.log(addUser, editUser); if (addUser) { const users = userData.put({ id: allUsers?.length + 1, firstName, lastName, email, age, }); console.log("add"); users.onsuccess = (query) => { tx.oncomplete = function () { db.close(); }; alert("User added!"); setFirstName(""); setLastName(""); setEmail(""); setAge('') setAddUser(false); getAllData(); event.preventDefault(); }; } else { const users = userData.put({ id: selectedUser?.id, firstName, lastName, email, age, }); console.log("edit"); users.onsuccess = (query) => { tx.oncomplete = function () { db.close(); }; alert("User updated!"); setFirstName(""); setLastName(""); setEmail(""); setAge('') setEditUser(false); getAllData(); setSelectedUser({}); event.preventDefault(); }; } }; } else { alert("Please enter all details"); } }; const deleteSelected = (user) => { const dbPromise = idb.open("test-db", 1); dbPromise.onsuccess = function () { const db = dbPromise.result; var tx = db.transaction("userData", "readwrite"); var userData = tx.objectStore("userData"); const deleteUser = userData.delete(user.id); deleteUser.onsuccess = (query) => { tx.oncomplete = function () { db.close(); }; alert("User deleted!"); getAllData(); }; }; }; return (
setMinAge(e.target.value)} className="form-control" style={{width: '200px'}} placeholder="Enter Min Age"/> setMaxAge(e.target.value)} className="form-control ml-2" style={{width: '200px'}} placeholder="Enter Max Age"/>
{allUsers?.map((user) => { return ( ); })}
First Name Last Name Age Email Actions
{user?.firstName} {user?.lastName} {user?.age} {user?.email} {" "}
{editUser || addUser ? (

{editUser ? "Update User" : "Add User"}

setFirstName(e.target.value)} value={firstName} />
setLastName(e.target.value)} value={lastName} />
setAge(e.target.value)} value={age} />
setEmail(e.target.value)} value={email} />
) : null}
); }; export default App;