chore: error messages

pull/3813/head
Arik Chakma 2 years ago
parent 9d8889a492
commit 9ca74a5750
  1. 26
      src/components/Login/EmailLoginForm.tsx
  2. 50
      src/components/Login/EmailSignupForm.tsx
  3. 6
      src/components/Login/GithubLogin.astro
  4. 4
      src/components/Login/GoogleLogin.astro
  5. 1
      src/components/Profile/ForgotPasswordForm.tsx
  6. 1
      src/components/Profile/ResetPasswordForm.tsx

@ -7,6 +7,7 @@ const EmailLoginForm: FunctionComponent<{}> = () => {
const [email, setEmail] = useState<string>(''); const [email, setEmail] = useState<string>('');
const [password, setPassword] = useState<string>(''); const [password, setPassword] = useState<string>('');
const [isLoading, setIsLoading] = useState<boolean>(false);
const [message, setMessage] = useState<{ const [message, setMessage] = useState<{
type: 'success' | 'error' | 'verification' | 'warning'; type: 'success' | 'error' | 'verification' | 'warning';
message: string; message: string;
@ -14,18 +15,21 @@ const EmailLoginForm: FunctionComponent<{}> = () => {
const handleFormSubmit = async (e: Event) => { const handleFormSubmit = async (e: Event) => {
e.preventDefault(); e.preventDefault();
setIsLoading(true);
setMessage(null);
// Check if the verification-email-sent-at is less than 5 seconds ago // Check if the verification-email-sent-at is less than 5 seconds ago
const verificationEmailSentAt = localStorage.getItem( const verificationEmailSentAt = localStorage.getItem(
'verification-email-sent-at' 'verification-email-sent-at'
); );
console.log(verificationEmailSentAt);
if (verificationEmailSentAt) { if (verificationEmailSentAt) {
const now = new Date(); const now = new Date();
if (Number(verificationEmailSentAt) > now.getTime()) { if (Number(verificationEmailSentAt) > now.getTime()) {
setIsLoading(false);
setEmail('');
setPassword('');
return setMessage({ return setMessage({
type: 'warning', type: 'error',
message: 'Please wait before sending another verification email.', message: 'Please wait before sending another verification email.',
}); });
} }
@ -45,6 +49,7 @@ const EmailLoginForm: FunctionComponent<{}> = () => {
// If the response isn't ok, we'll throw an error // If the response isn't ok, we'll throw an error
if (json.type === 'user_not_verified') { if (json.type === 'user_not_verified') {
setIsLoading(false);
return setMessage({ return setMessage({
type: 'verification', type: 'verification',
message: message:
@ -62,9 +67,12 @@ const EmailLoginForm: FunctionComponent<{}> = () => {
message: json.message, message: json.message,
}); });
} }
setIsLoading(false);
}; };
const handleResendVerificationEmail = async () => { const handleResendVerificationEmail = async () => {
setIsLoading(true);
// Check if the verification-email-sent-at is less than 5 seconds ago // Check if the verification-email-sent-at is less than 5 seconds ago
const verificationEmailSentAt = localStorage.getItem( const verificationEmailSentAt = localStorage.getItem(
'verification-email-sent-at' 'verification-email-sent-at'
@ -73,8 +81,9 @@ const EmailLoginForm: FunctionComponent<{}> = () => {
if (verificationEmailSentAt) { if (verificationEmailSentAt) {
const now = new Date(); const now = new Date();
if (Number(verificationEmailSentAt) > now.getTime()) { if (Number(verificationEmailSentAt) > now.getTime()) {
setIsLoading(false);
return setMessage({ return setMessage({
type: 'warning', type: 'error',
message: 'Please wait before sending another verification email.', message: 'Please wait before sending another verification email.',
}); });
} }
@ -102,6 +111,8 @@ const EmailLoginForm: FunctionComponent<{}> = () => {
}); });
} }
// If the response is ok, we'll set the token in a cookie // If the response is ok, we'll set the token in a cookie
setEmail('');
setPassword('');
setMessage({ setMessage({
type: 'success', type: 'success',
message: 'Verification instructions have been sent to your email.', message: 'Verification instructions have been sent to your email.',
@ -116,6 +127,8 @@ const EmailLoginForm: FunctionComponent<{}> = () => {
'verification-email-sent-at', 'verification-email-sent-at',
now.getTime().toString() now.getTime().toString()
); );
setIsLoading(false);
}; };
return ( return (
@ -152,12 +165,12 @@ const EmailLoginForm: FunctionComponent<{}> = () => {
{message && ( {message && (
<> <>
{message.type === 'verification' ? ( {message.type === 'verification' ? (
<div className="mt-2 text-sm text-slate-600"> <div className="mt-2 rounded-lg bg-yellow-100 p-2 text-sm text-yellow-800">
Your account is not verified. Please click the verification link Your account is not verified. Please click the verification link
in your email. Or{' '} in your email. Or{' '}
<button <button
type="button" type="button"
className="text-blue-600 hover:text-blue-500" className="font-semibold text-yellow-900 hover:text-yellow-800"
onClick={handleResendVerificationEmail} onClick={handleResendVerificationEmail}
> >
resend verification email. resend verification email.
@ -178,6 +191,7 @@ const EmailLoginForm: FunctionComponent<{}> = () => {
)} )}
<button <button
disabled={isLoading || !email || !password}
type="submit" type="submit"
className="mt-3 inline-flex h-10 w-full items-center justify-center rounded-lg border border-slate-300 bg-black p-2 text-sm font-medium text-white outline-none transition duration-150 ease-in-out focus:ring-2 focus:ring-black focus:ring-offset-1 disabled:opacity-60" className="mt-3 inline-flex h-10 w-full items-center justify-center rounded-lg border border-slate-300 bg-black p-2 text-sm font-medium text-white outline-none transition duration-150 ease-in-out focus:ring-2 focus:ring-black focus:ring-offset-1 disabled:opacity-60"
> >

@ -7,10 +7,11 @@ const EmailSignupForm: FunctionComponent<{}> = () => {
const [email, setEmail] = useState<string>(''); const [email, setEmail] = useState<string>('');
const [password, setPassword] = useState<string>(''); const [password, setPassword] = useState<string>('');
const [name, setName] = useState<string>(''); const [name, setName] = useState<string>('');
const [message, setMessage] = useState<string | null>(null);
const [error, setError] = useState<{ const [isLoading, setIsLoading] = useState<boolean>(false);
const [message, setMessage] = useState<{
type: 'success' | 'error' | 'verification' | 'warning';
message: string; message: string;
status: number;
} | null>(null); } | null>(null);
return ( return (
@ -18,6 +19,7 @@ const EmailSignupForm: FunctionComponent<{}> = () => {
className="w-full" className="w-full"
onSubmit={(e) => { onSubmit={(e) => {
e.preventDefault(); e.preventDefault();
setIsLoading(true);
fetch('http://localhost:8080/v1-register', { fetch('http://localhost:8080/v1-register', {
method: 'POST', method: 'POST',
headers: { headers: {
@ -32,23 +34,26 @@ const EmailSignupForm: FunctionComponent<{}> = () => {
.then(async (res) => { .then(async (res) => {
const json = await res.json(); const json = await res.json();
if (res.status === 200) { if (res.status === 200) {
setMessage( setName('');
'We have sent you an email with the verification link. Please follow the instructions to login.' setEmail('');
); setPassword('');
setError(null); setMessage({
console.log(json); type: 'success',
message:
'We have sent you an email with the verification link. Please follow the instructions to login.',
});
} else { } else {
setMessage(null);
const error = new Error(json.message) as any; const error = new Error(json.message) as any;
error.status = res.status; error.status = res.status;
throw error; throw error;
} }
setIsLoading(false);
}) })
.catch((err) => { .catch((err) => {
setMessage(null); setIsLoading(false);
setError({ setMessage({
type: 'error',
message: err.message, message: err.message,
status: 400,
}); });
}); });
}} }}
@ -96,13 +101,28 @@ const EmailSignupForm: FunctionComponent<{}> = () => {
onInput={(e) => setPassword(String((e.target as any).value))} onInput={(e) => setPassword(String((e.target as any).value))}
/> />
{error && ( {message && (
<div className="mt-2 text-sm text-red-500">{error.message}</div> <div
className={`mt-2 rounded-lg p-2 ${
message.type === 'error'
? 'bg-red-100 text-red-700'
: message.type === 'success'
? 'bg-green-100 text-green-700'
: 'bg-blue-100 text-blue-700'
}`}
>
{message.message}
</div>
)} )}
{message && <div className="mt-2 text-sm text-green-500">{message}</div>}
<button <button
type="submit" type="submit"
disabled={
email.length === 0 ||
password.length === 0 ||
name.length === 0 ||
isLoading
}
className="mt-3 inline-flex h-10 w-full items-center justify-center rounded-lg border border-slate-300 bg-black p-2 text-sm font-medium text-white outline-none transition duration-150 ease-in-out focus:ring-2 focus:ring-black focus:ring-offset-1 disabled:opacity-60" className="mt-3 inline-flex h-10 w-full items-center justify-center rounded-lg border border-slate-300 bg-black p-2 text-sm font-medium text-white outline-none transition duration-150 ease-in-out focus:ring-2 focus:ring-black focus:ring-offset-1 disabled:opacity-60"
> >
Continue Continue

@ -27,11 +27,17 @@ import Spinner from '../Spinner.astro';
function addSpinner() { function addSpinner() {
githubLoginText?.classList.add('hidden'); githubLoginText?.classList.add('hidden');
githubLoginSpinner?.classList.remove('hidden'); githubLoginSpinner?.classList.remove('hidden');
// Disable button
githubLoginButton?.setAttribute('disabled', 'true');
} }
function hideSpinner() { function hideSpinner() {
githubLoginText?.classList.remove('hidden'); githubLoginText?.classList.remove('hidden');
githubLoginSpinner?.classList.add('hidden'); githubLoginSpinner?.classList.add('hidden');
// Enable button
githubLoginButton?.removeAttribute('disabled');
} }
function showError(err: Error) { function showError(err: Error) {

@ -29,11 +29,15 @@ import Spinner from '../Spinner.astro';
function addSpinner() { function addSpinner() {
googleLoginText?.classList.add('hidden'); googleLoginText?.classList.add('hidden');
googleLoginSpinner?.classList.remove('hidden'); googleLoginSpinner?.classList.remove('hidden');
// Disable button
googleLoginButton?.setAttribute('disabled', 'true');
} }
function hideSpinner() { function hideSpinner() {
googleLoginText?.classList.remove('hidden'); googleLoginText?.classList.remove('hidden');
googleLoginSpinner?.classList.add('hidden'); googleLoginSpinner?.classList.add('hidden');
// Enable button
googleLoginButton?.setAttribute('disabled', 'true');
} }
function showError(err: Error) { function showError(err: Error) {

@ -102,6 +102,7 @@ export default function ForgotPasswordForm() {
<button <button
type="submit" type="submit"
disabled={isLoading}
className="mt-2 inline-flex h-10 w-full items-center justify-center rounded-lg border border-slate-300 bg-black p-2 px-4 text-sm font-medium text-white outline-none transition duration-150 ease-in-out focus:ring-2 focus:ring-black focus:ring-offset-1 disabled:cursor-not-allowed disabled:opacity-60" className="mt-2 inline-flex h-10 w-full items-center justify-center rounded-lg border border-slate-300 bg-black p-2 px-4 text-sm font-medium text-white outline-none transition duration-150 ease-in-out focus:ring-2 focus:ring-black focus:ring-offset-1 disabled:cursor-not-allowed disabled:opacity-60"
> >
{isLoading ? ( {isLoading ? (

@ -123,6 +123,7 @@ export default function ResetPasswordForm() {
)} )}
<button <button
disabled={isLoading}
type="submit" type="submit"
className="mt-5 inline-flex h-10 w-full items-center justify-center rounded-lg border border-slate-300 bg-black p-2 px-4 text-sm font-medium text-white outline-none transition duration-150 ease-in-out focus:ring-2 focus:ring-black focus:ring-offset-1 disabled:cursor-not-allowed disabled:opacity-60" className="mt-5 inline-flex h-10 w-full items-center justify-center rounded-lg border border-slate-300 bg-black p-2 px-4 text-sm font-medium text-white outline-none transition duration-150 ease-in-out focus:ring-2 focus:ring-black focus:ring-offset-1 disabled:cursor-not-allowed disabled:opacity-60"
> >

Loading…
Cancel
Save