Skip to content

Commit 79ae7fb

Browse files
author
Lasim
committed
refactor(frontend): replace error handling with toast notifications
1 parent e69699a commit 79ae7fb

File tree

1 file changed

+34
-47
lines changed

1 file changed

+34
-47
lines changed

services/frontend/src/views/Register.vue

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@ import { toTypedSchema } from '@vee-validate/zod'
44
import * as z from 'zod'
55
import { ref, onMounted } from 'vue'
66
import { useRouter } from 'vue-router'
7-
import { Mail, Lock, User, AlertTriangle } from 'lucide-vue-next'
7+
import { Mail, Lock, User } from 'lucide-vue-next'
88
import { useI18n } from 'vue-i18n'
99
import { getEnv } from '@/utils/env'
1010
import { Github } from 'lucide-vue-next'
11+
import { toast } from 'vue-sonner'
1112
1213
import {
1314
Card,
1415
CardContent,
1516
CardFooter,
1617
} from '@/components/ui/card'
1718
18-
import {
19-
Alert,
20-
AlertDescription,
21-
AlertTitle,
22-
} from '@/components/ui/alert'
23-
2419
import { Button } from '@/components/ui/button'
2520
import {
2621
FormControl,
@@ -33,10 +28,8 @@ import { Input } from '@/components/ui/input'
3328
3429
const router = useRouter()
3530
const isLoading = ref(false)
36-
const errorMessage = ref('')
37-
const successMessage = ref('')
3831
const githubOAuthEnabled = ref(false)
39-
const { t } = useI18n() // Initialize i18n composable
32+
const { t } = useI18n()
4033
4134
// Get API URL from environment
4235
const apiUrl = getEnv('VITE_DEPLOYSTACK_BACKEND_URL')
@@ -96,10 +89,9 @@ const form = useForm({
9689
},
9790
})
9891
99-
// Clear error when user starts typing
92+
// Clear any existing toasts when user starts typing
10093
const clearError = () => {
101-
errorMessage.value = ''
102-
successMessage.value = ''
94+
// Toasts auto-dismiss, no manual clearing needed
10395
}
10496
10597
interface RegisterError {
@@ -114,30 +106,34 @@ interface PotentialError {
114106
status?: unknown;
115107
}
116108
117-
// Handle different types of errors
109+
// Handle different types of errors with Sonner toasts
118110
const handleError = (error: RegisterError) => {
111+
let message = ''
112+
119113
if (error.name === 'TypeError' && error.message && error.message.includes('fetch')) {
120114
// Network error - backend is down
121-
errorMessage.value = 'Unable to connect to server. Please try again later.'
115+
message = t('register.errors.networkError', 'Unable to connect to server. Please try again later.')
122116
} else if (error.status && error.status === 409) {
123117
// Conflict - username or email already exists
124-
errorMessage.value = error.message || 'Username or email already exists.'
118+
message = error.message || t('register.errors.conflict', 'Username or email already exists.')
125119
} else if (error.status && error.status >= 500) {
126120
// Server error
127-
errorMessage.value = 'Server error occurred. Please try again later.'
121+
message = t('register.errors.serverError', 'Server error occurred. Please try again later.')
128122
} else if (error.name === 'AbortError') {
129123
// Request timeout
130-
errorMessage.value = 'Request timed out. Please try again.'
124+
message = t('register.errors.timeout', 'Request timed out. Please try again.')
131125
} else {
132126
// Unknown error
133-
errorMessage.value = error.message || 'An unexpected error occurred during registration.'
127+
message = error.message || t('register.errors.unknownError', 'An unexpected error occurred during registration.')
134128
}
129+
130+
toast.error(t('register.errors.title', 'Registration Error'), {
131+
description: message
132+
})
135133
}
136134
137135
const onSubmit = form.handleSubmit(async (values) => {
138136
isLoading.value = true
139-
errorMessage.value = ''
140-
successMessage.value = ''
141137
142138
try {
143139
// Create AbortController for timeout handling
@@ -170,11 +166,14 @@ const onSubmit = form.handleSubmit(async (values) => {
170166
}
171167
172168
await response.json()
173-
successMessage.value = 'Account created successfully! Redirecting to login...'
174-
175-
setTimeout(() => {
176-
router.push('/login')
177-
}, 2000)
169+
170+
// Show success toast and redirect to login
171+
toast.success(t('register.success.title', 'Account created successfully!'), {
172+
description: t('register.success.description', 'You can now sign in with your credentials.')
173+
})
174+
175+
// Immediate redirect to login
176+
router.push('/login')
178177
179178
} catch (e) {
180179
console.error('Registration error:', e);
@@ -235,20 +234,26 @@ const handleGitHubSignup = async () => {
235234
try {
236235
const isEnabled = await checkGitHubOAuthStatus()
237236
if (!isEnabled) {
238-
errorMessage.value = t('login.oauth.github.unavailable')
237+
toast.error(t('register.errors.title', 'Registration Error'), {
238+
description: t('login.oauth.github.unavailable', 'GitHub OAuth is not available')
239+
})
239240
return
240241
}
241242
242243
if (!apiUrl) {
243-
errorMessage.value = t('login.errors.networkError')
244+
toast.error(t('register.errors.title', 'Registration Error'), {
245+
description: t('login.errors.networkError', 'Network error occurred')
246+
})
244247
return
245248
}
246249
247250
// Redirect to GitHub OAuth endpoint (same as login)
248251
window.location.href = `${apiUrl}/api/auth/github/login`
249252
} catch (error) {
250253
console.error('GitHub OAuth error:', error)
251-
errorMessage.value = t('login.errors.githubOAuthError')
254+
toast.error(t('register.errors.title', 'Registration Error'), {
255+
description: t('login.errors.githubOAuthError', 'GitHub OAuth error occurred')
256+
})
252257
}
253258
}
254259
@@ -272,24 +277,6 @@ onMounted(async () => {
272277
</div>
273278

274279
<div class="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
275-
<!-- Error Alert -->
276-
<Alert v-if="errorMessage" variant="destructive" class="mb-6">
277-
<AlertTriangle class="h-4 w-4" />
278-
<AlertTitle>Registration Error</AlertTitle>
279-
<AlertDescription>
280-
{{ errorMessage }}
281-
</AlertDescription>
282-
</Alert>
283-
284-
<!-- Success Alert -->
285-
<Alert v-if="successMessage" class="mb-6">
286-
<AlertTriangle class="h-4 w-4" />
287-
<AlertTitle>Success</AlertTitle>
288-
<AlertDescription>
289-
{{ successMessage }}
290-
</AlertDescription>
291-
</Alert>
292-
293280
<Card>
294281
<CardContent class="pt-6">
295282
<form @submit="onSubmit" class="space-y-4">

0 commit comments

Comments
 (0)