From 94f2cea2b9847b6d02b4c02d965fd1201ab80967 Mon Sep 17 00:00:00 2001 From: Abhijat Sinha Date: Mon, 2 Jun 2025 20:37:52 +0530 Subject: [PATCH] SavePassword Function Fix There Was an error When we try to save Password Previous Password will get Delete. We will use if statement to check and then only we will delete the password --- .../passop-mongo/src/components/Manager.jsx | 280 +++++++++--------- 1 file changed, 133 insertions(+), 147 deletions(-) diff --git a/Video 130/passop-mongo/src/components/Manager.jsx b/Video 130/passop-mongo/src/components/Manager.jsx index e60c064a..28cb9e41 100644 --- a/Video 130/passop-mongo/src/components/Manager.jsx +++ b/Video 130/passop-mongo/src/components/Manager.jsx @@ -1,19 +1,18 @@ -import React from 'react' -import { useRef, useState, useEffect } from 'react'; -import { ToastContainer, toast } from 'react-toastify'; +import { useRef, useState, useEffect } from 'react' +import { ToastContainer, toast } from 'react-toastify' +import 'react-toastify/dist/ReactToastify.css' import { v4 as uuidv4 } from 'uuid'; -import 'react-toastify/dist/ReactToastify.css'; const Manager = () => { const ref = useRef() const passwordRef = useRef() - const [form, setform] = useState({ site: "", username: "", password: "" }) + const [form, setform] = useState({ site: '', username: '', password: '' }) const [passwordArray, setPasswordArray] = useState([]) const getPasswords = async () => { - let req = await fetch("http://localhost:3000/") + let req = await fetch('http://localhost:3000/') let passwords = await req.json() - setPasswordArray(passwords) + setPasswordArray(passwords); } @@ -22,46 +21,23 @@ const Manager = () => { }, []) - const copyText = (text) => { - toast('Copied to clipboard!', { - position: "top-right", - autoClose: 5000, - hideProgressBar: false, - closeOnClick: true, - pauseOnHover: true, - draggable: true, - progress: undefined, - theme: "dark", - }); - navigator.clipboard.writeText(text) + const handleChange = (e) => { + setform({ ...form, [e.target.name]: e.target.value }) } const showPassword = () => { - passwordRef.current.type = "text" - console.log(ref.current.src) - if (ref.current.src.includes("icons/eyecross.png")) { - ref.current.src = "icons/eye.png" - passwordRef.current.type = "password" - } - else { - passwordRef.current.type = "text" - ref.current.src = "icons/eyecross.png" + if (ref.current.src.includes("/crosseye.svg")) { + ref.current.src = "/eye.svg" + passwordRef.current.type = 'password' + } else { + ref.current.src = "/crosseye.svg" + passwordRef.current.type = 'text' } - } const savePassword = async () => { - if (form.site.length > 3 && form.username.length > 3 && form.password.length > 3) { - - // If any such id exists in the db, delete it - await fetch("http://localhost:3000/", { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id: form.id }) }) - - setPasswordArray([...passwordArray, { ...form, id: uuidv4() }]) - await fetch("http://localhost:3000/", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...form, id: uuidv4() }) }) - - // Otherwise clear the form and show toast - setform({ site: "", username: "", password: "" }) - toast('Password saved!', { + if (form.site === '' || form.username === '' || form.password === '') { + toast.error('Please fill all fields!', { position: "top-right", autoClose: 5000, hideProgressBar: false, @@ -69,144 +45,155 @@ const Manager = () => { pauseOnHover: true, draggable: true, progress: undefined, - theme: "dark", + theme: "light", }); + return; } - else { - toast('Error: Password not saved!'); + setPasswordArray([...passwordArray, { ...form, id: uuidv4() }]) + //while editing delete the old password + if (form.id) { // Changed from form.ide to form.id + await fetch("http://localhost:3000/", { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ id: form.id }) + }) } - + // localStorage.setItem('passwords', JSON.stringify([...passwordArray, { ...form, id: uuidv4() }])) + await fetch("http://localhost:3000/", { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ ...form, id: uuidv4() }) + }) + setform({ site: '', username: '', password: '' }) + toast.success('Password saved successfully!', { + position: "top-right", + autoClose: 5000, + hideProgressBar: false, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "light", + }); } - const deletePassword = async (id) => { - console.log("Deleting password with id ", id) - let c = confirm("Do you really want to delete this password?") - if (c) { - setPasswordArray(passwordArray.filter(item => item.id !== id)) - - await fetch("http://localhost:3000/", { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id }) }) - - toast('Password Deleted!', { - position: "top-right", - autoClose: 5000, - hideProgressBar: false, - closeOnClick: true, - draggable: true, - progress: undefined, - theme: "dark", - }); - } - + let confirmDelete = window.confirm("Are you sure you want to delete this password?"); + if (!confirmDelete) return; + setPasswordArray(passwordArray.filter((item) => item.id !== id)) + // localStorage.setItem('passwords', JSON.stringify(passwordArray.filter((item) => item.id !== id))) + let res = await fetch("http://localhost:3000/", { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ id: id }) + }) + + toast.error('Password deleted successfully!', { + position: "top-right", + autoClose: 5000, + hideProgressBar: false, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "light", + }); } - const editPassword = (id) => { - setform({ ...passwordArray.filter(i => i.id === id)[0], id: id }) - setPasswordArray(passwordArray.filter(item => item.id !== id)) - } - - const handleChange = (e) => { - setform({ ...form, [e.target.name]: e.target.value }) + setform({ + ...passwordArray.find((item) => item.id === id), + id: id // keep the id same for editing + }) + setPasswordArray(passwordArray.filter((item) => item.id !== id)) } - return ( <> - -
-
+ + +
+

- < - - PassOP/> + < + Pass + Vault + />

-

Your own Password Manager

- -
- -
- -
- - - eye - +

Your own password manager

+ +
+ +
+ +
+ +
-
- +
+ +

Your Passwords

- {passwordArray.length === 0 &&
No passwords to show
} - {passwordArray.length != 0 && - + {passwordArray.length === 0 &&
No passwords Found
} + + {passwordArray.length !== 0 &&
+ - - - + + + - + {passwordArray.map((item, index) => { return - - - - + + + @@ -216,7 +203,6 @@ const Manager = () => {
SiteUsernamePasswordSite NameUser NameUser Password Actions
-
- {item.site} -
{ copyText(item.site) }}> - - -
-
-
-
- {item.username} -
{ copyText(item.username) }}> - - -
-
-
-
- {"*".repeat(item.password.length)} -
{ copyText(item.password) }}> - - -
-
-
- { editPassword(item.id) }}> + {item.site}{item.username}{item.password} + { + editPassword(item.id) + }}> + stroke="bold"> - { deletePassword(item.id) }}> + { + deletePassword(item.id) + }}> + src="https://cdn.lordicon.com/oqeixref.json" + trigger="hover">
}
- ) }