-
Notifications
You must be signed in to change notification settings - Fork 3
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
Allow re-enrollment in recipe #51
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,38 +1,129 @@ | ||||||
import { ChangeEvent, FC, useState, useCallback } from "react"; | ||||||
import { Form, Container, Button } from "react-bootstrap"; | ||||||
import { ChangeEvent, FC, useState, useCallback, useMemo } from "react"; | ||||||
import { Form, Container, Button, Modal } from "react-bootstrap"; | ||||||
import { NimbusExperiment } from "@mozilla/nimbus-schemas"; | ||||||
|
||||||
import { useToastsContext } from "../hooks/useToasts"; | ||||||
|
||||||
const RecipeEnrollmentPage: FC = () => { | ||||||
const [jsonInput, setJsonInput] = useState(""); | ||||||
const [enrollError, setEnrollError] = | ||||||
useState<EnrollInExperimentResult["error"]>(null); | ||||||
const { addToast } = useToastsContext(); | ||||||
|
||||||
const recipe = useMemo<NimbusExperiment | null>(() => { | ||||||
try { | ||||||
return JSON.parse(jsonInput) as NimbusExperiment; | ||||||
} catch { | ||||||
return null; | ||||||
} | ||||||
}, [jsonInput]); | ||||||
|
||||||
const slug = recipe?.slug || ""; | ||||||
Comment on lines
+13
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're doing this all to get the slug. Instead, lets change this slightly: type EnrollError = EnrollInExperimentResult["error"] & { slug: string; };
const [enrollError, setEnrollError] = useState<EnrollError | null>(null);
// line 63
setEnrollError({ slug: parsedRecipe.slug ?? "", ...result.error }); Since we're already parsing this to pass it off to the API, we may as well grab the slug there and only if we're parsing successfully and there is an error. otherwise, just wasted work and threading data through awkwardly. |
||||||
|
||||||
const handleInputChange = useCallback( | ||||||
(event: ChangeEvent<HTMLTextAreaElement>) => { | ||||||
setJsonInput(event.target.value); | ||||||
}, | ||||||
[], | ||||||
[setJsonInput], | ||||||
); | ||||||
|
||||||
const handleEnrollClick = useCallback(async () => { | ||||||
try { | ||||||
const result = await browser.experiments.nimbus.enrollInExperiment( | ||||||
JSON.parse(jsonInput) as object, | ||||||
); | ||||||
const handleEnrollClick = useCallback( | ||||||
async ( | ||||||
event?: React.MouseEvent<HTMLButtonElement>, | ||||||
forceEnroll = false, | ||||||
) => { | ||||||
if (!jsonInput) { | ||||||
addToast({ message: "Invalid Input: Enter JSON", variant: "danger" }); | ||||||
return; | ||||||
} | ||||||
|
||||||
if (result) { | ||||||
addToast({ message: "Enrollment successful", variant: "success" }); | ||||||
setJsonInput(""); | ||||||
} else { | ||||||
addToast({ message: "Enrollment failed", variant: "danger" }); | ||||||
let parsedRecipe; | ||||||
try { | ||||||
parsedRecipe = JSON.parse(jsonInput) as object; | ||||||
} catch (error) { | ||||||
addToast({ | ||||||
message: `Error enrolling into experiment: ${ | ||||||
(error as Error).message ?? String(error) | ||||||
}`, | ||||||
variant: "danger", | ||||||
}); | ||||||
return; | ||||||
} | ||||||
} catch (error) { | ||||||
addToast({ | ||||||
message: `Error enrolling into experiment: ${(error as Error).message ?? String(error)}`, | ||||||
variant: "danger", | ||||||
}); | ||||||
|
||||||
try { | ||||||
const result = await browser.experiments.nimbus.enrollInExperiment( | ||||||
parsedRecipe as object, | ||||||
forceEnroll, | ||||||
); | ||||||
|
||||||
if (result.enrolled) { | ||||||
addToast({ message: "Enrollment successful", variant: "success" }); | ||||||
setJsonInput(""); | ||||||
} else if (result.error) { | ||||||
setEnrollError(result.error); | ||||||
} | ||||||
} catch (error) { | ||||||
addToast({ | ||||||
message: `Error enrolling into experiment: ${ | ||||||
(error as Error).message ?? String(error) | ||||||
}`, | ||||||
variant: "danger", | ||||||
}); | ||||||
} | ||||||
}, | ||||||
[jsonInput, setJsonInput, addToast, setEnrollError], | ||||||
); | ||||||
|
||||||
const handleModalConfirm = useCallback(async () => { | ||||||
setEnrollError(null); | ||||||
await handleEnrollClick(undefined, true); | ||||||
}, [handleEnrollClick, setEnrollError]); | ||||||
|
||||||
const handleModalClose = useCallback(() => { | ||||||
setEnrollError(null); | ||||||
}, [setEnrollError]); | ||||||
|
||||||
function EnrollmentError( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a top level function, not nested inside the component fn if its nested inside, it will always trigger a re-render because the function is redefined every time the component is called |
||||||
slug: string, | ||||||
enrollError: EnrollInExperimentResult["error"], | ||||||
) { | ||||||
if (!enrollError) { | ||||||
return null; | ||||||
} | ||||||
}, [jsonInput, setJsonInput, addToast]); | ||||||
const { activeEnrollment, slugExistsInStore } = enrollError; | ||||||
|
||||||
if (activeEnrollment && slugExistsInStore) { | ||||||
return ( | ||||||
<p> | ||||||
There is already an enrollment for the slug: <strong>{slug}</strong>. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only true if slug == activeEnrollment There are technically two cases, slug == activeEnrollment and slug !== activeEnrollment (ie there is already an active enrollment for this feature but the slug doesnt match this one) |
||||||
Would you like to proceed with force enrollment by unenrolling, | ||||||
deleting, and re-enrolling into the new configuration? | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
</p> | ||||||
); | ||||||
} | ||||||
|
||||||
if (activeEnrollment) { | ||||||
return ( | ||||||
<p> | ||||||
There is an active enrollment for the slug: <strong>{slug}</strong>. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually might not be the same slug.
Suggested change
|
||||||
Would you like to unenroll from the active enrollment and re-enroll | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
into the new configuration? | ||||||
</p> | ||||||
); | ||||||
} | ||||||
|
||||||
if (slugExistsInStore) { | ||||||
return ( | ||||||
<p> | ||||||
There is an inactive enrollment stored for the slug:{" "} | ||||||
<strong>{slug}</strong>. Would you like to delete the inactive | ||||||
enrollment and re-enroll into the new configuration? | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
</p> | ||||||
); | ||||||
} | ||||||
|
||||||
return null; | ||||||
} | ||||||
|
||||||
return ( | ||||||
<Container className="main-content m-0 p-2"> | ||||||
|
@@ -53,6 +144,21 @@ const RecipeEnrollmentPage: FC = () => { | |||||
Enroll | ||||||
</Button> | ||||||
</Form> | ||||||
|
||||||
<Modal show={!!enrollError} onHide={handleModalClose}> | ||||||
<Modal.Header closeButton> | ||||||
<Modal.Title>Force Enrollment</Modal.Title> | ||||||
</Modal.Header> | ||||||
<Modal.Body>{EnrollmentError(slug, enrollError)}</Modal.Body> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EnrollmentError is a component:
Suggested change
|
||||||
<Modal.Footer> | ||||||
<Button variant="secondary" onClick={handleModalClose}> | ||||||
Cancel | ||||||
</Button> | ||||||
<Button variant="danger" onClick={handleModalConfirm}> | ||||||
Force Enroll | ||||||
</Button> | ||||||
</Modal.Footer> | ||||||
</Modal> | ||||||
</Container> | ||||||
); | ||||||
}; | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't actually know its a valid NimbusExperiment here, unfortunately. It should be
unknown | null