|
1 | 1 | import { React, useState, observer } from 'vendor' |
2 | 2 | import { Button } from 'react-bootstrap' |
3 | | -import { map, filter, findIndex, every, sumBy } from 'lodash' |
| 3 | +import { map, filter, every, sumBy } from 'lodash' |
4 | 4 | import styled from 'styled-components' |
5 | 5 | import { colors } from '../../../theme' |
6 | 6 | import { Icon } from 'shared' |
7 | 7 | import { ID } from '../../../models/types' |
8 | | -import { currentCourses } from '../../../models' |
| 8 | +import { currentCourses, currentOfferings } from '../../../models' |
9 | 9 | import OfferingBlock from './offering-block' |
10 | 10 | import AddSubjectDropdown from './add-subject-dropdown' |
11 | 11 | import { DeleteOfferingModal, DeleteOfferingWarningModal } from './delete-offering-modal' |
@@ -130,22 +130,19 @@ export const MyCoursesDashboard = observer(() => { |
130 | 130 |
|
131 | 131 | // delete offering |
132 | 132 | const deleteOffering = () => { |
133 | | - const tempDisplayedOfferingIds = [...displayedOfferingIds] |
134 | | - const index = findIndex(tempDisplayedOfferingIds, id => id === deleteOfferingIdModal) |
135 | | - if (index >= 0 && deleteOfferingIdModal) { |
136 | | - const offeringCourses = filter(courses.array, c => c.offering_id === deleteOfferingIdModal && !c.is_preview) |
137 | | - Promise.all(map(offeringCourses, async c => { |
138 | | - return c.teachers.current?.drop() || Promise.resolve() |
139 | | - })).then(() => { |
140 | | - tempDisplayedOfferingIds.splice(index, 1) |
141 | | - setDisplayedOfferingIds(tempDisplayedOfferingIds) |
142 | | - setDeleteOfferingIdModal(null) |
143 | | - }) |
144 | | - } |
| 133 | + if (!deleteOfferingIdModal) return |
| 134 | + const offering = currentOfferings.get(deleteOfferingIdModal) |
| 135 | + if (!offering) return |
| 136 | + Promise.all(courses.forOffering(offering).array.map(c => { |
| 137 | + return c.teachers.current?.drop() || Promise.resolve() |
| 138 | + })).then(() => { |
| 139 | + setDisplayedOfferingIds(displayedOfferingIds.filter(id => id != offering.id)) |
| 140 | + setDeleteOfferingIdModal(null) |
| 141 | + }) |
145 | 142 | } |
146 | 143 |
|
147 | 144 | const renderDeleteModal = () => { |
148 | | - const offeringCourses = filter(courses.array, c => c.offering_id === deleteOfferingIdModal) |
| 145 | + const offeringCourses = courses.array.filter(c => c.offering_id == deleteOfferingIdModal) |
149 | 146 | const areAllPreviewCourses = every(offeringCourses, oc => oc.is_preview) |
150 | 147 | const areAllNonPreviewCoursesWithoutStudents = every(filter(offeringCourses, c => String(c.term) !== 'preview'), oc => sumBy(oc?.periods, (p:any) => { |
151 | 148 | return p.num_enrolled_students |
|
0 commit comments