Skip to content

Commit c42e91a

Browse files
fix: enhance code structure and i18n support
1 parent 2b790bc commit c42e91a

File tree

7 files changed

+61
-41
lines changed

7 files changed

+61
-41
lines changed

src/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,9 @@ export const iframeMessageTypes = {
107107
xblockEvent: 'xblock-event',
108108
xblockScroll: 'xblock-scroll',
109109
};
110+
111+
export const BROKEN = 'broken';
112+
113+
export const LOCKED = 'locked';
114+
115+
export const MANUAL = 'manual';

src/optimizer-page/CourseOptimizerPage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import CourseStepper from '../generic/course-stepper';
1313
import ConnectionErrorAlert from '../generic/ConnectionErrorAlert';
1414
import AlertMessage from '../generic/alert-message';
1515
import { RequestFailureStatuses } from '../data/constants';
16+
import { RERUN_LINK_UPDATE_STATUSES } from './data/constants';
1617
import { STATEFUL_BUTTON_STATES } from '../constants';
1718
import messages from './messages';
1819
import {
@@ -45,7 +46,8 @@ export function pollRerunLinkUpdateDuringUpdate(
4546
courseId: string,
4647
) {
4748
const shouldPoll = rerunLinkUpdateInProgress === true
48-
|| (rerunLinkUpdateResult && rerunLinkUpdateResult.status && rerunLinkUpdateResult.status !== 'Succeeded');
49+
|| (rerunLinkUpdateResult && rerunLinkUpdateResult.status
50+
&& rerunLinkUpdateResult.status !== RERUN_LINK_UPDATE_STATUSES.SUCCEEDED);
4951

5052
if (shouldPoll) {
5153
clearInterval(interval.current as number | undefined);

src/optimizer-page/messages.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ const messages = defineMessages({
6161
id: 'course-authoring.course-optimizer.scanHeader',
6262
defaultMessage: 'Scan results',
6363
},
64+
updateButton: {
65+
id: 'course-authoring.scanResults.updateButton',
66+
defaultMessage: 'Update',
67+
},
68+
updated: {
69+
id: 'course-authoring.scanResults.updated',
70+
defaultMessage: 'Updated',
71+
},
6472
});
6573

6674
export default messages;

src/optimizer-page/scan-results/BrokenLinkTable.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Card, Icon, DataTable, StatefulButton, Spinner,
33
} from '@openedx/paragon';
4+
import { useIntl } from '@edx/frontend-platform/i18n';
45
import {
56
ArrowForwardIos,
67
LinkOff,
@@ -12,7 +13,9 @@ import messages from './messages';
1213
import CustomIcon from './CustomIcon';
1314
import lockedIcon from './lockedIcon';
1415
import ManualIcon from './manualIcon';
15-
import { STATEFUL_BUTTON_STATES } from '../../constants';
16+
import {
17+
STATEFUL_BUTTON_STATES, BROKEN, LOCKED, MANUAL,
18+
} from '../../constants';
1619

1720
const BrokenLinkHref: FC<{ href: string }> = ({ href }) => {
1821
const handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
@@ -29,7 +32,7 @@ const BrokenLinkHref: FC<{ href: string }> = ({ href }) => {
2932
);
3033
};
3134

32-
const GoToBlock: FC<{ block: { url: string, displayName: string } }> = ({ block }) => {
35+
const GoToBlock: FC<{ block: { url: string, displayName?: string } }> = ({ block }) => {
3336
const handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
3437
event.preventDefault();
3538
window.open(block.url, '_blank');
@@ -87,6 +90,7 @@ const LinksCol: FC<{
8790
updatedLinkMap,
8891
updatedLinkInProgress,
8992
}) => {
93+
const intl = useIntl();
9094
const handleUpdate = () => {
9195
if (onUpdate) {
9296
onUpdate(originalLink || href, block.id || block.url, sectionId);
@@ -119,13 +123,16 @@ const LinksCol: FC<{
119123
<span
120124
className="updated-link-text d-flex align-items-center text-success"
121125
>
122-
Updated
126+
{intl.formatMessage(messages.updated)}
123127
<Icon src={Check} className="text-success" />
124128
</span>
125129
) : (
126130
<StatefulButton
127131
className="px-4 rounded-0 update-link-btn"
128-
labels={{ default: 'Update', pending: 'Update' }}
132+
labels={{
133+
default: intl.formatMessage(messages.updateButton),
134+
pending: intl.formatMessage(messages.updateButton),
135+
}}
129136
icons={{ default: '', pending: <Spinner animation="border" size="sm" className="mr-2 spinner-icon" /> }}
130137
state={isUpdating ? STATEFUL_BUTTON_STATES.pending : STATEFUL_BUTTON_STATES.default}
131138
onClick={handleUpdate}
@@ -160,7 +167,7 @@ type TableData = {
160167
const BrokenLinkTable: FC<BrokenLinkTableProps> = ({
161168
unit,
162169
filters,
163-
linkType = 'broken',
170+
linkType = BROKEN,
164171
onUpdateLink,
165172
sectionId,
166173
updatedLinks = [],
@@ -225,7 +232,7 @@ const BrokenLinkTable: FC<BrokenLinkTableProps> = ({
225232
<LinksCol
226233
block={{ url: block.url, displayName: block.displayName || 'Go to block' }}
227234
href={link}
228-
linkType="broken"
235+
linkType={BROKEN}
229236
/>
230237
),
231238
}));
@@ -241,7 +248,7 @@ const BrokenLinkTable: FC<BrokenLinkTableProps> = ({
241248
<LinksCol
242249
block={{ url: block.url, displayName: block.displayName || 'Go to block' }}
243250
href={link}
244-
linkType="locked"
251+
linkType={LOCKED}
245252
/>
246253
),
247254
}));
@@ -258,7 +265,7 @@ const BrokenLinkTable: FC<BrokenLinkTableProps> = ({
258265
<LinksCol
259266
block={{ url: block.url, displayName: block.displayName || 'Go to block' }}
260267
href={link}
261-
linkType="manual"
268+
linkType={MANUAL}
262269
/>
263270
),
264271
}));

src/optimizer-page/scan-results/ScanResults.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
}
130130

131131
.links-container > div:first-child {
132-
max-width: 95%;
132+
max-width: 85%;
133133
}
134134

135135
.locked-links-checkbox {

src/optimizer-page/scan-results/ScanResults.tsx

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -196,38 +196,27 @@ const ScanResults: FC<Props> = ({
196196
const successfulLinkIds: string[] = [];
197197
const newMap: Record<string, string> = {};
198198

199-
const blocksWithResults = new Set();
200-
if (response.results && Array.isArray(response.results)) {
199+
const typeToSection: Record<string, string> = {
200+
course_updates: 'course-updates',
201+
custom_pages: 'custom-pages',
202+
};
203+
204+
const blocksWithResults = new Set<string>();
205+
206+
const addBlocksWithPrevLinks = (sectionId: string) => {
207+
const section = allSections.find(s => s.id === sectionId);
208+
if (!section) { return; }
209+
section.subsections.forEach(sub => sub.units.forEach(unit => unit.blocks.forEach(b => {
210+
if (b.previousRunLinks?.length) { blocksWithResults.add(b.id); }
211+
})));
212+
};
213+
214+
if (Array.isArray(response.results)) {
201215
response.results.forEach((result) => {
202-
if (result.type === 'course_updates') {
203-
allSections.forEach(section => {
204-
if (section.id === 'course-updates') {
205-
section.subsections.forEach(subsection => {
206-
subsection.units.forEach(unit => {
207-
unit.blocks.forEach(block => {
208-
if (block.previousRunLinks && block.previousRunLinks.length > 0) {
209-
blocksWithResults.add(block.id);
210-
}
211-
});
212-
});
213-
});
214-
}
215-
});
216-
} else if (result.type === 'custom_pages') {
217-
allSections.forEach(section => {
218-
if (section.id === 'custom-pages') {
219-
section.subsections.forEach(subsection => {
220-
subsection.units.forEach(unit => {
221-
unit.blocks.forEach(block => {
222-
if (block.previousRunLinks && block.previousRunLinks.length > 0) {
223-
blocksWithResults.add(block.id);
224-
}
225-
});
226-
});
227-
});
228-
}
229-
});
230-
} else {
216+
const sectionId = typeToSection[result.type];
217+
if (sectionId) {
218+
addBlocksWithPrevLinks(sectionId);
219+
} else if (result.id) {
231220
blocksWithResults.add(result.id);
232221
}
233222
});

src/optimizer-page/scan-results/messages.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ const messages = defineMessages({
7474
id: 'course-authoring.course-optimizer.updateLinksError',
7575
defaultMessage: 'Some links couldn\'t be updated.',
7676
},
77+
updateButton: {
78+
id: 'course-authoring.scanResults.updateButton',
79+
defaultMessage: 'Update',
80+
},
81+
updated: {
82+
id: 'course-authoring.scanResults.updated',
83+
defaultMessage: 'Updated',
84+
},
7785
});
7886

7987
export default messages;

0 commit comments

Comments
 (0)