frontAnswerer/src/widgets/container/ContainerWidget.tsx

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-04-24 15:56:11 +00:00
import QuizAnswerer from "@/components/QuizAnswerer";
import { Root, createRoot } from "react-dom/client";
import { pollForSelector } from "../shared/pollForSelector";
2024-04-24 15:56:11 +00:00
export class ContainerWidget {
root: Root | undefined;
constructor({ selector, quizId, selectorPollingTimeLimit = 60 }: {
2024-04-24 15:56:11 +00:00
quizId: string;
selector: string;
/**
* In seconds, null - polling disabled
*/
selectorPollingTimeLimit?: number | null;
2024-04-24 15:56:11 +00:00
}) {
const element = document.querySelector(selector);
if (element) {
this.root = createRoot(element);
this.root.render(
<QuizAnswerer
quizId={quizId}
changeFaviconAndTitle={false}
disableGlobalCss
/>
);
2024-04-24 15:56:11 +00:00
return;
}
2024-04-24 15:56:11 +00:00
if (!selectorPollingTimeLimit) {
console.error(`Не удалось найти элемент ${selector} для вставки виджета`);
return;
}
pollForSelector(selector, selectorPollingTimeLimit, (element) => {
this.root = createRoot(element);
this.root.render(
<QuizAnswerer
quizId={quizId}
changeFaviconAndTitle={false}
disableGlobalCss
/>
);
});
2024-04-24 15:56:11 +00:00
}
destroy() {
if (this.root) this.root.unmount();
}
}