All files / src/react-components elearn.js

92.5% Statements 74/80
85.71% Branches 30/35
93.75% Functions 15/16
93.67% Lines 74/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369                                                                                            4x         4x   4x 4x 4x 3x 3x               4x                                                   20x         24x 24x 1x   23x 4x   23x 23x         14x                   23x         23x 23x 23x 7x   23x 19x 19x 79x       79x   79x 5x 5x 5x 5x                             79x           4x             19x   23x                                   5x     5x                                                                     8x 8x 9x         17x 17x 2x   15x                                 5x   5x 5x 4x 4x     4x 1x 1x   3x     1x   5x                                     2x 2x 2x         2x                             2x 2x 2x 2x 2x 2x 2x       2x   2x   2x   2x                                                 2x     2x 1x                  
/**
 * @flow
 * @prettier
 */
import * as React from "react";
import {
    Card,
    CardTitle,
    CardText,
    CardBody,
    CardSubtitle,
    Col,
    Container,
    Badge,
    Row,
    ListGroup,
    ListGroupItem,
    Alert,
    Button,
} from "reactstrap";
import quizManagerHandler, {
    quizManagerQuestion,
    quizDataManager,
} from "../quiz-manager";
import { Markdown } from "./markdown";
import { NotFound, ConditionalRender } from "./shared";
import { MainContainer, PageTitle } from "./layout";
import { MenuListRenderer } from "./menu";
import device from "../helper.device";
import type { appList } from "./menu";
 
// Let flow know about our GIT_REVISION and PLEIAR_ENV globals (from webpack)
declare var PLEIAR_ENV: string;
 
type QuestionProps = {|
    question: quizManagerQuestion | null,
|};
 
/**
 * Displays the score of a completed quiz
 */
class CourseCompleted extends React.Component<{|
    handler: quizManagerHandler,
|}> {
    // eslint-disable-next-line require-jsdoc
    constructor() {
        super();
    }
 
    // eslint-disable-next-line require-jsdoc
    render(): React.Node {
        const { handler } = this.props;
        let content: ?React.Node;
        const endSlide = handler.getEndSlide();
        let header: string = "Fullført og bestått!";
        if (endSlide !== null) {
            header = endSlide.header;
            content = (
                <Markdown
                    key="markdown-endslide"
                    content={endSlide.markdown}
                    permitSpecialNodes={true}
                />
            );
        }
        return (
            <Container>
                <Row>
                    <Col>
                        <Alert color="success">
                            <h1 className="alert-header">{header}</h1>
                            <Row>
                                <Col>{content}</Col>
                            </Row>
                        </Alert>
                    </Col>
                </Row>
            </Container>
        );
    }
}
 
/**
 * Renders a single question
 */
class Question extends React.Component<QuestionProps> {
    /**
     * Scroll ourself into view whenever a new Question is mounted (which in
     * this case means whenever a new question is displayed)
     */
    componentDidMount() {
        window.scrollTo(0, 0);
    }
 
    // eslint-disable-next-line require-jsdoc
    render(): React.Node {
        const { question } = this.props;
        if (question === null) {
            return null;
        }
        const pushAnswer = (answer) => {
            question.answer(answer);
        };
        const content: Array<React.Node> = [];
        if (
            question.markdown !== undefined &&
            question.markdown !== null &&
            typeof question.markdown === "string"
        ) {
            content.push(
                <Markdown
                    key="markdown-content"
                    content={question.markdown}
                    permitSpecialNodes={true}
                />,
            );
        }
 
        // $FlowExpectedError[incompatible-type] - flow for some reason thinks something here is a string
        const percentageCompleted: number = (
            ((question.questionNumber - 1) / question.totalQuestions) *
            100
        ).toPrecision(2);
        let listGroup: ?React.Node;
        let id: number = 0;
        let continueIsDisabled: boolean = true;
        if (question.splashSlide === true || question.correct === true) {
            continueIsDisabled = false;
        }
        if (question.splashSlide !== true) {
            const options = [];
            for (const possible of question.possibleAnswers) {
                id++;
 
                let feedback: ?React.Node;
                let color: string;
                let className: string = "force-pointer";
 
                if (question.hasAnswered(possible)) {
                    className = "";
                    Eif (possible.correct === true) {
                        color = "success";
                        feedback = (
                            <Badge key="correct" pill>
                                Rett!
                            </Badge>
                        );
                    } else {
                        color = "danger";
                        feedback = (
                            <Badge key="wrong" pill>
                                Feil!
                            </Badge>
                        );
                    }
                }
 
                options.push(
                    <ListGroupItem
                        key={"lgi" + id}
                        data-question-id={id - 1}
                        color={color}
                        action
                        onClick={() => pushAnswer(possible)}
                        className={className}
                    >
                        {possible.entry} {feedback}
                    </ListGroupItem>,
                );
            }
            listGroup = <ListGroup>{options}</ListGroup>;
        }
        return (
            <Container>
                <Row>
                    <Col>
                        <Card className="badgeNoBold">
                            <CardBody>
                                <CardSubtitle tag="h6" className="text-muted">
                                    {percentageCompleted}% fullført
                                </CardSubtitle>
                                <CardTitle tag="h1">
                                    {question.question}
                                </CardTitle>
                                <CardText tag="div">{content}</CardText>
                                {listGroup}
                                <CardText tag="div">
                                    <div className="mt-2">
                                        <Button
                                            onClick={() => {
                                                Eif (
                                                    continueIsDisabled === false
                                                ) {
                                                    question.handler.next();
                                                }
                                            }}
                                            className="mt-2"
                                            disabled={continueIsDisabled}
                                            outline
                                            color={
                                                continueIsDisabled === true
                                                    ? "secondary"
                                                    : "success"
                                            }
                                        >
                                            Fortsett
                                        </Button>
                                    </div>
                                </CardText>
                            </CardBody>
                        </Card>
                    </Col>
                </Row>
            </Container>
        );
    }
}
 
type QuizProps = {|
    handler: quizManagerHandler,
|};
 
/**
 * The Quiz base class that renders a single quiz
 */
class Quiz extends React.Component<QuizProps> {
    // eslint-disable-next-line require-jsdoc
    constructor(props: QuizProps) {
        super();
        props.handler.listen(() => {
            this.forceUpdate();
        });
    }
    // eslint-disable-next-line require-jsdoc
    render(): React.Node {
        const { handler } = this.props;
        if (handler.isDone()) {
            return <CourseCompleted handler={handler} />;
        }
        return (
            <Question
                question={this.props.handler.current}
                key={"question" + this.props.handler.currentID}
            />
        );
    }
}
 
/**
 * Root of the about component.
 */
class ELearnRaw extends React.Component<{|
    match: {| params: {| learningSet?: string |} |},
|}> {
    // eslint-disable-next-line require-jsdoc
    render(): React.Node {
        const match = this.props.match;
        let subComponent: React.Node;
        let title: string = "E-læring";
        if (match.params.learningSet) {
            const entry = new quizManagerHandler(match.params.learningSet);
            Iif (PLEIAR_ENV === "development") {
                window.PleiarActiveQuizManager = entry;
            }
            if (entry.exists) {
                title += " - " + entry.title;
                subComponent = <Quiz handler={entry} />;
            } else {
                subComponent = <NotFound root={false} />;
            }
        } else {
            subComponent = <ELearnList />;
        }
        return (
            <MainContainer app="elearning">
                <Row>
                    <Col>
                        <PageTitle title={title} />
                        {subComponent}
                    </Col>
                </Row>
            </MainContainer>
        );
    }
}
 
/**
 * Builds a menu of all elearn entries
 */
class ELearnList extends React.PureComponent<{||}> {
    // eslint-disable-next-line require-jsdoc
    render(): React.Node {
        let apps: appList = [];
        for (const name of Object.keys(quizDataManager.data()).sort()) {
            apps.push({
                name: quizDataManager.data()[name].title,
                id: "elaering/" + name,
            });
        }
        apps = apps.sort((a, b) => a.name.localeCompare(b.name));
        // On non-mobile devices the list will be displayed in two columns, so we split it
        // into two columns so that it becomes:
        // A   B
        // C   D
        //
        // and not
        //
        // A   C
        // B   D
        //
        // On mobile it is single-column, so the original sort order is correct.
        //
        // This will on some devices probably do the wrong thing, but the menu will
        // work just fine either way, it just won't be perfectly alphabetical.
        Eif (!device.isMobileDevice()) {
            const appListOne: appList = [];
            const appListTwo: appList = [];
            let last: number = -1;
            for (const app of apps) {
                let addTo: appList = appListOne;
                Iif (last === 1) {
                    addTo = appListTwo;
                    last = 2;
                } else {
                    last = 1;
                }
                addTo.push(app);
            }
            apps = [].concat(appListOne, appListTwo);
        }
        return (
            <>
                <div className="col-lg-10 offset-lg-1">
                    <h1>E-læringskurs</h1>
                    <div className="mb-4">
                        Her finner du korte nettkurs produsert for medlemmer i
                        Fagforbundet. Kursene passer godt for deg som vil lære
                        noe nytt, eller friske opp kunnskapene i et tema du
                        kjenner til fra før. Alle kursene er gratis.
                    </div>
                    <MenuListRenderer apps={apps} />
                </div>
            </>
        );
    }
}
 
/**
 * Root that loads our data
 */
class ELearn extends React.Component<{|
    match: {| params: {| learningSet?: string |} |},
|}> {
    // eslint-disable-next-line require-jsdoc
    render(): React.Node {
        return (
            <ConditionalRender
                component={ELearnRaw}
                test={() => quizDataManager.hasInitialized()}
                resolve={() => quizDataManager.initialize()}
                {...this.props}
            />
        );
    }
}
 
export default ELearn;
export { CourseCompleted, Question, Quiz, ELearn, ELearnRaw, ELearnList };