Projekt

Obecné

Profil

Stáhnout (9.76 KB) Statistiky
| Větev: | Tag: | Revize:
1 d3c12593 Fantič
import { Center, Box, VStack, Button, HStack, Text, Image, ScrollView, View, Spacer, Card, Heading, Switch, Flex } from "native-base"
2 56626a63 Fantič
import React, { useCallback, useEffect, useState } from "react"
3 b88520f8 Fantič
import { Item } from "../../types/item"
4
import { Note } from "../../types/note"
5 a7a9a204 Fantič
import LoadingBox from "../loading/LoadingBox"
6 cb25df10 Fantič
import NotesListView from "../notes/NotesListView"
7 b225ffee Fantič
import { IMAGE_URL } from "../../api/constants"
8 d3c12593 Fantič
import { log } from "../../logging/logger"
9 2135f53d Fantič
import { createNote, getAllNotes } from "../../stores/actions/notesThunks"
10
import { AppDispatch, RootState } from "../../stores/store"
11
import { useDispatch, useSelector } from "react-redux"
12 d3c12593 Fantič
import { getItemNotes } from "../../stores/actions/itemThunks"
13 b88520f8 Fantič
14 293f99f8 Fantič
import { Dimensions } from "react-native";
15
16 b88520f8 Fantič
interface ItemDetailProps {
17
    item: Item,
18 852de08e Fantič
    itemLoading: boolean,
19
    notes: Note[],
20 72afb023 Fantič
    notesLoading: boolean,
21
    navigation: any
22 b88520f8 Fantič
}
23
24 d3c12593 Fantič
const ItemDetail = (props: { item: Item, itemLoading: boolean }) => {
25 b88520f8 Fantič
    const item = props.item;
26 852de08e Fantič
27 72afb023 Fantič
    return (<>
28
        {
29 d3c12593 Fantič
            item && props.itemLoading ?
30
                (<LoadingBox text="Item loading..." />)
31
                :
32
                (
33 2135f53d Fantič
                    <ScrollView flex="1">
34
                        <VStack>
35
                            <HStack marginTop="5%">
36
                                <VStack width="75%">
37
                                    <Text>
38
                                        {item.authorDisplayName ? item.authorDisplayName : item.authorName}
39 72afb023 Fantič
                                    </Text>
40 2135f53d Fantič
                                    <Heading size="sm">
41
                                        {item.workName}
42
                                    </Heading>
43
44
                                </VStack>
45
                                <Box width="25%">
46
                                    <Button variant="outline" size="md">
47
                                        {item.concordances[0].id}
48
                                    </Button>
49
                                </Box>
50
                            </HStack>
51
                            <Card shadow="1" marginTop="5%">
52
                                {item.inventoryItem &&
53
                                    <VStack>
54
                                        <Text italic>
55
                                            <Text color="light.500">Inventory item: </Text>
56
                                            {item.inventoryItem}
57 d3c12593 Fantič
                                        </Text>
58
                                        <Text italic marginTop="2.5%">
59 2135f53d Fantič
                                            {item.searchSubjects.map((subject, index) => (
60
                                                index < item.searchSubjects.length - 1 ? (subject + ", ") : (subject)
61
                                            ))}
62 d3c12593 Fantič
                                        </Text>
63 2135f53d Fantič
                                    </VStack>}
64
                            </Card>
65
                            {item.fullView && item.imageUrl && (
66
                                <Center background="primary.100" marginTop="5%">
67
                                    <Text>Resembling object</Text>
68
                                    <Image size={250} alt="image" source={{ uri: IMAGE_URL + "/" + item.imageUrl }} />
69
                                </Center>)}
70
                            {item.fullView && (
71
                                <Card shadow="1" marginTop="5%" marginBottom="5%">
72
                                    {item.authorName &&
73
                                        <HStack>
74
                                            <Text italic marginTop="2.5%">
75
                                                <Text color="light.500">Author name: </Text>
76
                                                {item.authorName}
77
                                            </Text>
78
                                        </HStack>}
79
                                    {item.title &&
80
                                        <HStack>
81
                                            <Text italic marginTop="2.5%">
82
                                                <Text color="light.500">Title: </Text>
83
                                                {item.title}
84
                                            </Text>
85
                                        </HStack>}
86
                                    {item.institution &&
87
                                        <HStack>
88
                                            <Text italic marginTop="2.5%">
89
                                                <Text color="light.500">Institution: </Text>
90
                                                {item.institution.name}, Inv. No. {item.institution.inventoryNumber}, {item.institution.city} {"(" + item.institution.country + ")"}
91
                                            </Text>
92
                                        </HStack>}
93
                                    {item.repository &&
94
                                        <HStack>
95
                                            <Text italic marginTop="2.5%">
96
                                                <Text color="light.500">Repository: </Text>
97
                                                {item.repository}
98
                                            </Text>
99
                                        </HStack>}
100
                                    {item.provenance &&
101
                                        <HStack>
102
                                            <Text italic marginTop="2.5%">
103
                                                <Text color="light.500">Provenance: </Text>
104
                                                {item.provenance}
105
                                            </Text>
106
                                        </HStack>}
107
                                    {item.description &&
108
                                        <HStack>
109
                                            <Text italic marginTop="2.5%">
110
                                                <Text color="light.500">Description: </Text>
111
                                                {item.description}
112
                                            </Text>
113
                                        </HStack>}
114
                                </Card>)}
115
                        </VStack>
116
                    </ScrollView>
117 d3c12593 Fantič
                )
118 72afb023 Fantič
        }</>
119 b88520f8 Fantič
    );
120
}
121
122
123
124 d3c12593 Fantič
const ItemNotes = (props: { item: Item, notes: Note[], notesLoading: boolean, navigation: any }) => {
125
126
    const [showRelatedComments, setShowRelatedComments] = useState(true);
127
128 1853180f Fantič
    const { requestPending, triggerRefresh } = useSelector((state: RootState) => state.noteViewState)
129 2135f53d Fantič
130 d3c12593 Fantič
    const dispatch = useDispatch<AppDispatch>();
131
132 1853180f Fantič
    // trigger refresh on triggerRefresh increment
133
    useEffect(() => {
134
        if(triggerRefresh > 0){
135
            log.debug("NotesViewPage", "useEffect", "getNotes");
136
            dispatch(getItemNotes({ item: props.item, relatedComments: showRelatedComments }));
137
        }
138
    }, [triggerRefresh])
139
140 d3c12593 Fantič
    const toggleRelatedComments = () => {
141
        if (!props.notesLoading) {
142
143
            let value = !showRelatedComments;
144
145
            setShowRelatedComments(value);
146
            dispatch(getItemNotes({ item: props.item, relatedComments: value }));
147
        }
148
    }
149
150 56626a63 Fantič
    const handleCreateItemComment = useCallback(
151
        (newComment: string, replyingTo: { messageId: string; userName: string } | null) => {
152
            if (!requestPending) {
153
                // creating new comment
154
                if (replyingTo != null) {
155
                    dispatch(createNote({ newNote: { note: newComment, reply_to: replyingTo.messageId } as Note }));
156
                } else {
157
                    dispatch(createNote({ newNote: { note: newComment, items: props.item.id } as any }));
158
                }
159 2135f53d Fantič
            }
160 56626a63 Fantič
        },
161
        [props.item.id]
162
    );
163 2135f53d Fantič
164 d3c12593 Fantič
    return (
165
166
        props.notesLoading ?
167
            (<LoadingBox text="Notes loading..." />)
168
            :
169 2135f53d Fantič
            (<VStack height="90%">
170 d3c12593 Fantič
                <Flex direction="row" alignItems="center" justify="flex-end">
171
                    <Text fontSize={"12"}>Show related comments</Text>
172
                    <Switch value={showRelatedComments} onChange={toggleRelatedComments} size="md" />
173
                </Flex>
174 2135f53d Fantič
                <Box height="120%">
175 293f99f8 Fantič
                    <NotesListView height={Dimensions.get('window').height - 70 - 160} requestPending={requestPending} notes={props.notes} navigation={props.navigation} handleCreateComment={handleCreateItemComment} />
176 2135f53d Fantič
                </Box>
177 d3c12593 Fantič
            </VStack>)
178
    );
179
}
180
181
182
183 b88520f8 Fantič
const ItemView = (props: ItemDetailProps) => {
184
185
    // item shown / note shown
186
    const [itemShown, setItemShown] = React.useState(true);
187
188 72afb023 Fantič
    const { itemLoading, notesLoading, navigation } = props;
189 852de08e Fantič
190 b88520f8 Fantič
    const handleItemClick = () => {
191
        setItemShown(true);
192
    }
193
194
    const handleNotesClick = () => {
195
        setItemShown(false);
196
    }
197
198
    return (
199 852de08e Fantič
        <Box display="flex" flex={1} flexDirection="column" justifyContent="space-between">
200 2135f53d Fantič
201
            <Box width="92%" marginLeft="5%" height="92%">
202
                {itemShown ? (
203
                    <ItemDetail itemLoading={itemLoading} item={props.item} />
204
                ) : (
205
                    <ItemNotes item={props.item} notesLoading={props.notesLoading} notes={props.notes} navigation={props.navigation} />
206
                )}
207
            </Box>
208
209 72afb023 Fantič
210 852de08e Fantič
211
            {/* item notes switch tab */}
212
            <HStack alignItems="center">
213 091dec61 Fantič
                <Button
214 852de08e Fantič
                    variant={itemShown ? "subtle" : "outline"}
215
                    w="50%"
216 b88520f8 Fantič
                    onPress={handleItemClick}
217
                >
218
                    Item
219
                </Button>
220 091dec61 Fantič
                <Button
221 852de08e Fantič
                    variant={itemShown ? "outline" : "subtle"}
222
                    w="50%"
223 b88520f8 Fantič
                    onPress={handleNotesClick}
224
                >
225
                    Notes
226
                </Button>
227
            </HStack>
228 852de08e Fantič
        </Box>
229 b88520f8 Fantič
    )
230
}
231
232
export default ItemView