Skip to content

Commit

Permalink
Ported core packages and examples to Typecript
Browse files Browse the repository at this point in the history
  • Loading branch information
meta-paul committed Dec 2, 2024
1 parent caf8021 commit 3aa9709
Show file tree
Hide file tree
Showing 169 changed files with 10,826 additions and 23,217 deletions.
288 changes: 152 additions & 136 deletions examples/form_composer_demo/webapp/package-lock.json

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions examples/form_composer_demo/webapp/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "form_composer_demo",
"version": "1.0.0",
"version": "2.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
Expand All @@ -16,13 +16,18 @@
"keywords": [],
"author": "",
"dependencies": {
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@types/webpack-env": "^1.18.5",
"bootstrap": "^4.6.2",
"bootstrap-select": "^1.13.18",
"jquery": "^3.6.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-error-overlay": "^6.0.11",
"react-router-dom": "^6.26.0"
"react-router-dom": "^6.26.0",
"ts-loader": "^9.5.1",
"typescript": "^5.7.2"
},
"devDependencies": {
"@babel/cli": "^7.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* LICENSE file in the root directory of this source tree.
*/

import React from "react";
import ReactDOM from "react-dom";
import { ErrorBoundary, useMephistoTask } from "mephisto-core";
import * as React from "react";
import * as ReactDOM from "react-dom";
import {
FormComposerBaseFrontend,
LoadingScreen,
} from "./components/core_components_dynamic.jsx";
import { useMephistoTask, ErrorBoundary } from "mephisto-core";
} from "./components/core_components_dynamic";

/* ================= Application Components ================= */

Expand All @@ -20,6 +20,11 @@ function MainApp() {
initialTaskData,
handleSubmit,
handleFatalError,
}: {
isLoading: boolean;
initialTaskData: ConfigTaskType;
handleSubmit: Function;
handleFatalError: Function;
} = useMephistoTask();

if (isLoading || !initialTaskData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* LICENSE file in the root directory of this source tree.
*/

import React from "react";
import ReactDOM from "react-dom";
import { ErrorBoundary, useMephistoRemoteProcedureTask } from "mephisto-core";
import * as React from "react";
import * as ReactDOM from "react-dom";
import {
FormComposerBaseFrontend,
LoadingScreen,
} from "./components/core_components_remote_procedure.jsx";
import { useMephistoRemoteProcedureTask, ErrorBoundary } from "mephisto-core";
} from "./components/core_components_dynamic_remote_procedure";

/* ================= Application Components ================= */

Expand All @@ -21,14 +21,19 @@ function MainApp() {
remoteProcedure,
handleSubmit,
handleFatalError,
setTaskSubmitData,
}: {
isLoading: boolean;
initialTaskData: ConfigTaskType;
remoteProcedure: RemoteProcedureCollectionType;
handleSubmit: Function;
handleFatalError: Function;
} = useMephistoRemoteProcedureTask();

if (isLoading || !initialTaskData) {
return <LoadingScreen />;
}

let _initialTaskData = initialTaskData;
let _initialTaskData: ConfigTaskType = initialTaskData;
if (initialTaskData.hasOwnProperty("task_data")) {
_initialTaskData = initialTaskData.task_data;
}
Expand All @@ -41,7 +46,6 @@ function MainApp() {
onSubmit={handleSubmit}
onError={handleFatalError}
remoteProcedure={remoteProcedure}
setTaskSubmitData={setTaskSubmitData}
/>
</ErrorBoundary>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,43 @@
* LICENSE file in the root directory of this source tree.
*/

import { WorkerOpinion } from "mephisto-addons";
import { ErrorBoundary, useMephistoTask } from "mephisto-core";
import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
import { WelcomePage, WorkerOpinion } from "mephisto-addons";
import {
ErrorBoundary,
isWorkerOpinionEnabled,
useMephistoTask,
} from "mephisto-core";
import * as React from "react";
import * as ReactDOM from "react-dom";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import {
FormComposerBaseFrontend,
LoadingScreen,
OnboardingComponent,
} from "./components/core_components_simple.jsx";

let WITH_WORKER_OPINION = false;
try {
WITH_WORKER_OPINION = process.env.REACT_APP__WITH_WORKER_OPINION === "true";
} catch {}
} from "./components/core_components_simple";

const STORAGE_USERNAME_KEY = "username";
let WITH_WORKER_OPINION: boolean = isWorkerOpinionEnabled();

/* ================= Application Components ================= */

type HomePagePropsType = {
isLoading?: boolean;
initialTaskData: ConfigTaskType;
handleSubmit: Function;
handleFatalError: Function;
isOnboarding: boolean;
resonseSubmitted: boolean;
setResonseSubmitted: React.Dispatch<React.SetStateAction<boolean>>;
};

function HomePage({
handleFatalError,
handleSubmit,
initialTaskData,
isOnboarding,
resonseSubmitted,
setResonseSubmitted,
}) {
}: HomePagePropsType) {
// In case of visiting home page but without any GET-parameters
if (!initialTaskData?.form) {
return (
Expand All @@ -51,7 +60,7 @@ function HomePage({
<FormComposerBaseFrontend
taskData={initialTaskData}
isOnboarding={isOnboarding}
onSubmit={(data) => {
onSubmit={(data: FormComposerResultsType) => {
setResonseSubmitted(true);
handleSubmit(data);
}}
Expand All @@ -70,82 +79,30 @@ function HomePage({
);
}

function WelcomePage() {
const storagedUsername = localStorage.getItem(STORAGE_USERNAME_KEY);

const [username, setUsername] = React.useState(storagedUsername || "");

function generateRandomString(len, chars) {
chars =
chars || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let randomString = "";
for (let i = 0; i < len; i++) {
let randomPoz = Math.floor(Math.random() * chars.length);
randomString += chars.substring(randomPoz, randomPoz + 1);
}
return randomString;
}

function openTaskPage(e) {
e.preventDefault();

if (username === "") {
alert("Username cannot be empty");
} else {
localStorage.setItem(STORAGE_USERNAME_KEY, username);
window.location.replace(
`/?worker_id=${username}&id=${generateRandomString(10)}`
);
}
}

return (
<>
<div className="card container mt-xl-5" style={{ width: "600px" }}>
<div className="card-body">
<h5 className="card-title text-center mb-xl-4">
Welcome to Mephisto
</h5>

<form onSubmit={(e) => openTaskPage(e)}>
<p className="card-text mb-xl-3">
Please provide your username to start your task
</p>

<div className="form-group">
<input
className="form-control"
id="id_username"
placeholder={"Username"}
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
</div>

<button className={"btn btn-primary"} type={"submit"}>
Start task
</button>
</form>
</div>
</div>
</>
);
}

function MainApp() {
const {
blockedExplanation,
blockedReason,
isLoading,
initialTaskData,
handleFatalError,
handleSubmit,
initialTaskData,
isLoading,
isOnboarding,
blockedExplanation,
blockedReason,
}: {
isLoading: boolean;
initialTaskData: ConfigTaskType;
handleSubmit: Function;
handleFatalError: Function;
isOnboarding: boolean;
blockedExplanation: string;
blockedReason: string;
} = useMephistoTask();

const [resonseSubmitted, setResonseSubmitted] = useState(false);
const [resonseSubmitted, setResonseSubmitted] = React.useState<boolean>(
false
);

useEffect(() => {
React.useEffect(() => {
if (resonseSubmitted) {
// Scroll to the bollom of the page to reveal Worker Opinion block
window.scrollTo(0, document.body.scrollHeight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* LICENSE file in the root directory of this source tree.
*/

import React from "react";
import ReactDOM from "react-dom";
import { ErrorBoundary, useMephistoRemoteProcedureTask } from "mephisto-core";
import * as React from "react";
import * as ReactDOM from "react-dom";
import {
FormComposerBaseFrontend,
LoadingScreen,
} from "./components/core_components_dynamic_remote_procedure.jsx";
import { useMephistoRemoteProcedureTask, ErrorBoundary } from "mephisto-core";
} from "./components/core_components_simple_remote_procedure";

/* ================= Application Components ================= */

Expand All @@ -21,13 +21,19 @@ function MainApp() {
remoteProcedure,
handleSubmit,
handleFatalError,
}: {
isLoading: boolean;
initialTaskData: ConfigTaskType;
remoteProcedure: RemoteProcedureCollectionType;
handleSubmit: Function;
handleFatalError: Function;
} = useMephistoRemoteProcedureTask();

if (isLoading || !initialTaskData) {
return <LoadingScreen />;
}

let _initialTaskData = initialTaskData;
let _initialTaskData: ConfigTaskType = initialTaskData;
if (initialTaskData.hasOwnProperty("task_data")) {
_initialTaskData = initialTaskData.task_data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,44 @@
* LICENSE file in the root directory of this source tree.
*/

import React from "react";
import * as React from "react";
import { FormComposer } from "mephisto-addons";

// Required import for custom validators
import * as customValidators from "custom-validators";
// Required import for custom triggers
import * as customTriggers from "custom-triggers";

function LoadingScreen() {
return <Directions>Loading...</Directions>;
}
type DirectionsPropsType = {
children: React.ReactNode;
};

function Directions({ children }) {
function Directions({ children }: DirectionsPropsType) {
return (
<div className="card mb-4">
<div className="card-body container">{children}</div>
</div>
);
}

function LoadingScreen() {
return <Directions>Loading...</Directions>;
}

type FormComposerBaseFrontendPropsType = {
taskData: ConfigTaskType;
onSubmit?: Function;
onError?: Function;
finalResults?: FormComposerResultsType;
};

function FormComposerBaseFrontend({
taskData,
onSubmit,
onError,
finalResults = null,
}) {
let initialConfigFormData = taskData.form;
}: FormComposerBaseFrontendPropsType) {
let initialConfigFormData: ConfigFormType = taskData.form;

if (!initialConfigFormData) {
return <div>Passed form data is invalid... Recheck your task config.</div>;
Expand Down
Loading

0 comments on commit 3aa9709

Please sign in to comment.