Skip to content

Commit

Permalink
Move api call out of useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed Sep 23, 2021
1 parent 53ded44 commit 196834c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
33 changes: 18 additions & 15 deletions lms/static/scripts/frontend_apps/components/FilePickerApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ export default function FilePickerApp({ onSubmit }) {
* render.
*/
const [shouldSubmit, setShouldSubmit] = useState(false);
const submit = useCallback(() => setShouldSubmit(true), []);

// Submit the form after a selection is made via one of the available
// methods.
useEffect(() => {
const submit = useCallback(() => {
async function createAssignment() {
const data = {
...createAssignmentAPI.data,
Expand All @@ -112,30 +108,37 @@ export default function FilePickerApp({ onSubmit }) {
});
setExtLTIAssignmentId(assignment.ext_lti_assignment_id);
} catch (error) {
setErrorInfo({ title: 'Creating or editing an assignment', error: error });
setErrorInfo({
title: 'Creating or editing an assignment',
error: error,
});
}
}

if (content && createAssignmentAPI && !extLTIAssignmentId) {
createAssignment();
return;
}

if (shouldSubmit) {
// Submit form using a hidden button rather than calling `form.submit()`
// to facilitate observing the submission in tests and suppressing the
// actual submit.
submitButton.current.click();
}
setShouldSubmit(true);
}, [
shouldSubmit,
extLTIAssignmentId,
authToken,
content,
createAssignmentAPI,
extLTIAssignmentId,
groupConfig.groupSet,
]);

// Submit the form after a selection is made via one of the available
// methods.
useEffect(() => {
if (shouldSubmit) {
// Submit form using a hidden button rather than calling `form.submit()`
// to facilitate observing the submission in tests and suppressing the
// actual submit.
submitButton.current.click();
}
}, [shouldSubmit]);

/** @type {(c: Content) => void} */
const selectContent = useCallback(
content => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ describe('FilePickerApp', () => {
assert.deepEqual(formFields.props(), {
children: [],
content: expectedContent,
extLTIAssignmentId: expectedExtLTIAssignmentId,
formFields: fakeConfig.filePicker.formFields,
extLTIAssignmentId: expectedExtLTIAssignmentId,
groupSet: expectedGroupSet,
Expand Down

0 comments on commit 196834c

Please sign in to comment.