Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/IsaacApiTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,11 @@ export interface MisuseStatisticDTO {
currentCounter: number;
}

export interface AnvilMarkingRequestDTO {
payload: string;
hmac: string;
}

export type GameboardCreationMethod = "FILTER" | "BUILDER";

export type EventStatus = "OPEN" | "FULLY_BOOKED" | "CANCELLED" | "CLOSED" | "WAITING_LIST_ONLY" | "RESERVATION_ONLY";
Expand Down
31 changes: 17 additions & 14 deletions src/app/components/content/AnvilApp.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {RefObject, useContext, useEffect} from 'react';
import {AnvilAppDTO} from "../../../IsaacApiTypes";
import {selectors, useAppSelector} from "../../state";
import {selectors, useAppSelector, usePostSkillsAnswerMutation} from "../../state";
import {AccordionSectionContext, QuestionContext} from "../../../IsaacAppTypes";
import {selectQuestionPart} from "../../services";
import { AnvilCookieHandler } from '../handlers/InterstitialCookieHandler';
Expand Down Expand Up @@ -67,26 +67,29 @@ export const AnvilApp = ({doc}: AnvilAppProps) => {
}).join('&');

const iframeSrc = `${baseURL}#?${queryParams}`;

const onMessage = function(e: any) {
if (iframeRef.current && e.source !== (iframeRef.current as HTMLIFrameElement).contentWindow) {
return;
}

const data = e.data;

if (iframeRef.current && (data.fn == "newAppHeight")) {
(iframeRef.current as HTMLIFrameElement).height = data.newHeight + 15;
}
};
const [postSkillsAnswer] = usePostSkillsAnswerMutation();

useEffect(() => {
const onMessage = function(e: any) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can certainly narrow the type a little here; if all the available types were known we could put that in place of this any too?

Suggested change
const onMessage = function(e: any) {
const onMessage = function(e: MessageEvent<any>) {

if (iframeRef.current && e.source !== (iframeRef.current as HTMLIFrameElement).contentWindow) {
return;
}

const data = e.data;

if (iframeRef.current && (data.fn == "newAppHeight")) {
(iframeRef.current as HTMLIFrameElement).height = data.newHeight + 15;
} else if (iframeRef.current && user?.loggedIn && e.origin === `https://${doc.appId?.toLowerCase()}.anvil.app` && data.type === 'SUBMISSION_MARKED') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is the e.origin check for here? Why do we not do it for newAppHeight? Perhaps we would rather return immediately at the start if this is not the case?

void postSkillsAnswer({ appId: `${typeof page === 'object' && page?.id}`, body: { hmac: data.hmac, payload: data.payload } });
}
};

window.addEventListener("message", onMessage);

return () => {
window.removeEventListener("message", onMessage);
};
}, [onMessage]);
}, [postSkillsAnswer, doc.appId, page, user?.loggedIn]);

return <AnvilCookieHandler afterAcceptedElement={
<iframe ref={iframeRef} src={iframeSrc} title={title} className="anvil-app"/>
Expand Down
1 change: 1 addition & 0 deletions src/app/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export * from "./slices/api/accountApi";
export * from "./slices/api/gameboardApi";
export * from "./slices/api/assignmentsApi";
export * from "./slices/api/bookmarksApi";
export * from "./slices/api/skillsApi";
export * from "./slices/api/contentApi";
export * from "./slices/api/groupsApi";
export * from "./slices/api/emailApi";
Expand Down
20 changes: 20 additions & 0 deletions src/app/state/slices/api/skillsApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { AnvilMarkingRequestDTO } from "../../../../IsaacApiTypes";
import {isaacApi} from "./baseApi";
import { onQueryLifecycleEvents } from "./utils";

export const skillsApi = isaacApi.injectEndpoints({
endpoints: (build) => ({
postSkillsAnswer: build.mutation<void, { appId: string, body: AnvilMarkingRequestDTO}>({
query: ({ appId, body }) => ({
url: `skills/${appId}/answer`,
method: "POST",
body,
}),
onQueryStarted: onQueryLifecycleEvents({
errorTitle: "Couldn't save answer.",
}),
}),
})
});

export const { usePostSkillsAnswerMutation } = skillsApi;
Loading