All files / src/react-components/shared structured-data.js

100% Statements 20/20
90% Branches 9/10
100% Functions 1/1
100% Lines 20/20

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                                                                                                                              6x     6x 6x   1x       5x   6x   1x       5x 5x   6x                             6x   3x   3x   3x 3x 3x   6x   4x 4x           6x              
/*
 * 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';
 
// Very simplistic typing of the ld-json structure
type LDjsonTopKeys = "@context" | "@type" | "mainEntityOfPage" | "image" | "headline" | "author" | "isAccessibleForFree" | "publisher" | "hasPart";
type LDjsonSecondaryKeys = "@type" | "@id" | "name" | "logo" | "url" | "cssSelector" | "isAccessibleForFree";
type LDjsonStruct = {
    [LDjsonTopKeys]: string | {
        [LDjsonSecondaryKeys]: string | {
            [LDjsonSecondaryKeys]: string
        }
    }
};
 
/**
 * Props for StructuredData when type="auto"
 */
type StructuredDataAutoProps = {|
    walled: boolean,
    type: "auto"
|};
/**
 * Props for StructuredData when type="article"
 */
type StructuredDataArticleProps = {|
    walled: boolean,
    type: "article",
    title: string,
    author?: string,
    image?: string,
|};
/**
 * Props for StructuredData
 */
type StructuredDataProps = StructuredDataAutoProps | StructuredDataArticleProps;
 
/**
 * This renders structured data for this entry.
 */
class StructuredData extends React.PureComponent<StructuredDataProps>
{
    render (): React.Node // eslint-disable-line require-jsdoc
    {
        const { walled } = this.props;
        let image: string;
        let author: string;
        let authorType: "Person" | "Organization" = "Person";
        if(this.props.image)
        {
            image = this.props.image;
        }
        else
        {
            image = '/fbed.jpg';
        }
        if(this.props.author)
        {
            author = this.props.author;
        }
        else
        {
            author = 'Fagforbundet, yrkesseksjon helse & sosial';
            authorType = 'Organization';
        }
        const ld: LDjsonStruct = {
            "@context": "https://schema.org",
            "publisher": {
                "@type": "Organization",
                "name": "Fagforbundet",
                "logo": {
                    "@type": "ImageObject",
                    "url": location.origin+'/brand.png',
                }
            },
            "author": {
                "@type": authorType,
                "name": author
            },
        };
        if(this.props.type === "auto")
        {
            ld['@type'] = 'WebPage';
        }
        else Eif(this.props.type === "article")
        {
            ld.headline = this.props.title;
            ld.image = image;
            ld['@type'] = 'Article';
        }
        if(walled)
        {
            ld.isAccessibleForFree = "False";
            ld.hasPart = {
                "@type": "WebPageElement",
                "isAccessibleForFree": "False",
                "cssSelector" : ".auth-content"
            };
        }
        return <script type="application/ld+json">
            {JSON.stringify(ld)}
        </script>;
    }
}
 
export { StructuredData };