|
| 1 | +import { FormattedMessage, useIntl } from "@edx/frontend-platform/i18n"; |
| 2 | +import { Button, ButtonGroup, Form, Stack } from "@openedx/paragon"; |
| 3 | +import { useConfigureSubsection, useCourseDetails, useCourseItemData } from "@src/course-outline/data/apiHooks"; |
| 4 | +import { ConfigureSubsectionData } from "@src/course-outline/data/types"; |
| 5 | +import { useOutlineSidebarContext } from "@src/course-outline/outline-sidebar/OutlineSidebarContext"; |
| 6 | +import { useCourseAuthoringContext } from "@src/CourseAuthoringContext"; |
| 7 | +import { VisibilityTypes } from "@src/data/constants"; |
| 8 | +import { DatepickerControl, DATEPICKER_TYPES } from "@src/generic/datepicker-control"; |
| 9 | +import { SidebarContent, SidebarSection } from "@src/generic/sidebar" |
| 10 | +import { useStateWithCallback } from "@src/hooks"; |
| 11 | +import { FormEvent, useState } from "react"; |
| 12 | +import messages from './messages'; |
| 13 | + |
| 14 | +interface Props { |
| 15 | + subsectionId: string; |
| 16 | +} |
| 17 | + |
| 18 | +const defaultPrereqScore = (val: string | number | null | undefined) => { |
| 19 | + if (val === null || val === undefined) { |
| 20 | + return 100; |
| 21 | + } |
| 22 | + const parsed = parseFloat(val.toString()); |
| 23 | + return isNaN(parsed) ? 100 : parsed; |
| 24 | +}; |
| 25 | + |
| 26 | +interface SubProps extends Props { |
| 27 | + onChange: (variables: Partial<ConfigureSubsectionData>) => void; |
| 28 | +} |
| 29 | + |
| 30 | +const ReleaseSection = ({ subsectionId, onChange }: SubProps) => { |
| 31 | + const intl = useIntl(); |
| 32 | + const { data: itemData } = useCourseItemData(subsectionId); |
| 33 | + const [localState, setLocalState] = useStateWithCallback( |
| 34 | + itemData?.start, |
| 35 | + (val) => onChange({ releaseDate: val }), |
| 36 | + ); |
| 37 | + |
| 38 | + return ( |
| 39 | + <SidebarSection |
| 40 | + title={intl.formatMessage(messages.subsectionReleaseTitle)} |
| 41 | + > |
| 42 | + <Stack className="mt-3" direction="horizontal" gap={3}> |
| 43 | + <DatepickerControl |
| 44 | + type={DATEPICKER_TYPES.date} |
| 45 | + value={localState} |
| 46 | + label={intl.formatMessage(messages.releaseDateLabel)} |
| 47 | + controlName="state-date" |
| 48 | + onChange={setLocalState} |
| 49 | + /> |
| 50 | + <DatepickerControl |
| 51 | + type={DATEPICKER_TYPES.time} |
| 52 | + value={localState} |
| 53 | + label={intl.formatMessage(messages.releaseTimeLabel)} |
| 54 | + controlName="start-time" |
| 55 | + onChange={setLocalState} |
| 56 | + /> |
| 57 | + </Stack> |
| 58 | + </SidebarSection> |
| 59 | + |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +const GradingSection = ({ subsectionId, onChange }: SubProps) => { |
| 64 | + const intl = useIntl(); |
| 65 | + const { data: itemData } = useCourseItemData(subsectionId); |
| 66 | + const [graded, setGraded] = useState(itemData?.graded); |
| 67 | + const { courseId } = useCourseAuthoringContext(); |
| 68 | + const { data: courseDetails } = useCourseDetails(courseId); |
| 69 | + const [localState, setLocalState] = useStateWithCallback<Partial<ConfigureSubsectionData>>( |
| 70 | + { |
| 71 | + graderType: itemData?.format, |
| 72 | + dueDate: itemData?.due || '', |
| 73 | + }, |
| 74 | + (val) => onChange(val || {}) |
| 75 | + ); |
| 76 | + |
| 77 | + const setUngraded = () => { |
| 78 | + setGraded(false); |
| 79 | + onChange({ graderType: "notgraded" }); |
| 80 | + } |
| 81 | + |
| 82 | + const createOptions = () => itemData?.courseGraders?.map((option) => ( |
| 83 | + <option key={option} value={option}> {option} </option> |
| 84 | + )); |
| 85 | + |
| 86 | + return ( |
| 87 | + <SidebarSection |
| 88 | + title={intl.formatMessage(messages.subsectionGradingTitle)} |
| 89 | + > |
| 90 | + <ButtonGroup toggle> |
| 91 | + <Button |
| 92 | + variant={graded ? 'outline-primary' : 'primary'} |
| 93 | + onClick={setUngraded} |
| 94 | + > |
| 95 | + <FormattedMessage {...messages.subsectionGradingUngradedBtn} /> |
| 96 | + </Button> |
| 97 | + <Button |
| 98 | + variant={graded ? 'primary' : 'outline-primary'} |
| 99 | + onClick={() => setGraded(true)} |
| 100 | + > |
| 101 | + <FormattedMessage {...messages.subsectionGradingGradedBtn} /> |
| 102 | + </Button> |
| 103 | + </ButtonGroup> |
| 104 | + {graded && |
| 105 | + <Form.Group> |
| 106 | + <Form.Label className="x-small"> |
| 107 | + <FormattedMessage {...messages.subsectionGradingDropdownLabel} /> |
| 108 | + </Form.Label> |
| 109 | + <Form.Control |
| 110 | + as="select" |
| 111 | + defaultValue={itemData?.format} |
| 112 | + onChange={(e) => setLocalState({ ...localState, graderType: e.target.value })} |
| 113 | + data-testid="grader-type-select" |
| 114 | + > |
| 115 | + <option key="notgraded" value="notgraded"> |
| 116 | + {intl.formatMessage(messages.subsectionGradingDropdownPlaceholder)} |
| 117 | + </option> |
| 118 | + {createOptions()} |
| 119 | + </Form.Control> |
| 120 | + </Form.Group> |
| 121 | + } |
| 122 | + {!courseDetails?.selfPaced && graded && |
| 123 | + <Stack className="mt-3" direction="horizontal" gap={3}> |
| 124 | + <DatepickerControl |
| 125 | + type={DATEPICKER_TYPES.date} |
| 126 | + value={localState?.dueDate} |
| 127 | + label={intl.formatMessage(messages.subsectionGradingDueDateLabel)} |
| 128 | + controlName="state-date" |
| 129 | + onChange={(val) => setLocalState({ ...localState, dueDate: val })} |
| 130 | + data-testid="due-date-picker" |
| 131 | + /> |
| 132 | + <DatepickerControl |
| 133 | + type={DATEPICKER_TYPES.time} |
| 134 | + value={localState?.dueDate} |
| 135 | + label={intl.formatMessage(messages.subsectionGradingDueTimeLabel)} |
| 136 | + controlName="start-time" |
| 137 | + onChange={(val) => setLocalState({ ...localState, dueDate: val })} |
| 138 | + /> |
| 139 | + </Stack> |
| 140 | + } |
| 141 | + </SidebarSection> |
| 142 | + ); |
| 143 | +} |
| 144 | + |
| 145 | +export const SubsectionSettings = ({ subsectionId }: Props) => { |
| 146 | + const { courseId } = useCourseAuthoringContext(); |
| 147 | + const { data: courseDetails } = useCourseDetails(courseId); |
| 148 | + const { data: itemData, isPending } = useCourseItemData(subsectionId); |
| 149 | + const { mutate } = useConfigureSubsection(); |
| 150 | + const { selectedContainerState } = useOutlineSidebarContext(); |
| 151 | + |
| 152 | + const onChange = (variables: Partial<ConfigureSubsectionData>) => { |
| 153 | + if (isPending || !itemData) { |
| 154 | + return; |
| 155 | + } |
| 156 | + return mutate({ |
| 157 | + itemId: subsectionId, |
| 158 | + sectionId: selectedContainerState?.sectionId, |
| 159 | + isVisibleToStaffOnly: itemData.visibilityState === VisibilityTypes.STAFF_ONLY, |
| 160 | + releaseDate: itemData.start, |
| 161 | + graderType: itemData.format == null ? 'notgraded' : itemData.format, |
| 162 | + dueDate: itemData.due == null ? '' : itemData.due, |
| 163 | + isTimeLimited: itemData.isTimeLimited, |
| 164 | + isProctoredExam: itemData.isProctoredExam, |
| 165 | + isOnboardingExam: itemData.isOnboardingExam, |
| 166 | + isPracticeExam: itemData.isPracticeExam, |
| 167 | + examReviewRules: itemData.examReviewRules, |
| 168 | + defaultTimeLimitMin: itemData.defaultTimeLimitMinutes, |
| 169 | + hideAfterDue: itemData.hideAfterDue === undefined ? false : itemData.hideAfterDue, |
| 170 | + showCorrectness: itemData.showCorrectness, |
| 171 | + isPrereq: itemData.isPrereq, |
| 172 | + prereqUsageKey: itemData.prereq, |
| 173 | + prereqMinScore: defaultPrereqScore(itemData.prereqMinScore), |
| 174 | + prereqMinCompletion: defaultPrereqScore(itemData.prereqMinCompletion), |
| 175 | + ...variables, |
| 176 | + }) |
| 177 | + } |
| 178 | + |
| 179 | + return ( |
| 180 | + <SidebarContent> |
| 181 | + { !courseDetails?.selfPaced && <ReleaseSection |
| 182 | + subsectionId={subsectionId} |
| 183 | + onChange={onChange} |
| 184 | + /> } |
| 185 | + <GradingSection |
| 186 | + subsectionId={subsectionId} |
| 187 | + onChange={onChange} |
| 188 | + /> |
| 189 | + </SidebarContent> |
| 190 | + ) |
| 191 | +} |
| 192 | + |
0 commit comments