All files / src/react-components tools.js

100% Statements 21/21
100% Branches 6/6
100% Functions 4/4
100% Lines 21/21

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                                                                                                      16x 16x       16x   3x   13x                           1x 1x   3x 3x   6x       3x           1x                               5x 5x 5x   1x     5x                                           9x   9x   1x   9x                          
/*
 * Part of Pleiar.no - a collection of tools for nurses
 *
 * Copyright (C) Fagforbundet 2019
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
 
// @flow
 
import * as React from 'react';
import { MainContainer, PageTitle } from './layout';
import { Row, Col } from 'reactstrap';
import NutriCalc from './nutricalc';
import MedCalc from './medcalc';
import NEWS from './news';
import MedicationSynonyms from './med-synonyms';
import { Handle404 } from './shared';
import { Switch, Route } from 'react-router-dom';
import { withRouter } from 'react-router';
import { Link } from 'react-router-dom';
import ToolsList from '../tools.list.js';
import type { Match as RouterMatch } from 'react-router-dom';
import type { ToolListEntry } from '../types/data';
import { RequiresAuth } from './auth';
import SimpleAccordion from './shared/simple-accordion';
 
/**
 * This is the container each tool should wrap itself in. It functions like
 * `<MainContainer>`, but this expects a `<MainContainer>` to exist as its
 * parent already, so it won't do much other than return a div with a class set
 * and a navigation link. It will also wrap it in `<RequiresAuth>` unless
 * you pass requiresAuth={false}.
 */
class ToolContainer extends React.PureComponent<{| tool: string, requiresAuth?: boolean, children: React.Node |}>
{
    // eslint-disable-next-line require-jsdoc
    render(): React.Node
    {
        const { requiresAuth } = this.props;
        const wrapper = <div className={"app-"+this.props.tool}>
                <Link className="d-block tool-back-link" to='/verktoy'>« Tilbake til verktøylista</Link>
                {this.props.children}
            </div>;
        if (requiresAuth === false)
        {
            return wrapper;
        }
        return <RequiresAuth>
            {wrapper}
        </RequiresAuth>;
    }
}
 
/**
 * This builds the graphical index of tools
 */
class ToolsIndex extends React.PureComponent<{||}>
{
    // eslint-disable-next-line require-jsdoc
    render(): React.Node
    {
        const tools = [];
        for(const category of ToolsList)
        {
            const entries = [];
            for(const entry of category.tools)
            {
                entries.push(
                    <li key={entry.path}><Link to={"/verktoy"+entry.path}>{entry.title}</Link></li>
                );
            }
            tools.push({
                header: category.title,
                render: <ul>{entries}</ul>
            });
        }
 
        return <div>
                <h1>Verktøy</h1>
                <PageTitle title="Verktøy" />
                <SimpleAccordion elements={tools} alwaysOpen={true} />
            </div>;
    }
}
 
/**
 * Toplevel handler and router of everything under /verktoy
 */
class ToolsSection extends React.PureComponent<{|match: RouterMatch, location: Location|}>
{
    // eslint-disable-next-line require-jsdoc
    render(): React.Node
    {
        const { match, location } = this.props;
        let small: boolean = false;
        if(match.path === location.pathname)
        {
            small = true;
        }
 
        return <MainContainer app="tools" small={small}>
            <Switch>
                <Route path={match.path} exact component={ToolsIndex} />
                <Route path={match.path+'/ernaering'} component={NutriCalc} />
                <Route path={match.path+'/medikamentregning'} component={MedCalc} />
                <Route path={match.path+'/news'} component={NEWS} />
                <Route path={match.path+'/medikament/synonymer'} component={MedicationSynonyms} />
                <Handle404 />
            </Switch>
        </MainContainer>;
    }
}
 
/**
 * Component that renders an entry from our ToolsList as a search result
 * for global search. Used by {@link WhateverRenderer}
 */
class ToolsSearchRenderer extends React.PureComponent<{| entry: ToolListEntry |}>
{
    // eslint-disable-next-line require-jsdoc
    render(): React.Node
    {
        const { entry } = this.props;
        let description: ?string;
        if (entry.description)
        {
            description = entry.description;
        }
        return <Row className="toolEntry">
            <Col>
                <Link to={"/verktoy"+entry.path} className="linkWrapper">
                    <div className="toolSearchTitle likeLink" target="_blank">Verktøy: {entry.title}</div>
                    {description}
                </Link>
            </Col>
        </Row>;
    }
}
 
export default (withRouter(ToolsSection): React.AbstractComponent<{| |}>);
export { ToolsIndex, ToolContainer, ToolsSearchRenderer };