From e31613533c88e78ca0aae927fd4dbfbf52ae9bac Mon Sep 17 00:00:00 2001 From: Miodec Date: Thu, 8 Feb 2024 17:57:45 +0100 Subject: [PATCH] refactor(types): result types move to shared types (#4774) add types to ape client (#4786) --- backend/src/api/controllers/result.ts | 4 +++- frontend/src/ts/ape/endpoints/results.ts | 13 +++++++++---- frontend/src/ts/ape/types/results.d.ts | 9 +++++++++ frontend/src/ts/popups/result-tags-popup.ts | 3 ++- frontend/src/ts/test/test-logic.ts | 2 +- shared-types/types.d.ts | 12 ++++++++++++ 6 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 frontend/src/ts/ape/types/results.d.ts diff --git a/backend/src/api/controllers/result.ts b/backend/src/api/controllers/result.ts index 87e62a07b..c2d125bb2 100644 --- a/backend/src/api/controllers/result.ts +++ b/backend/src/api/controllers/result.ts @@ -621,7 +621,9 @@ export async function addResult( ); } - const data: AddResultData = { + const data: Omit & { + insertedId: ObjectId; + } = { isPb, tagPbs, insertedId: addedResult.insertedId, diff --git a/frontend/src/ts/ape/endpoints/results.ts b/frontend/src/ts/ape/endpoints/results.ts index 9e17df998..0cc1e69c3 100644 --- a/frontend/src/ts/ape/endpoints/results.ts +++ b/frontend/src/ts/ape/endpoints/results.ts @@ -5,25 +5,30 @@ export default class Results { this.httpClient = httpClient; } - async get(offset?: number): Ape.EndpointResponse { + async get( + offset?: number + ): Ape.EndpointResponse[]> { return await this.httpClient.get(BASE_PATH, { searchQuery: { offset } }); } async save( result: SharedTypes.Result - ): Ape.EndpointResponse { + ): Ape.EndpointResponse { return await this.httpClient.post(BASE_PATH, { payload: { result }, }); } - async updateTags(resultId: string, tagIds: string[]): Ape.EndpointResponse { + async updateTags( + resultId: string, + tagIds: string[] + ): Ape.EndpointResponse { return await this.httpClient.patch(`${BASE_PATH}/tags`, { payload: { resultId, tagIds }, }); } - async deleteAll(): Ape.EndpointResponse { + async deleteAll(): Ape.EndpointResponse { return await this.httpClient.delete(BASE_PATH); } } diff --git a/frontend/src/ts/ape/types/results.d.ts b/frontend/src/ts/ape/types/results.d.ts new file mode 100644 index 000000000..d0b0a040f --- /dev/null +++ b/frontend/src/ts/ape/types/results.d.ts @@ -0,0 +1,9 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +// for some reason when using the dot notaion, the types are not being recognized as used +declare namespace Ape.Results { + type PostResult = SharedTypes.PostResultResponse; + type PatchResult = { + tagPbs: string[]; + }; + type DeleteAll = null; +} diff --git a/frontend/src/ts/popups/result-tags-popup.ts b/frontend/src/ts/popups/result-tags-popup.ts index 7f5baa1f8..d8f79e344 100644 --- a/frontend/src/ts/popups/result-tags-popup.ts +++ b/frontend/src/ts/popups/result-tags-popup.ts @@ -149,7 +149,8 @@ $("#resultEditTagsPanelWrapper .confirmButton").on("click", async () => { ); } - const responseTagPbs = response.data.tagPbs; + //can do this because the response will not be null if the status is 200 + const responseTagPbs = response.data?.tagPbs ?? []; Notifications.add("Tags updated", 1, { duration: 2, diff --git a/frontend/src/ts/test/test-logic.ts b/frontend/src/ts/test/test-logic.ts index 2026f1723..c2ddec133 100644 --- a/frontend/src/ts/test/test-logic.ts +++ b/frontend/src/ts/test/test-logic.ts @@ -1198,7 +1198,7 @@ async function saveResult( $("#result .stats .tags .editTagsButton").attr( "result-id", - response.data.insertedId + response.data?.insertedId as string //if status is 200 then response.data is not null or undefined ); $("#result .stats .tags .editTagsButton").removeClass("invisible"); diff --git a/shared-types/types.d.ts b/shared-types/types.d.ts index 4ce4a0ae6..919c725da 100644 --- a/shared-types/types.d.ts +++ b/shared-types/types.d.ts @@ -454,4 +454,16 @@ declare namespace SharedTypes { badgeId: number | null; hidden?: boolean; } + + type PostResultResponse = { + isPb: boolean; + tagPbs: string[]; + insertedId: string; + dailyLeaderboardRank?: number; + weeklyXpLeaderboardRank?: number; + xp: number; + dailyXpBonus: boolean; + xpBreakdown: Record; + streak: number; + }; }