Skip to content

Commit

Permalink
chore: abstract and give type to fill in correct type in toast message
Browse files Browse the repository at this point in the history
  • Loading branch information
bsilkyn committed May 18, 2024
1 parent 7b28362 commit 876ec0e
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 26 deletions.
17 changes: 16 additions & 1 deletion frontend/src/assets/lang/app/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,21 @@
"student": "Student",
"assistant": "Assistant",
"teacher": "Teacher"
},
"article": {
"admin": "The admin",
"assistant": "The assistant",
"course": "The course",
"docker": "The docker image",
"extraCheck": "The extra check",
"faculty": "The faculty",
"group": "The group",
"project": "The project",
"structureCheck": "The structure check",
"student": "The student",
"submission": "The submission",
"teacher": "The teacher",
"user": "The user"
}
},
"helpers": {
Expand All @@ -222,7 +237,7 @@
"success": "Success",
"error": "Error",
"unknown": "An unknown error has occurred.",
"create": "The object has successfully been created",
"create": "{type} has successfully been created",
"courses": {
"enrollment": {
"success": "You have been successfully enrolled for the course '{0}'.",
Expand Down
17 changes: 16 additions & 1 deletion frontend/src/assets/lang/app/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,21 @@
"student": "Student",
"assistant": "Assistent",
"teacher": "Professor"
},
"article": {
"admin": "De admin",
"assistant": "De assistent",
"course": "Het vak",
"docker": "De docker image",
"extraCheck": "De extra check",
"faculty": "De faculteit",
"group": "De groep",
"project": "Het project",
"structureCheck": "The structuurcheck",
"student": "De student",
"submission": "De indiening",
"teacher": "De professor",
"user": "De gebruiker"
}
},
"helpers": {
Expand All @@ -219,7 +234,7 @@
"success": "Succes",
"error": "Fout",
"unknown": "Er is een onbekende fout opgetreden.",
"create": "Het object werd succesvol aangemaakt",
"create": "{type} werd succesvol aangemaakt",
"courses": {
"enrollment": {
"success": "Je bent succesvol ingeschreven voor het vak '{0}'.",
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/composables/services/admin.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Ref, ref } from 'vue';
import { endpoints } from '@/config/endpoints.ts';
import { get, getList, create, deleteId } from '@/composables/services/helpers.ts';
import { get, getList, deleteId, createToast } from '@/composables/services/helpers.ts';
import { User } from '@/types/users/User.ts';

interface AdminState {
Expand Down Expand Up @@ -28,7 +28,8 @@ export function useAdmin(): AdminState {

async function createAdmin(user: User): Promise<void> {
const endpoint = endpoints.admins.index;
await create<User>(
await createToast<User>(
'admin',
endpoint,
{
id: user.id,
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/composables/services/assistant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { type User } from '@/types/users/User.ts';
import { Response } from '@/types/Response';
import { type Ref, ref } from 'vue';
import { endpoints } from '@/config/endpoints.ts';
import { get, getList, create, deleteId, deleteIdWithData, getPaginatedList } from '@/composables/services/helpers.ts';
import {
get,
getList,
create,
deleteId,
deleteIdWithData,
getPaginatedList,
createToast,
} from '@/composables/services/helpers.ts';
import { type PaginatorResponse } from '@/types/filter/Paginator.ts';
import { type Filter } from '@/types/filter/Filter.ts';

Expand Down Expand Up @@ -61,7 +69,8 @@ export function useAssistant(): AssistantState {

async function createAssistant(user: User): Promise<void> {
const endpoint = endpoints.assistants.index;
await create<Assistant>(
await createToast<Assistant>(
'assistant',
endpoint,
{
user: user.id,
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/composables/services/docker.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Response } from '@/types/Response.ts';
import { endpoints } from '@/config/endpoints.ts';
import { type Ref, ref } from 'vue';
import { type Filter } from '@/types/filter/Filter.ts';
import { create, getList, getPaginatedList, patch } from '@/composables/services/helpers.ts';
import { createToast, getList, getPaginatedList, patch } from '@/composables/services/helpers.ts';
import { type PaginatorResponse } from '@/types/filter/Paginator.ts';

interface DockerImagesState {
Expand Down Expand Up @@ -38,7 +38,8 @@ export function useDockerImages(): DockerImagesState {

async function createDockerImage(dockerData: DockerImage, file: File): Promise<void> {
const endpoint = endpoints.dockerImages.index;
await create<Response>(
await createToast<Response>(
'docker',
endpoint,
{
file,
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/composables/services/faculty.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Faculty } from '@/types/Faculty.ts';
import { type Ref, ref } from 'vue';
import { endpoints } from '@/config/endpoints.ts';
import { get, getList, create, deleteId } from '@/composables/services/helpers.ts';
import { get, getList, deleteId, createToast } from '@/composables/services/helpers.ts';

interface FacultyState {
faculties: Ref<Faculty[] | null>;
Expand All @@ -28,7 +28,13 @@ export function useFaculty(): FacultyState {

async function createFaculty(facultyData: Faculty): Promise<void> {
const endpoint = endpoints.faculties.index;
await create<Faculty>(endpoint, { id: facultyData.id, name: facultyData.name }, faculty, Faculty.fromJSON);
await createToast<Faculty>(
'faculty',
endpoint,
{ id: facultyData.id, name: facultyData.name },
faculty,
Faculty.fromJSON,
);
}

async function deleteFaculty(id: string): Promise<void> {
Expand Down
13 changes: 5 additions & 8 deletions frontend/src/composables/services/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,28 @@ export async function create<T>(
/**
* Create an item and display toast message when successful stating the creation has been successful.
*
* @param type type of the object that gets created
* @param endpoint
* @param data
* @param ref
* @param fromJson
* @param contentType
*/
export async function createToast<T>(
type: string,
endpoint: string,
data: any,
ref: Ref<T | null>,
fromJson: (data: any) => T,
contentType: string = 'application/json'
contentType: string = 'application/json',
): Promise<void> {
const { t } = i18n.global;
const { addSuccessMessage } = useMessagesStore();

try {
await create<T>(endpoint, data, ref, fromJson, contentType)
addSuccessMessage(t('toasts.messages.success'), t('toasts.messages.create'));
} catch (e) {
throw e
}
await create<T>(endpoint, data, ref, fromJson, contentType);
addSuccessMessage(t('toasts.messages.success'), t('toasts.messages.create', { type: t('types.article.' + type) }));
}


/**
* Patch an item given its ID.
*
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/composables/services/structure_check.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StructureCheck } from '@/types/StructureCheck.ts';
import { type Ref, ref } from 'vue';
import { endpoints } from '@/config/endpoints.ts';
import { get, getList, create, deleteId } from '@/composables/services/helpers.ts';
import { get, getList, deleteId, createToast } from '@/composables/services/helpers.ts';

interface StructureCheckState {
structureChecks: Ref<StructureCheck[] | null>;
Expand All @@ -28,7 +28,8 @@ export function useStructureCheck(): StructureCheckState {

async function createStructureCheck(structureCheckData: StructureCheck, projectId: string): Promise<void> {
const endpoint = endpoints.structureChecks.byProject.replace('{projectId}', projectId);
await create<StructureCheck>(
await createToast<StructureCheck>(
'structureCheck',
endpoint,
{
name: structureCheckData.name,
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/composables/services/student.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Student } from '@/types/users/Student.ts';
import { Response } from '@/types/Response';
import { type Ref, ref } from 'vue';
import { endpoints } from '@/config/endpoints.ts';
import { get, getList, create, deleteId, deleteIdWithData } from '@/composables/services/helpers.ts';
import { get, getList, create, deleteId, deleteIdWithData, createToast } from '@/composables/services/helpers.ts';

interface StudentsState {
students: Ref<Student[] | null>;
Expand Down Expand Up @@ -69,7 +69,8 @@ export function useStudents(): StudentsState {
async function createStudent(studentData: Student): Promise<void> {
const endpoint = endpoints.students.index;

await create<Student>(
await createToast<Student>(
'student',
endpoint,
{
user: studentData.id,
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/composables/services/teacher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { Teacher } from '@/types/users/Teacher.ts';
import { Response } from '@/types/Response';
import { type Ref, ref } from 'vue';
import { endpoints } from '@/config/endpoints.ts';
import { get, getList, create, deleteId, deleteIdWithData, getPaginatedList } from '@/composables/services/helpers.ts';
import {
get,
getList,
create,
deleteId,
deleteIdWithData,
getPaginatedList,
createToast,
} from '@/composables/services/helpers.ts';
import { type PaginatorResponse } from '@/types/filter/Paginator.ts';
import { type Filter } from '@/types/filter/Filter.ts';

Expand Down Expand Up @@ -62,7 +70,8 @@ export function useTeacher(): TeacherState {

async function createTeacher(user: User): Promise<void> {
const endpoint = endpoints.teachers.index;
await create<Teacher>(
await createToast<Teacher>(
'teacher',
endpoint,
{
user: user.id,
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/composables/services/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { User } from '@/types/users/User.ts';
import { type Response } from '@/types/Response.ts';
import { type PaginatorResponse } from '@/types/filter/Paginator.ts';
import { endpoints } from '@/config/endpoints.ts';
import { create, get, patch, getList, getPaginatedList } from '@/composables/services/helpers.ts';
import { get, patch, getList, getPaginatedList, createToast } from '@/composables/services/helpers.ts';
import { type Filter } from '@/types/filter/Filter.ts';

interface userState {
Expand Down Expand Up @@ -40,7 +40,8 @@ export function useUser(): userState {

async function createUser(userData: User): Promise<void> {
const endpoint = endpoints.users.index;
await create<User>(
await createToast<User>(
'user',
endpoint,
{
username: userData.username,
Expand Down

0 comments on commit 876ec0e

Please sign in to comment.