Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper error translate #175

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions frontend/src/assets/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@
"title": "Calendar"
}
},
"composables": {
"helpers": {
"errors": {
"notFound": "Not Found",
"notFoundDetail": "Resource not found.",
"unauthorized": "Unauthorized",
"unauthorizedDetail": "You are not authorized to access this resource.",
"server": "Server Error",
"serverDetail": "An error occurred on the server.",
"network": "Network Error",
"networkDetail": "Unable to connect to the server.",
"request": "Request Error",
"requestDetail": "An error occurred while making the request."
}
}
},
"components": {
"buttons": {
"academic_year": "Academic year {0}"
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/assets/lang/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@
"title": "Kalender"
}
},
"composables": {
"helpers": {
"errors": {
"notFound": "Niet Gevonden",
"notFoundDetail": "Resource niet gevonden.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Resource/Bron/

"unauthorized": "Onbevoegd",
"unauthorizedDetail": "Je bent niet bevoegd om deze resource te bereiken.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Resource/Bron/

"server": "Server Fout",
"serverDetail": "Er vond een fout plaats op de server.",
"network": "Netwerk Fout",
"networkDetail": "Kan de server niet bereiken.",
"request": "Verzoek Fout",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er is een fout verzoek ontvangen.

"requestDetail": "Een fout vond plaats tijdens het maken van het verzoek."
}
}
},
"components": {
"buttons": {
"academic_year": "Academiejaar {0}"
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/composables/services/admins.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useAdmin() {
const admins = ref<Admin[]|null>(null);
const admin = ref<Admin|null>(null);
const toast = useToast();

async function getAdminByID(id: number) {
async function getAdminByID(id: number, t: ComposerTranslation) {
const endpoint = endpoints.admins.retrieve.replace('{id}', id.toString());
get<Admin>(endpoint, admin, Admin.fromJSON, toast);
get<Admin>(endpoint, admin, Admin.fromJSON, toast, t);
console.log(admin)
}

async function getAdmins() {
async function getAdmins(t: ComposerTranslation) {
const endpoint = endpoints.admins.index;
getList<Admin>(endpoint, admins, Admin.fromJSON, toast);
getList<Admin>(endpoint, admins, Admin.fromJSON, toast, t);
console.log(admins.value ? admins.value.map((admin, index) => `Admin ${index + 1}: ${JSON.stringify(admin)}`) : 'Admins is null');
}

Expand Down
9 changes: 5 additions & 4 deletions frontend/src/composables/services/assistant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useAssistant() {
const assistants = ref<Assistant[]|null>(null);
const assistant = ref<Assistant|null>(null);
const toast = useToast();

async function getAssistantByID(id: number) {
async function getAssistantByID(id: number, t: ComposerTranslation) {
const endpoint = endpoints.assistants.retrieve.replace('{id}', id.toString());
get<Assistant>(endpoint, assistant, Assistant.fromJSON, toast);
get<Assistant>(endpoint, assistant, Assistant.fromJSON, toast, t);
console.log(assistant)
}

async function getAssistants() {
async function getAssistants(t: ComposerTranslation) {
const endpoint = endpoints.assistants.index;
getList<Assistant>(endpoint, assistants, Assistant.fromJSON, toast);
getList<Assistant>(endpoint, assistants, Assistant.fromJSON, toast, t);
console.log(assistants.value ? assistants.value.map((assistant, index) => `Assistant ${index + 1}: ${JSON.stringify(assistant)}`) : 'assistants is null');
}

Expand Down
13 changes: 7 additions & 6 deletions frontend/src/composables/services/courses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useCourses() {
const courses = ref<Course[]|null>(null);
const course = ref<Course|null>(null);
const toast = useToast();

async function getCourseByID(id: number) {
async function getCourseByID(id: number, t: ComposerTranslation) {
const endpoint = endpoints.courses.retrieve.replace('{id}', id.toString());
get<Course>(endpoint, course, Course.fromJSON, toast);
get<Course>(endpoint, course, Course.fromJSON, toast, t);
console.log(course.value);
}

async function getCourses() {
async function getCourses(t: ComposerTranslation) {
const endpoint = endpoints.courses.index;
getList<Course>(endpoint, courses, Course.fromJSON, toast);
getList<Course>(endpoint, courses, Course.fromJSON, toast, t);
console.log(courses.value ? courses.value.map((course, index) => `Course ${index + 1}: ${JSON.stringify(course)}`) : 'Courses is null');
}

async function getCoursesByStudent(student_id: number) {
async function getCoursesByStudent(student_id: number, t: ComposerTranslation) {
const endpoint = endpoints.courses.byStudent.replace('{student_id}', student_id.toString());
getList<Course>(endpoint, courses, Course.fromJSON, toast);
getList<Course>(endpoint, courses, Course.fromJSON, toast, t);
console.log(courses.value ? courses.value.map((course, index) => `Course ${index + 1}: ${JSON.stringify(course)}`) : 'Courses is null');
}

Expand Down
9 changes: 5 additions & 4 deletions frontend/src/composables/services/faculties.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useFaculty() {
const faculties = ref<Faculty[]|null>(null);
const faculty = ref<Faculty|null>(null);
const toast = useToast();

async function getFacultyByID(name: string) {
async function getFacultyByID(name: string, t: ComposerTranslation) {
const endpoint = endpoints.faculties.retrieve.replace('{name}', name);
get<Faculty>(endpoint, faculty, Faculty.fromJSON, toast);
get<Faculty>(endpoint, faculty, Faculty.fromJSON, toast, t);
console.log(faculty)
}

async function getFacultys() {
async function getFacultys(t: ComposerTranslation) {
const endpoint = endpoints.faculties.index;
getList<Faculty>(endpoint, faculties, Faculty.fromJSON, toast);
getList<Faculty>(endpoint, faculties, Faculty.fromJSON, toast, t);
console.log(faculties.value ? faculties.value.map((faculty, index) => `Faculty ${index + 1}: ${JSON.stringify(faculty)}`) : 'Facultys is null');
}

Expand Down
9 changes: 5 additions & 4 deletions frontend/src/composables/services/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useGroup() {
const groups = ref<Group[]|null>(null);
const group = ref<Group|null>(null);
const toast = useToast();

async function getGroupByID(id: number) {
async function getGroupByID(id: number, t: ComposerTranslation) {
const endpoint = endpoints.groups.retrieve.replace('{id}', id.toString());
get<Group>(endpoint, group, Group.fromJSON, toast);
get<Group>(endpoint, group, Group.fromJSON, toast, t);
console.log(group)
}

async function getGroupsByProject(project_id: number) {
async function getGroupsByProject(project_id: number, t: ComposerTranslation) {
const endpoint = endpoints.groups.byProject.replace('{project_id}', project_id.toString());
getList<Group>(endpoint, groups, Group.fromJSON, toast);
getList<Group>(endpoint, groups, Group.fromJSON, toast, t);
console.log(groups.value ? groups.value.map((group, index) => `Group ${index + 1}: ${JSON.stringify(group)}`) : 'Groups is null');
}

Expand Down
28 changes: 16 additions & 12 deletions frontend/src/composables/services/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import axios, { AxiosError, AxiosResponse } from 'axios';
// import { useI18n } from 'vue-i18n';
import {Ref} from 'vue';
import {ComposerTranslation} from "vue-i18n";
const lifeTime = 3000;

export async function get<T>(endpoint: string, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
// const { t } = useI18n();

export async function get<T>(endpoint: string, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {
await axios.get(endpoint).then((response: AxiosResponse) => {
ref.value = fromJson(response.data);
//toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
}).catch((error: AxiosError) => {
processError(error, toast);
processError(error, toast, t);
console.error(error); // Log the error for debugging
});
}

export async function getList<T>(endpoint: string, ref: Ref<T[]|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
export async function getList<T>(endpoint: string, ref: Ref<T[]|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {

await axios.get(endpoint).then(response => {
ref.value = response.data.map((data: T) => fromJson(data));
//toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
console.log(ref.value);
}
).catch((error: AxiosError) => {
processError(error, toast);
processError(error, toast, t);
console.error(error); // Log the error for debugging
});
}

export async function getListMerged<T>(endpoints: string[], ref: Ref<T[]|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
export async function getListMerged<T>(endpoints: string[], ref: Ref<T[]|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {

// Create an array to accumulate all response data
const allData: T[] = [];
Expand All @@ -38,29 +42,29 @@ export async function getListMerged<T>(endpoints: string[], ref: Ref<T[]|null>,
// toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
}
).catch((error: AxiosError) => {
processError(error, toast);
processError(error, toast, t);
console.error(error); // Log the error for debugging
});
}
ref.value = allData;
}

function processError(error: AxiosError, toast:any){
function processError(error: AxiosError, toast:any, t: ComposerTranslation){
if (error.response) {
console.log(error.response.status);
// The request was made and the server responded with a status code
if (error.response.status === 404) {
toast.add({ severity: 'error', summary: 'Not Found', detail: 'Resource not found.', life: lifeTime });
toast.add({ severity: 'error', summary: t('composables.helpers.errors.notFound'), detail: t('composables.helpers.errors.notFoundDetail'), life: lifeTime });
} else if (error.response.status === 401) {
toast.add({ severity: 'error', summary: 'Unauthorized', detail: 'You are not authorized to access this resource.', life: lifeTime });
toast.add({ severity: 'error', summary: t('composables.helpers.errors.unauthorized'), detail: t('composables.helpers.errors.unauthorizedDetail'), life: lifeTime });
} else {
toast.add({ severity: 'error', summary: 'Server Error', detail: 'An error occurred on the server.', life: lifeTime });
toast.add({ severity: 'error', summary: t('composables.helpers.errors.server'), detail: t('composables.helpers.errors.serverDetail'), life: lifeTime });
}
} else if (error.request) {
// The request was made but no response was received
toast.add({ severity: 'error', summary: 'Network Error', detail: 'Unable to connect to the server.', life: lifeTime });
toast.add({ severity: 'error', summary: t('composables.helpers.errors.network'), detail: t('composables.helpers.errors.networkDetail'), life: lifeTime });
} else {
// Something happened in setting up the request that triggered an error
toast.add({ severity: 'error', summary: 'Request Error', detail: 'An error occurred while making the request.', life: lifeTime });
toast.add({ severity: 'error', summary: t('composables.helpers.errors.request'), detail: t('composables.helpers.errors.requestDetail'), life: lifeTime });
}
}
15 changes: 8 additions & 7 deletions frontend/src/composables/services/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@ import axios from 'axios';
import { get, getList, getListMerged } from '@/composables/services/helpers.ts';
import { Course } from '@/types/Course';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";


export function useProject() {
const projects = ref<Project[]|null>(null);
const project = ref<Project|null>(null);
const toast = useToast();

async function getProjectByID(id: number) {
async function getProjectByID(id: number, t: ComposerTranslation) {
const endpoint = endpoints.projects.retrieve.replace('{id}', id.toString());
get<Project>(endpoint, project, Project.fromJSON, toast);
get<Project>(endpoint, project, Project.fromJSON, toast, t);
console.log(project)
}

async function getProjectsByCourse(course_id: number) {
async function getProjectsByCourse(course_id: number, t: ComposerTranslation) {
const endpoint = endpoints.projects.byCourse.replace('{course_id}', course_id.toString());
getList<Project>(endpoint, projects, Project.fromJSON, toast);
getList<Project>(endpoint, projects, Project.fromJSON, toast, t);
console.log(projects.value ? projects.value.map((project, index) => `Project ${index + 1}: ${JSON.stringify(project)}`) : 'Projects is null');
}

async function getProjectsByStudent(student_id: string) {
async function getProjectsByStudent(student_id: string, t: ComposerTranslation) {
const endpoint = endpoints.courses.byStudent.replace('{student_id}', student_id);
const courses = ref<Course[]|null>(null);
await getList<Course>(endpoint, courses, Course.fromJSON, toast);
await getList<Course>(endpoint, courses, Course.fromJSON, toast, t);

const endpList = [];
for (const course of courses.value?courses.value:[]){
endpList.push(endpoints.projects.byCourse.replace('{course_id}', course.id.toString()));
}
await getListMerged<Project>(endpList, projects, Project.fromJSON, toast);
await getListMerged<Project>(endpList, projects, Project.fromJSON, toast, t);
console.log(projects.value ? projects.value.map((project, index) => `Project ${index + 1}: ${JSON.stringify(project)}`) : 'Projects is null');
}

Expand Down
9 changes: 5 additions & 4 deletions frontend/src/composables/services/structure_check.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useStructure_check() {
const structure_checks = ref<Structure_check[]|null>(null);
const structure_check = ref<Structure_check|null>(null);
const toast = useToast();

async function getStructure_checkByID(id: number) {
async function getStructure_checkByID(id: number, t: ComposerTranslation) {
const endpoint = endpoints.structure_checks.retrieve.replace('{id}', id.toString());
get<Structure_check>(endpoint, structure_check, Structure_check.fromJSON, toast);
get<Structure_check>(endpoint, structure_check, Structure_check.fromJSON, toast, t);
console.log(structure_check)
}

async function getStructure_checkByProject(project_id: number) {
async function getStructure_checkByProject(project_id: number, t: ComposerTranslation) {
const endpoint = endpoints.structure_checks.byProject.replace('{project_id}', project_id.toString());
getList<Structure_check>(endpoint, structure_checks, Structure_check.fromJSON, toast);
getList<Structure_check>(endpoint, structure_checks, Structure_check.fromJSON, toast, t);
console.log(structure_checks.value ? structure_checks.value.map((structure_check, index) => `Structure_check ${index + 1}: ${JSON.stringify(structure_check)}`) : 'Structure_check is null');
}

Expand Down
9 changes: 5 additions & 4 deletions frontend/src/composables/services/students.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

export function useStudents() {
const students = ref<Student[]|null>(null);
const student = ref<Student|null>(null);
const toast = useToast();

async function getStudentByID(id: number) {
async function getStudentByID(id: number, t: ComposerTranslation) {
const endpoint = endpoints.students.retrieve.replace('{id}', id.toString());
get<Student>(endpoint, student, Student.fromJSON, toast);
get<Student>(endpoint, student, Student.fromJSON, toast, t);
console.log(student)
}

async function getStudents() {
async function getStudents(t: ComposerTranslation) {
const endpoint = endpoints.students.index;
getList<Student>(endpoint, students, Student.fromJSON, toast);
getList<Student>(endpoint, students, Student.fromJSON, toast, t);
console.log(students.value ? students.value.map((student, index) => `Student ${index + 1}: ${JSON.stringify(student)}`) : 'Students is null');
}

Expand Down
Loading
Loading