mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-19 03:04:05 +00:00
Refactor install wizard and mail configuration
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useNotificationStore } from '@/scripts/stores/notification.store'
|
||||
import { getErrorTranslationKey, handleApiError } from '@/scripts/utils/error-handling'
|
||||
|
||||
interface InstallationResponse {
|
||||
success?: boolean | string
|
||||
error?: string | boolean
|
||||
error_message?: string
|
||||
message?: string
|
||||
}
|
||||
|
||||
export function useInstallationFeedback() {
|
||||
const { t } = useI18n()
|
||||
const notificationStore = useNotificationStore()
|
||||
|
||||
function isSuccessfulResponse(response: InstallationResponse | null | undefined): boolean {
|
||||
return Boolean(response?.success) && !response?.error && !response?.error_message
|
||||
}
|
||||
|
||||
function showResponseError(response: InstallationResponse | null | undefined): void {
|
||||
const candidate =
|
||||
typeof response?.error_message === 'string' && response.error_message.trim()
|
||||
? response.error_message
|
||||
: typeof response?.error === 'string' && response.error.trim()
|
||||
? response.error
|
||||
: typeof response?.message === 'string' && response.message.trim()
|
||||
? response.message
|
||||
: ''
|
||||
|
||||
notificationStore.showNotification({
|
||||
type: 'error',
|
||||
message: resolveMessage(candidate),
|
||||
})
|
||||
}
|
||||
|
||||
function showRequestError(error: unknown): void {
|
||||
if (error instanceof Error && !('response' in error) && error.message.trim()) {
|
||||
notificationStore.showNotification({
|
||||
type: 'error',
|
||||
message: resolveMessage(error.message),
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const normalizedError = handleApiError(error)
|
||||
|
||||
notificationStore.showNotification({
|
||||
type: 'error',
|
||||
message: resolveMessage(normalizedError.message),
|
||||
})
|
||||
}
|
||||
|
||||
function resolveMessage(message: string): string {
|
||||
const normalizedMessage = message.trim()
|
||||
|
||||
if (!normalizedMessage) {
|
||||
return 'validation.something_went_wrong'
|
||||
}
|
||||
|
||||
const wizardErrorKey = `wizard.errors.${normalizedMessage}`
|
||||
|
||||
if (t(wizardErrorKey) !== wizardErrorKey) {
|
||||
return wizardErrorKey
|
||||
}
|
||||
|
||||
return getErrorTranslationKey(normalizedMessage) ?? normalizedMessage
|
||||
}
|
||||
|
||||
return {
|
||||
isSuccessfulResponse,
|
||||
showRequestError,
|
||||
showResponseError,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user