Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to customize html-static-task-example submit button #879

Merged
merged 8 commits into from
Aug 3, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React from "react";
import ReactDOM from "react-dom";
import { Button } from "react-bootstrap";
import { useMephistoTask, postData } from "mephisto-task";
import { useMephistoGlobalConfig } from "./hooks";
const axios = require("axios");

/* global
Expand Down Expand Up @@ -98,27 +99,20 @@ function MainApp() {

function SubmitFrame({ children, onSubmit, currentTask }) {
const [submitting, setSubmitting] = React.useState(false);
const [isSubmitButtonHidden, setIsSubmitButtonHidden] = React.useState(false);

const handleHideSubmitButtonEvent = React.useCallback(
(hideValue) => {
if (typeof hideValue == "boolean") setIsSubmitButtonHidden(hideValue);
},
[setIsSubmitButtonHidden]
const [
isSubmitButtonHidden,
setIsSubmitButtonHidden,
] = useMephistoGlobalConfig(
"HIDE_SUBMIT_BUTTON",
false,
(val) => typeof val === "boolean"
);

React.useEffect(() => {
// Reset submitting when switching from onboarding
setSubmitting(false);
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

what was the rationale behind removing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's a mistake, I don't even know how that got updated.


React.useEffect(() => {
window._MEPHISTO_CONFIG_.EVENT_EMITTER.on(
"HIDE_SUBMIT_BUTTON",
handleHideSubmitButtonEvent
);
}, [setIsSubmitButtonHidden]);

function handleFormSubmit(event) {
event.preventDefault();
setSubmitting(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState, useCallback, useEffect } from "react";

/**
* This hook is to be used with events that are supposed to update state when they are consumed.
* @param {string} eventName
Copy link
Contributor

Choose a reason for hiding this comment

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

small nit: I would rename eventName to configName.

I consider HIDE_SUBMIT_BUTTON to be a config property, not an event

* @param {any} defaultValue
* @param {function} validatorFunction
* @returns [any any]
*/
export function useMephistoGlobalConfig(
Copy link
Contributor

@pringshia pringshia Aug 3, 2022

Choose a reason for hiding this comment

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

this method looks great implementation-wise! Thanks for the jsdocs as well :)

eventName,
defaultValue,
validatorFunction
) {
const [configState, setConfigState] = useState(defaultValue ?? false);

const handleEvent = useCallback(
(eventValue) => {
if (validatorFunction) {
if (validatorFunction(eventValue)) {
setConfigState(eventValue);
}
} else setConfigState(eventValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

both conditional paths are calling setConfigState here...?

Copy link
Contributor

Choose a reason for hiding this comment

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

nvm, i see one is for the outer branch

},
[setConfigState]
);

useEffect(() => {
window._MEPHISTO_CONFIG_.EVENT_EMITTER.on(eventName, handleEvent);
}, [setConfigState]);

return [configState, setConfigState];
}