Skip to content

Commit 0e2e620

Browse files
committed
refactor: core functionality files converted to TS
1 parent 4feadf3 commit 0e2e620

File tree

14 files changed

+325
-95
lines changed

14 files changed

+325
-95
lines changed

package-lock.json

Lines changed: 123 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
"dependencies": {
1818
"@primeuix/themes": "^1.0.0",
1919
"@tailwindcss/vite": "^4.1.13",
20-
"@vueuse/core": "^12.2.0",
21-
"axios": "^1.7.2",
20+
"@vueuse/core": "^12.8.2",
21+
"@vueuse/integrations": "^13.9.0",
22+
"axios": "^1.12.2",
2223
"lucide-vue-next": "^0.487.0",
2324
"nprogress": "^0.2.0",
2425
"pinia": "^2.1.7",
@@ -34,6 +35,7 @@
3435
"@primevue/auto-import-resolver": "^4.2.5",
3536
"@tsconfig/node22": "^22.0.2",
3637
"@types/node": "^24.5.2",
38+
"@types/nprogress": "^0.2.3",
3739
"@vitejs/plugin-vue": "^5.0.5",
3840
"@vue/eslint-config-typescript": "^14.6.0",
3941
"@vue/tsconfig": "^0.8.1",
@@ -46,4 +48,4 @@
4648
"vite-plugin-vue-devtools": "^8.0.2",
4749
"vue-tsc": "^3.0.8"
4850
}
49-
}
51+
}

src/composables/useAppLayout.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import { ref, computed, onMounted, onUnmounted, watchEffect } from 'vue';
22
import { useRoute } from 'vue-router';
33
import { LayoutGrid, House, Info, Settings, LogOut, ExternalLink, FileSearch, FolderGit2 } from 'lucide-vue-next';
44
import { useAuthStore } from '@/stores/auth';
5+
import { MenuItem } from '@/types';
56

67
export function useAppLayout() {
78
const authStore = useAuthStore();
89
const route = useRoute();
9-
const currentRoute = computed(() => route.name);
1010

11+
const currentRoute = computed(() => route.name);
1112
const userName = computed(() => authStore?.user?.name ?? 'User');
1213

1314
// Menu items
14-
const menuItems = computed(() => [
15+
const menuItems = computed<MenuItem[]>(() => [
1516
{
1617
label: 'Home',
1718
lucideIcon: House,
@@ -57,7 +58,7 @@ export function useAppLayout() {
5758
]);
5859

5960
// User menu and logout functionality.
60-
const userMenuItems = [
61+
const userMenuItems: MenuItem[] = [
6162
{
6263
label: 'Settings',
6364
route: { name: 'settings.profile.edit' },

src/composables/useAxiosErrorHandling.ts

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,83 @@
11
import { ref } from 'vue';
22
import { useRouter } from 'vue-router';
33
import { useToast } from 'primevue/usetoast';
4+
import type { AxiosError } from 'axios';
5+
6+
type ValidationErrors = Record<string, string[]>;
7+
8+
interface ValidationErrorResponse {
9+
errors: ValidationErrors;
10+
}
11+
12+
interface ErrorHandlingOptions {
13+
onError?: (error: AxiosError) => void;
14+
}
415

516
export function useAxiosErrorHandling() {
617
const router = useRouter();
718
const toast = useToast();
8-
const validationErrors = ref({});
19+
const validationErrors = ref<ValidationErrors>({});
920

10-
const clearErrors = (...fields) => {
21+
function clearErrors(...fields: string[]): void {
1122
if (fields.length > 0) {
12-
fields.forEach((field) => {
23+
fields.forEach(field => {
1324
if (field in validationErrors.value) {
1425
delete validationErrors.value[field];
1526
}
1627
});
1728
} else {
1829
validationErrors.value = {};
1930
}
20-
};
31+
}
2132

22-
const showErrorToast = (summary, detail) => {
33+
function showErrorToast(summary: string, detail: string, severity: 'error' | 'warn' = 'error'): void {
2334
toast.add({
24-
severity: 'error',
35+
severity,
2536
summary,
2637
detail,
27-
life: 3000,
38+
life: 5000,
2839
});
29-
};
40+
}
3041

31-
const handleAxiosError = (error, options = {}) => {
42+
function handleAxiosError(
43+
error: AxiosError,
44+
options: ErrorHandlingOptions = {}
45+
): void {
3246
if (error.response) {
33-
const status = error.response.status;
34-
const responseData = error.response.data;
35-
36-
if (status === 419) {
37-
router.push({ name: 'login' });
38-
showErrorToast('Session Expired', 'Please log in again.');
39-
} else if (status === 422 && responseData?.errors) {
40-
validationErrors.value = responseData.errors;
41-
//showErrorToast('Validation Error', 'Validation failed.');
42-
} else if (status >= 500) {
43-
showErrorToast('Server Error', 'A critical error occurred.');
47+
const { status, data: responseData } = error.response;
48+
49+
if (status === 401) {
50+
console.log('User is unauthorized');
51+
//showErrorToast('401 - Unauthorized', 'Please reload the page and login in.', 'warn');
52+
} else if (status === 403) {
53+
showErrorToast('403 - Forbidden', 'Sorry, you are unauthorized to access this resource/action.', 'warn');
54+
} else if (status === 404) {
55+
showErrorToast('404 - Not Found', 'Sorry, the resource you are looking for could not be found.', 'warn');
56+
} else if (status === 419) {
57+
router.push({ name: 'login' }).then(() => {
58+
showErrorToast('419 - Session Expired ', 'Please reload the page.', 'warn');
59+
});
60+
} else if (status === 422) {
61+
const errorData = responseData as ValidationErrorResponse;
62+
if (errorData.errors) {
63+
validationErrors.value = errorData.errors;
64+
}
65+
} else if (status === 500) {
66+
showErrorToast('500 - Server Error', 'Whoops, something went wrong on our end. Please try again.');
67+
} else if (status === 503) {
68+
showErrorToast(
69+
'503 - Service Unavailable',
70+
'Sorry, we are doing some maintenance. Please check back soon.'
71+
);
72+
} else {
73+
showErrorToast('Error', 'Something went wrong...');
4474
}
4575
} else if (error.request) {
46-
showErrorToast('Network Error', 'Technical difficulties, please try again later.');
47-
//console.error('Network Error:', error.message);
76+
showErrorToast('Error', 'Technical difficulties, please contact I.T. support.');
4877
}
4978

50-
if (options.onError) options.onError(error);
51-
};
79+
options.onError?.(error);
80+
}
5281

5382
return {
5483
validationErrors,

0 commit comments

Comments
 (0)