1+ // Route: /edit-link/:id
12import React , { useRef , useEffect , useState } from "react" ;
23import axios from "axios" ;
34import "./style.css" ;
@@ -66,9 +67,19 @@ function classNames(...classes: string[]) {
6667 return classes . filter ( Boolean ) . join ( " " ) ;
6768}
6869
70+ const getStoredUser = ( ) : User | null => {
71+ try {
72+ const stored = localStorage . getItem ( "user" ) ;
73+ return stored ? JSON . parse ( stored ) : null ;
74+ } catch ( error ) {
75+ console . error ( "Error reading stored user:" , error ) ;
76+ return null ;
77+ }
78+ } ;
79+
6980const EditLink : React . FC = ( ) => {
70- const [ isLoggedIn , setIsLoggedIn ] = useState ( false ) ;
71- const [ user , setUser ] = useState < User | null > ( null ) ;
81+ const [ isLoggedIn , setIsLoggedIn ] = useState < boolean > ( ( ) => ! ! getStoredUser ( ) ) ;
82+ const [ user , setUser ] = useState < User | null > ( ( ) => getStoredUser ( ) ) ;
7283 const [ loading , setLoading ] = useState ( true ) ;
7384 const { id } = useParams < { id : string } > ( ) ;
7485 const [ link , setLink ] = useState < Partial < Link > | null > ( null ) ;
@@ -102,24 +113,6 @@ const EditLink: React.FC = () => {
102113 const checkDomain = removePrefix ( customLink ) ;
103114 const invalidDomainLink = checkDomain . includes ( "/" ) ;
104115
105- const fetchUserData = async ( ) => {
106- setLoading ( true ) ;
107- try {
108- const storedUserData = localStorage . getItem ( "user" ) ;
109-
110- if ( storedUserData ) {
111- const user = JSON . parse ( storedUserData ) ;
112- setUser ( user ) ;
113- } else {
114- console . error ( "Failed to fetch user data or no user data found" ) ;
115- }
116- } catch ( error ) {
117- console . error ( "Error fetching user data:" , error ) ;
118- } finally {
119- setLoading ( false ) ;
120- }
121- } ;
122-
123116 useEffect ( ( ) => {
124117 if ( message ) {
125118 const timer = setTimeout ( ( ) => {
@@ -226,6 +219,7 @@ const uniqueId = id
226219 setInitialLink ( foundLink ?. customLink || "" ) ;
227220 setColor ( foundLink ?. qrcodeColor || "#000000" ) ;
228221 setLogo ( foundLink ?. qrcodeLogo || null ) ;
222+ setLoading ( false ) ;
229223 }
230224 } ;
231225
@@ -438,16 +432,6 @@ const uniqueId = id
438432 }
439433 } ;
440434
441- useEffect ( ( ) => {
442- const user = localStorage . getItem ( "user" ) ;
443- if ( user ) {
444- setIsLoggedIn ( true ) ;
445- fetchUserData ( ) ;
446- } else {
447- setLoading ( false ) ;
448- }
449- } , [ ] ) ;
450-
451435 const handleSignOut = ( ) => {
452436 localStorage . removeItem ( "user" ) ;
453437 setIsLoggedIn ( false ) ;
@@ -1015,4 +999,4 @@ const Text = styled.span`
1015999 color: #888;
10161000` ;
10171001
1018- export default EditLink ;
1002+ export default EditLink ;
0 commit comments