-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'BC-5444-typing-date' of https://github.com/hpi-schul-cl…
…oud/nuxt-client into BC-5444-typing-date
- Loading branch information
Showing
23 changed files
with
807 additions
and
620 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/components/data-external-tool/ExternalToolApi.composable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
ToolApiFactory, | ||
ToolApiInterface, | ||
ToolLaunchRequestResponse, | ||
} from "@/serverApi/v3"; | ||
import { ToolLaunchRequest } from "@/store/external-tool"; | ||
import { ExternalToolMapper } from "@/store/external-tool/mapper"; | ||
import { $axios } from "@/utils/api"; | ||
import { AxiosResponse } from "axios"; | ||
|
||
export const useExternalToolApi = () => { | ||
const toolApi: ToolApiInterface = ToolApiFactory(undefined, "/v3", $axios); | ||
|
||
const fetchLaunchDataCall = async ( | ||
contextExternalToolId: string | ||
): Promise<ToolLaunchRequest> => { | ||
const response: AxiosResponse<ToolLaunchRequestResponse> = | ||
await toolApi.toolLaunchControllerGetToolLaunchRequest( | ||
contextExternalToolId | ||
); | ||
|
||
const mapped: ToolLaunchRequest = ExternalToolMapper.mapToToolLaunchRequest( | ||
response.data | ||
); | ||
|
||
return mapped; | ||
}; | ||
|
||
return { | ||
fetchLaunchDataCall, | ||
}; | ||
}; |
65 changes: 65 additions & 0 deletions
65
src/components/data-external-tool/ExternalToolApi.composable.unit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as serverApi from "@/serverApi/v3/api"; | ||
import { ToolLaunchRequestResponse } from "@/serverApi/v3/api"; | ||
import { | ||
ToolLaunchRequest, | ||
ToolLaunchRequestMethodEnum, | ||
} from "@/store/external-tool"; | ||
import { | ||
mockApiResponse, | ||
toolLaunchRequestResponseFactory, | ||
} from "@@/tests/test-utils"; | ||
import { createMock, DeepMocked } from "@golevelup/ts-jest"; | ||
import { useExternalToolApi } from "./ExternalToolApi.composable"; | ||
|
||
describe("ExternalToolApi.composable", () => { | ||
let toolApi: DeepMocked<serverApi.ToolApiInterface>; | ||
|
||
beforeEach(() => { | ||
toolApi = createMock<serverApi.ToolApiInterface>(); | ||
|
||
jest.spyOn(serverApi, "ToolApiFactory").mockReturnValue(toolApi); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("fetchLaunchDataCall", () => { | ||
const setup = () => { | ||
const launchRequest: ToolLaunchRequestResponse = | ||
toolLaunchRequestResponseFactory.build(); | ||
|
||
toolApi.toolLaunchControllerGetToolLaunchRequest.mockResolvedValue( | ||
mockApiResponse({ data: launchRequest }) | ||
); | ||
|
||
return { | ||
launchRequest, | ||
}; | ||
}; | ||
|
||
it("should call the api for a tool launch request", async () => { | ||
setup(); | ||
|
||
await useExternalToolApi().fetchLaunchDataCall("contextExternalToolId"); | ||
|
||
expect( | ||
toolApi.toolLaunchControllerGetToolLaunchRequest | ||
).toHaveBeenCalledWith("contextExternalToolId"); | ||
}); | ||
|
||
it("should return launch request data", async () => { | ||
const { launchRequest } = setup(); | ||
|
||
const result: ToolLaunchRequest = | ||
await useExternalToolApi().fetchLaunchDataCall("contextExternalToolId"); | ||
|
||
expect(result).toEqual<ToolLaunchRequest>({ | ||
url: launchRequest.url, | ||
payload: launchRequest.payload, | ||
method: ToolLaunchRequestMethodEnum.Get, | ||
openNewTab: launchRequest.openNewTab, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/components/data-external-tool/ExternalToolLaunchState.composable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { | ||
ToolLaunchRequest, | ||
ToolLaunchRequestMethodEnum, | ||
} from "@/store/external-tool"; | ||
import { BusinessError } from "@/store/types/commons"; | ||
import { HttpStatusCode } from "@/store/types/http-status-code.enum"; | ||
import { mapAxiosErrorToResponseError } from "@/utils/api"; | ||
import { ref, Ref } from "vue"; | ||
import { useExternalToolApi } from "./ExternalToolApi.composable"; | ||
|
||
export const useExternalToolLaunchState = () => { | ||
const { fetchLaunchDataCall } = useExternalToolApi(); | ||
|
||
const isLoading: Ref<boolean> = ref(false); | ||
const error: Ref<BusinessError | undefined> = ref(); | ||
const toolLaunchRequest: Ref<ToolLaunchRequest | undefined> = ref(); | ||
|
||
const fetchLaunchRequest = async ( | ||
contextExternalToolId: string | ||
): Promise<void> => { | ||
isLoading.value = true; | ||
error.value = undefined; | ||
|
||
try { | ||
toolLaunchRequest.value = await fetchLaunchDataCall( | ||
contextExternalToolId | ||
); | ||
} catch (axiosError: unknown) { | ||
const apiError = mapAxiosErrorToResponseError(axiosError); | ||
|
||
error.value = { | ||
error: apiError, | ||
message: apiError.message, | ||
statusCode: apiError.code, | ||
}; | ||
} | ||
|
||
isLoading.value = false; | ||
}; | ||
|
||
const launchTool = () => { | ||
if (!toolLaunchRequest.value) { | ||
return; | ||
} | ||
|
||
switch (toolLaunchRequest.value.method) { | ||
case ToolLaunchRequestMethodEnum.Get: | ||
handleGetLaunchRequest(toolLaunchRequest.value); | ||
break; | ||
case ToolLaunchRequestMethodEnum.Post: | ||
handlePostLaunchRequest(toolLaunchRequest.value); | ||
break; | ||
default: | ||
error.value = { | ||
message: "Unknown launch method", | ||
statusCode: HttpStatusCode.UnprocessableEntity, | ||
}; | ||
break; | ||
} | ||
}; | ||
|
||
const handleGetLaunchRequest = (toolLaunch: ToolLaunchRequest) => { | ||
if (toolLaunch.openNewTab) { | ||
window.open(toolLaunch.url, "_blank"); | ||
return; | ||
} | ||
window.location.href = toolLaunch.url; | ||
}; | ||
|
||
const handlePostLaunchRequest = (toolLaunch: ToolLaunchRequest) => { | ||
const form: HTMLFormElement = document.createElement("form"); | ||
form.method = "POST"; | ||
form.action = toolLaunch.url; | ||
form.target = toolLaunch.openNewTab ? "_blank" : "_self"; | ||
form.id = "launch-form"; | ||
|
||
const payload = JSON.parse(toolLaunch.payload || "{}"); | ||
|
||
for (const key in payload) { | ||
if (Object.prototype.hasOwnProperty.call(payload, key)) { | ||
const hiddenField = document.createElement("input"); | ||
hiddenField.type = "hidden"; | ||
hiddenField.name = key; | ||
hiddenField.value = payload[key]; | ||
|
||
form.appendChild(hiddenField); | ||
} | ||
} | ||
|
||
document.body.appendChild(form); | ||
form.submit(); | ||
}; | ||
|
||
return { | ||
toolLaunchRequest, | ||
error, | ||
isLoading, | ||
fetchLaunchRequest, | ||
launchTool, | ||
}; | ||
}; |
Oops, something went wrong.