Skip to content

Commit

Permalink
Create assignments if configured by the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed Sep 20, 2021
1 parent 1858755 commit 057dae5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lms/static/scripts/frontend_apps/components/FilePickerApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'preact/hooks';

import { Config } from '../config';
import { apiCall } from '../utils/api';
import { truncateURL } from '../utils/format';

import ContentSelector from './ContentSelector';
Expand Down Expand Up @@ -58,14 +59,17 @@ function contentDescription(content) {
export default function FilePickerApp({ onSubmit }) {
const submitButton = useRef(/** @type {HTMLInputElement|null} */ (null));
const {
api: { authToken },
filePicker: {
formAction,
formFields,
createAssignmentAPI: createAssignmentAPI,
canvas: { groupsEnabled: enableGroupConfig, ltiLaunchUrl },
},
} = useContext(Config);

const [content, setContent] = useState(/** @type {Content|null} */ (null));
const [extLTIAssignmentId, setExtLTIAssignmentId] = useState(null);

const [groupConfig, setGroupConfig] = useState(
/** @type {GroupConfig} */ ({
Expand Down Expand Up @@ -94,13 +98,37 @@ export default function FilePickerApp({ onSubmit }) {
// Submit the form after a selection is made via one of the available
// methods.
useEffect(() => {
async function createAssignment() {
const data = { ...createAssignmentAPI.data };
data.content = content;
data.groupset = groupConfig.groupSet;
const assignment = await apiCall({
authToken,
path: createAssignmentAPI.path,
data: data,
});
setExtLTIAssignmentId(assignment.ext_lti_assignment_id);
}

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();
}
}, [shouldSubmit]);
}, [
shouldSubmit,
extLTIAssignmentId,
authToken,
content,
createAssignmentAPI,
groupConfig.groupSet,
]);

/** @type {(c: Content) => void} */
const selectContent = useCallback(
Expand Down Expand Up @@ -164,6 +192,7 @@ export default function FilePickerApp({ onSubmit }) {
ltiLaunchURL={ltiLaunchUrl}
content={content}
formFields={formFields}
extLTIAssignmentId={extLTIAssignmentId}
groupSet={groupConfig.useGroupSet ? groupConfig.groupSet : null}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { contentItemForContent } from '../utils/content-item';
* @prop {Record<string,string>} formFields - Form fields provided by the backend
* that should be included in the response without any changes
* @prop {string|null} groupSet
* @prop {string|null} extLTIAssignmentId
*/

/**
Expand All @@ -34,9 +35,13 @@ export default function FilePickerFormFields({
formFields,
groupSet,
ltiLaunchURL,
extLTIAssignmentId,
}) {
/** @type {Record<string,string>} */
const extraParams = groupSet ? { group_set: groupSet } : {};
if (extLTIAssignmentId) {
extraParams.ext_lti_assignment_id = extLTIAssignmentId;
}
const contentItem = JSON.stringify(
contentItemForContent(ltiLaunchURL, content, extraParams)
);
Expand Down

0 comments on commit 057dae5

Please sign in to comment.