Skip to content

Commit f796a1d

Browse files
committed
fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! feat: Syllabus page for learner MFE
1 parent 1170669 commit f796a1d

File tree

1 file changed

+62
-51
lines changed

1 file changed

+62
-51
lines changed

src/components/PrintSyllabus.tsx

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import * as React from 'react';
2-
import { useRef } from 'react';
2+
import { useRef, useState } from 'react';
33
import { useIntl } from '@edx/frontend-platform/i18n';
44
import { Button } from '@openedx/paragon';
55
import messages from '../messages';
66
import { type BlockResponse } from '../types';
77

8-
export const PrintSyllabus = ({ blockData }: { blockData: BlockResponse }) => {
9-
const intl = useIntl();
10-
const iframeRef = useRef<HTMLIFrameElement | null>(null);
11-
const blocks = blockData?.blocks;
12-
const rootBlock = blockData?.blocks[blockData.root];
8+
const buildSyllabusForPrint = (intl, blocks, rootBlock) => {
139
const makeList = (items: string[] | null) => {
1410
if (!items) {
1511
return '';
@@ -27,56 +23,71 @@ export const PrintSyllabus = ({ blockData }: { blockData: BlockResponse }) => {
2723
)))}`))}`))}`));
2824

2925
const srcdoc = `<html>
30-
<head>
31-
<title>${intl.formatMessage(messages.syllabusTitle)}</title>
32-
<style>
33-
body > ul {
34-
padding: 0.125rem;
35-
margin: 0;
36-
list-style: none;
37-
}
26+
<head>
27+
<title>${intl.formatMessage(messages.syllabusTitle)}</title>
28+
<style>
29+
body > ul {
30+
padding: 0.125rem;
31+
margin: 0;
32+
list-style: none;
33+
}
34+
35+
body > ul > li {
36+
border: 1px solid black;
37+
margin: 0.5rem 0;
38+
padding: 0 0.5rem;
39+
}
40+
41+
body > ul > li > ul {
42+
break-after: auto;
43+
list-style: none;
44+
}
45+
46+
body ul ul ul ul li:before {
47+
content: '\\01F517';
48+
margin-right: 0.5rem;
49+
}
3850
39-
body > ul > li {
40-
border: 1px solid black;
41-
margin: 0.5rem 0;
42-
padding: 0 0.5rem;
43-
}
51+
body ul ul ul ul {
52+
list-style: none;
53+
padding-left: 0;
54+
}
4455
45-
body > ul > li > ul {
46-
break-after: auto;
47-
list-style: none;
48-
}
56+
body ul > li ul {
57+
break-after: auto;
58+
}
4959
50-
body ul ul ul ul li:before {
51-
content: '\\01F517';
52-
margin-right: 0.5rem;
53-
}
54-
55-
body ul ul ul ul {
56-
list-style: none;
57-
padding-left: 0;
58-
}
60+
li {
61+
break-inside: avoid;
62+
}
5963
60-
body ul > li ul {
61-
break-after: auto;
62-
}
64+
ul {
65+
margin: 0;
66+
padding-left: 1rem;
67+
break-after: auto;
68+
}
69+
</style>
70+
</head>
71+
<body>
72+
${syllabusList}
73+
</body>
74+
</html>
75+
`;
76+
return srcdoc;
77+
};
78+
79+
export const PrintSyllabus = ({ blockData }: { blockData: BlockResponse }) => {
80+
const intl = useIntl();
81+
const iframeRef = useRef<HTMLIFrameElement | null>(null);
82+
const [srcdoc, setSrcdoc] = useState('');
6383

64-
li {
65-
break-inside: avoid;
66-
}
84+
const printSyllabus = () => {
85+
const blocks = blockData?.blocks;
86+
const rootBlock = blockData?.blocks[blockData.root];
87+
setSrcdoc(buildSyllabusForPrint(intl, blocks, rootBlock));
88+
iframeRef.current.contentWindow.print();
89+
};
6790

68-
ul {
69-
margin: 0;
70-
padding-left: 1rem;
71-
break-after: auto;
72-
}
73-
</style>
74-
</head>
75-
<body>
76-
${syllabusList}
77-
</body>
78-
</html>
79-
`;
8091
return (
8192
<div className="d-flex justify-content-end my-2">
8293
<iframe
@@ -88,7 +99,7 @@ ${syllabusList}
8899
{iframeRef.current?.contentWindow && (
89100
<Button
90101
variant="outline-primary"
91-
onClick={() => iframeRef.current.contentWindow.print()}
102+
onClick={() => printSyllabus()}
92103
>
93104
{intl.formatMessage(messages.print)}
94105
</Button>

0 commit comments

Comments
 (0)