Projekt

Obecné

Profil

Stáhnout (17 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Center, Box, VStack, Button, HStack, Text, Image, ScrollView, View, Spacer, Card, Heading, Switch, Flex, ChevronLeftIcon, ChevronRightIcon, Divider, Pressable, CircleIcon } from "native-base"
2
import React, { useCallback, useEffect, useState } from "react"
3
import { Certainty, Item, RelationshipType } from "../../types/item"
4
import { Note } from "../../types/note"
5
import LoadingBox from "../loading/LoadingBox"
6
import NotesListView from "../notes/NotesListView"
7
import { IMAGE_URL } from "../../api/constants"
8
import { log } from "../../logging/logger"
9
import { createNote, getAllNotes } from "../../stores/actions/notesThunks"
10
import { AppDispatch, RootState } from "../../stores/store"
11
import { useDispatch, useSelector } from "react-redux"
12
import { getItemNotes } from "../../stores/actions/itemThunks"
13

    
14
import { Dimensions } from "react-native";
15
import { DrawerNavigationProp } from "@react-navigation/drawer"
16
import { RootStackParamList } from "../../pages/Navigation"
17
import { select } from "d3"
18
import { CertaintyWithColors } from "../../stores/reducers/itemSlice"
19

    
20
interface ItemDetailProps {
21
    item: Item,
22
    itemLoading: boolean,
23
    notes: Note[],
24
    notesLoading: boolean,
25
    navigation: DrawerNavigationProp<RootStackParamList, "Item", undefined>
26
}
27

    
28
const ItemDetail = (props: ItemDetailProps) => {
29
    const item = props.item;
30

    
31
    const [selectedImage, setSelectedImage] = useState<number>(0);
32

    
33
    const [images, setImages] = useState<{ imageUrl: string, title: string }[]>([]);
34

    
35

    
36
    useEffect(() => {
37
        if (item.images?.[0]) {
38
            setImages([item.images[0], item.images[0], item.images[0], item.images[0], item.images[0], item.images[0], item.images[0]])
39
        }
40
    }, [item])
41

    
42
    return (<>
43
        {
44
            item && props.itemLoading ?
45
                (<LoadingBox text="Item loading..." />)
46
                :
47
                (
48
                    <ScrollView flex="1" paddingLeft={2.5} paddingRight={2.5}>
49
                        <VStack>
50
                            <HStack marginLeft={2.5} alignItems={"center"}>
51
                                <VStack flex={1} marginTop={3.5}>
52
                                    <Text fontSize={13} color="#4D4D4D">
53
                                        {item.authorDisplayName ? item.authorDisplayName : item.authorName}
54
                                    </Text>
55
                                    <Heading marginTop={1} fontSize={18} color="#4D4D4D">
56
                                        {item.workName}
57
                                    </Heading>
58

    
59
                                </VStack>
60
                                <Box marginLeft="auto" marginTop={0}> {/* marginLeft: "auto" pushes the Box to the right */}
61
                                    <Button variant="outline" backgroundColor={"#F4DFAB"} borderRadius={8} size="sm" padding={1.5} >
62
                                        <Text color="#654B07" fontSize={14}>
63
                                            {item?.concordances?.[0]?.id}
64
                                        </Text>
65
                                    </Button>
66
                                </Box>
67
                            </HStack>
68

    
69

    
70

    
71

    
72
                            <View marginTop={5} backgroundColor="#F5F4F1" borderRadius={10} borderColor="#F5F4F1" padding={2}>
73
                                {item.inventoryItem &&
74
                                    <VStack>
75
                                        <Text italic>
76
                                            <Text color="#654B07" italic fontWeight={"semibold"}>Inventory item: </Text>
77
                                            {item.inventoryItem}
78
                                        </Text>
79
                                        <Text italic marginTop="2.5%">
80
                                            {item.searchSubjects != undefined && item?.searchSubjects?.map((subject, index) => (
81
                                                subject != "" ?
82
                                                    item.searchSubjects && index < item.searchSubjects.length - 1 ? (subject + ", ") : (subject)
83
                                                    :
84
                                                    ""
85
                                            ))}
86
                                        </Text>
87
                                    </VStack>}
88
                            </View>
89
                            {item.fullView && item.images && item.images.length > 0 && (
90
                                <VStack marginTop={5} alignItems="center" justifyContent="center" backgroundColor="#F5F4F1" borderRadius={10} borderColor="#F5F4F1" padding={2}>
91

    
92
                                    <Box style={{ backgroundColor: item.images[selectedImage].relationship_type != RelationshipType.IdentifiedArtwork ? "#B3B3B3" : CertaintyWithColors[item.images[selectedImage].cert ?? Certainty.Unknown].color }}
93
                                        marginTop={2.5} width={Dimensions.get('window').width - 110} alignItems="center" borderTopRadius={10} >
94
                                        <Text marginBottom={2}>
95

    
96
                                            {item.images[selectedImage].relationship_type == RelationshipType.Resembling && "Resembling object"}
97
                                            {item.images[selectedImage].relationship_type == RelationshipType.IdentifiedArtwork && "Identified artwork"}
98
                                            {item.images[selectedImage].relationship_type == RelationshipType.Related && "Related item"}
99

    
100
                                            {item.images[selectedImage].relationship_type == RelationshipType.IdentifiedArtwork && item.images[selectedImage].cert && item.images[selectedImage].cert != Certainty.Unknown && ` - ${item.images?.[selectedImage].cert}`}
101
                                        </Text>
102
                                    </Box>
103

    
104
                                    <HStack space={4}>
105
                                        <Button variant="ghost" size="lg" onPress={() => {
106
                                            if (item.images && selectedImage - 1 >= 0) {
107
                                                setSelectedImage(selectedImage - 1);
108
                                            }
109
                                        }} leftIcon={<ChevronLeftIcon size="lg" />} />
110
                                        <Image size={Dimensions.get('window').width - 110} alt="image" source={{ uri: IMAGE_URL + "/" + item.images[selectedImage].imageUrl }} resizeMode={"contain"} />
111
                                        <Button variant="ghost" size="lg" onPress={() => {
112
                                            if (item.images && selectedImage + 1 < item.images.length) {
113
                                                setSelectedImage(selectedImage + 1);
114
                                            }
115
                                        }} leftIcon={<ChevronRightIcon size="lg" />} />
116
                                    </HStack>
117
                                    <ScrollView horizontal marginBottom={2.5} marginTop={5}>
118

    
119
                                        {item.images.map((image, index) => (
120
                                            <Pressable
121
                                                onPress={() => { setSelectedImage(index); }}>
122
                                                <Image marginLeft={2.5} size={90} alt="image" source={{ uri: IMAGE_URL + "/" + image.imageUrl }}
123
                                                    borderColor={selectedImage == index ? "#1782FF" : undefined}
124
                                                    borderWidth={3}
125
                                                    resizeMode={"contain"}
126
                                                />
127
                                            </Pressable>
128

    
129
                                        ))}
130
                                    </ScrollView>
131
                                </VStack>
132
                            )}
133
                            {item.fullView && (
134
                                <View marginTop={5} backgroundColor="#F5F4F1" borderRadius={10} borderColor="#F5F4F1" padding={2} marginBottom={5}>
135
                                    {item.authorName &&
136
                                        <HStack>
137
                                            <Text marginTop={2.5}>
138
                                                <Text color="#654B07" italic fontWeight={"semibold"}>Author name: </Text>
139
                                                {item.images?.[selectedImage]?.author}
140
                                            </Text>
141
                                        </HStack>}
142
                                    {item.title &&
143
                                        <HStack>
144
                                            <Text marginTop={2.5}>
145
                                                <Text color="#654B07" italic fontWeight={"semibold"}>Title: </Text>
146
                                                {item.images?.[selectedImage].title}
147
                                            </Text>
148
                                        </HStack>}
149
                                    {item.institution &&
150
                                        <HStack>
151
                                            <Text marginTop={2.5}>
152
                                                <Text color="#654B07" italic fontWeight={"semibold"}>Institution: </Text>
153
                                                {item.images?.[selectedImage].institution.name && `${item.images?.[selectedImage].institution.name}`}
154
                                                {item.images?.[selectedImage].institution.inventoryNumber && `, Inv. No. ${item.images?.[selectedImage].institution.inventoryNumber}`}
155
                                                {item.images?.[selectedImage].institution.city && `, ${item.images?.[selectedImage].institution.city}`}
156
                                                {item.images?.[selectedImage].institution.country && ` (${item.images?.[selectedImage].institution.country})`}
157
                                            </Text>
158
                                        </HStack>}
159
                                    {item.repository &&
160
                                        <HStack>
161
                                            <Text marginTop={2.5}>
162
                                                <Text color="#654B07" italic fontWeight={"semibold"}>Repository: </Text>
163
                                                {item.images?.[selectedImage].repository}
164
                                            </Text>
165
                                        </HStack>}
166
                                    {item.images?.[selectedImage].provenance &&
167
                                        <HStack>
168
                                            <Text marginTop={2.5}>
169
                                                <Text color="#654B07" italic fontWeight={"semibold"}>Provenance: </Text>
170
                                                {item.images?.[selectedImage].provenance}
171
                                            </Text>
172
                                        </HStack>}
173
                                    {item.images?.[selectedImage].description &&
174
                                        <HStack>
175
                                            <Text marginTop={2.5}>
176
                                                <Text color="#654B07" italic fontWeight={"semibold"}>Description: </Text>
177
                                                {item.images?.[selectedImage].description}
178
                                            </Text>
179
                                        </HStack>}
180
                                </View>)}
181

    
182
                            {
183
                                item.room && item.room.id &&
184
                                <Flex direction="row" alignItems="center" justify="flex-end">
185
                                    <Button
186
                                        onPress={() => props.navigation.navigate("Plan", { roomId: item?.room?.id, placeId: undefined })}
187
                                        variant={"link"}
188
                                        backgroundColor={"#F4DFAB"}
189
                                        borderRadius={7}
190
                                        marginTop={0}
191
                                        marginBottom={5}
192
                                        width={125}
193
                                        rightIcon={<ChevronRightIcon size="xs" />}>
194
                                        Show in map
195
                                    </Button>
196
                                </Flex>
197

    
198
                            }
199

    
200
                        </VStack>
201
                    </ScrollView >
202
                )
203
        }</>
204
    );
205
}
206

    
207

    
208

    
209
const ItemNotes = (props: { item: Item, notes: Note[], notesLoading: boolean, navigation: DrawerNavigationProp<RootStackParamList, "Item", undefined> }) => {
210

    
211
    const [showRelatedComments, setShowRelatedComments] = useState(true);
212

    
213
    const { requestPending, triggerRefresh } = useSelector((state: RootState) => state.noteViewState)
214

    
215
    const dispatch = useDispatch<AppDispatch>();
216

    
217
    // trigger refresh on triggerRefresh increment
218
    useEffect(() => {
219
        if (triggerRefresh > 0) {
220
            log.debug("NotesViewPage", "useEffect", "getNotes");
221
            dispatch(getItemNotes({ item: props.item, relatedComments: showRelatedComments }));
222
        }
223
    }, [triggerRefresh])
224

    
225
    const toggleRelatedComments = useCallback(() => {
226
        if (!props.notesLoading) {
227

    
228
            let value = !showRelatedComments;
229

    
230
            setShowRelatedComments(value);
231
            dispatch(getItemNotes({ item: props.item, relatedComments: value }));
232
        }
233
    }, [showRelatedComments]);
234

    
235

    
236
    const handleCreateItemComment = useCallback(
237
        (newComment: string, replyingTo: { messageId: string; userName: string } | null) => {
238
            if (!requestPending) {
239
                // creating new comment
240
                if (replyingTo != null) {
241
                    dispatch(createNote({ newNote: { note: newComment, reply_to: replyingTo.messageId } as Note }));
242
                } else {
243
                    dispatch(createNote({ newNote: { note: newComment, items: props.item.id } as any }));
244
                }
245
            }
246
        },
247
        [props.item.id]
248
    );
249

    
250
    return (
251

    
252
        props.notesLoading ?
253
            (<LoadingBox text="Notes loading..." />)
254
            :
255
            (<VStack>
256
                <Flex direction="row" align="flex-start" justifyContent="space-between" marginTop={2.5} marginLeft={2.5} marginRight={2.5}>
257
                    <Text fontWeight="semibold" mt={4} ml={2} fontSize="md" color="#4D4D4D">Show related comments</Text>
258
                    <Switch value={showRelatedComments} onChange={toggleRelatedComments} size="lg" marginRight={2} />
259
                </Flex>
260
                <Box >
261
                    <NotesListView height={Dimensions.get('window').height - 315} requestPending={requestPending} notes={props.notes} navigation={props.navigation} handleCreateComment={handleCreateItemComment} />
262
                </Box>
263
            </VStack>)
264
    );
265
}
266

    
267

    
268

    
269
const ItemView = (props: ItemDetailProps) => {
270

    
271
    // item shown / note shown
272
    const [itemShown, setItemShown] = React.useState(true);
273

    
274
    const { itemLoading, notesLoading, navigation } = props;
275

    
276
    const handleItemClick = () => {
277
        setItemShown(true);
278
    }
279

    
280
    const handleNotesClick = () => {
281
        setItemShown(false);
282
    }
283

    
284
    return (
285
        <Box>
286

    
287
            <Box height={Dimensions.get('window').height - 165}>
288
                {itemShown ? (
289
                    <ItemDetail itemLoading={props.itemLoading} item={props.item} notesLoading={props.notesLoading} notes={props.notes} navigation={props.navigation} />
290
                ) : (
291
                    <ItemNotes item={props.item} notesLoading={props.notesLoading} notes={props.notes} navigation={props.navigation} />
292
                )}
293
            </Box>
294

    
295
            {/* item notes switch tab */}
296
            <HStack alignItems="center">
297
                <Button
298
                    variant={"unstyled"}
299
                    w="50%"
300
                    onPress={handleItemClick}
301
                >
302
                    <VStack alignItems={"center"}>
303
                        <Text fontSize="md" color="primary" bold={itemShown} mb={itemShown ? undefined : 2}>
304
                            Item
305
                        </Text>
306
                        {itemShown && <Divider borderRadius={5} borderWidth={1} width={70} mt={1.5} color="primary" />}
307
                    </VStack>
308
                </Button>
309
                <Button
310
                    variant={"unstyled"}
311
                    w="50%"
312
                    onPress={handleNotesClick}
313
                >
314
                    <VStack alignItems={"center"}>
315
                        <Text fontSize="md" color="primary" bold={!itemShown} mb={!itemShown ? undefined : 2}>
316
                            Notes
317
                        </Text>
318
                        {!itemShown && <Divider borderRadius={5} borderWidth={1} width={70} mt={1.5} color="primary" />}
319
                    </VStack>
320
                </Button>
321
            </HStack>
322
        </Box>
323
    )
324
}
325

    
326
export default ItemView
(2-2/2)