56
loading...
This website collects cookies to deliver better user experience
components/Profile.js
component.const [loading, setLoading] = useState(true);
const [updating, setUpdating] = useState(false);
getProfile
function, we can disable it once we are done with loading.async function getProfile() {
try {
// ... all the stuff
} catch (error) {
alert(error.message);
} finally {
setLoading(false);
}
}
updateProfile
we can start by setting the loading state and disabling it when we are done.async function updateProfile() {
try {
setUpdating(true);
// ... update call
} catch (error) {
alert(error.message);
} finally {
setUpdating(false);
}
}
return (
<div className='container mx-auto grid place-content-center min-h-screen'>
<p>Oh hi there {session.user.email}</p>
{loading ? (
<p>Loading your profile...</p>
) : (
// The form
)}
</div>
);
<button
onClick={(e) => {
e.preventDefault();
updateProfile();
}}
disabled={updating}
className='w-full mt-4 p-2 pl-5 pr-5 bg-blue-500 text-gray-100 text-lg rounded-lg focus:border-4 border-blue-300'
>
<span>{updating ? 'Updating profile' : 'Update profile'}</span>
</button>
components/Login.js
file and add a state like so:const [loading, setLoading] = useState(false);
<button
onClick={(e) => {
e.preventDefault();
handleLogin(email);
}}
disabled={loading}
className='w-full mt-4 p-2 pl-5 pr-5 bg-blue-500 text-gray-100 text-lg rounded-lg focus:border-4 border-blue-300'
>
<span>{loading ? 'Sending the link' : 'Send magic link'}</span>
</button>