Projekt

Obecné

Profil

Stáhnout (2.51 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { useEffect, useState } from "react"
2
import { ScrollView, Text, VStack } from "native-base"
3
import { useSelector } from "react-redux"
4
import { RootState } from "../../stores/store"
5
import ItemPreview from "./ItemPreview"
6
import { ItemPreviewType } from "../../types/listViewTypes"
7
import { log } from "../../logging/logger"
8

    
9
type ListViewProps = {
10
    navigation: any
11
}
12

    
13
const ListView = (props: ListViewProps) => {
14
    const items = useSelector((state: RootState) => state.listView.data)
15

    
16
    /**
17
     * Searches through names by given role, return getty_data.display_name if exists otherwise name.value
18
     * @param item
19
     * @param role
20
     */
21
    const parseNameByRole = (item: ItemPreviewType, role: string) => {
22
        // @ts-ignore
23
        const name = item.object[0].name?.find((name) => name.role === role)
24
        return name && name ?
25
            name.getty_data ?
26
                name.getty_data.display_name : name.value
27
            : ""
28
    }
29

    
30
    /**
31
     * Returns artist if it is original, otherwise returns copist and original artist in copy of
32
     * @param item
33
     */
34
    const parseArtists = (item: ItemPreviewType) => {
35
        // if is copy - display copist and copy of original artist if they are present in data
36
        if (item.src?.value && item.src?.value == "copy") {
37
            const copist = parseNameByRole(item, "copist")
38
            const artist = parseNameByRole(item, "artist")
39
            return `${ copist } ${ artist !== "" ? `(copy of ${ artist })` : "" }`
40
        } else {
41
            // if is original - display artist
42
            return parseNameByRole(item, "artist")
43
        }
44
    }
45

    
46
    /**
47
     * Parses image name
48
     * cycles through all objects and returns first image name with changed suffix to .png of undefined if not found
49
     * @param item
50
     */
51
    const parseImage = (item: ItemPreviewType) => {
52
        // @ts-ignore
53
        const image = item.object.find((object) => object.images)
54
        // @ts-ignore
55
        return image && image.images ? image.images[0].file.split('.')[0] + '.png' : undefined
56
    }
57

    
58
    return (
59
        <>
60
            { items.map((item) => (
61
                <ItemPreview
62
                    // @ts-ignore
63
                    caption={ item.object[0].caption }
64
                    title={ item.text }
65
                    name={ parseArtists(item) }
66
                    image={ parseImage(item) }
67
                    itemId={ item.xml_id }
68
                    navigation={ props.navigation }
69
                />
70
            )) }
71
        </>
72
    )
73
}
74

    
75
export default ListView
(2-2/2)