refactor: update profile errors

pull/3813/head
Arik Chakma 2 years ago
parent 61fe3a16bc
commit 423df1a225
  1. 41
      src/components/Setting/UpdateProfileForm.tsx

@ -8,10 +8,13 @@ export default function UpdateProfileForm() {
const [github, setGithub] = useState(''); const [github, setGithub] = useState('');
const [linkedin, setLinkedin] = useState(''); const [linkedin, setLinkedin] = useState('');
const [website, setWebsite] = useState(''); const [website, setWebsite] = useState('');
const [error, setError] = useState('');
const [successMessage, setSuccessMessage] = useState('');
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [message, setMessage] = useState<{
type: 'error' | 'success' | 'info';
message: string;
}>();
const handleSubmit = (e: Event) => { const handleSubmit = (e: Event) => {
e.preventDefault(); e.preventDefault();
setIsLoading(true); setIsLoading(true);
@ -50,11 +53,17 @@ export default function UpdateProfileForm() {
setGithub(github); setGithub(github);
setLinkedin(linkedin); setLinkedin(linkedin);
setWebsite(website); setWebsite(website);
setSuccessMessage('Profile updated successfully'); setMessage({
type: 'success',
message: 'Profile updated successfully',
});
}) })
.catch((err) => { .catch((err) => {
setIsLoading(false); setIsLoading(false);
setError(err.message); setMessage({
type: 'error',
message: err.message || 'Something went wrong',
});
}); });
}; };
@ -94,7 +103,10 @@ export default function UpdateProfileForm() {
throw new Error(json.message); throw new Error(json.message);
} }
} catch (error: any) { } catch (error: any) {
setError(error?.message || 'Something went wrong'); setMessage({
type: 'error',
message: error?.message || 'Something went wrong',
});
} }
setIsLoading(false); setIsLoading(false);
} }
@ -188,14 +200,17 @@ export default function UpdateProfileForm() {
/> />
</div> </div>
{error && ( {message && (
<div className="text-sm font-medium text-red-500"> <div
<span className="text-red-500">{error}</span> className={`mt-2 rounded-lg p-2 ${
</div> message.type === 'error'
)} ? 'bg-red-100 text-red-700'
{successMessage && ( : message.type === 'success'
<div className="text-sm font-medium text-green-500"> ? 'bg-green-100 text-green-700'
<span className="text-green-500">{successMessage}</span> : 'bg-blue-100 text-blue-700'
}`}
>
{message.message}
</div> </div>
)} )}

Loading…
Cancel
Save