Projekt

Obecné

Profil

Stáhnout (17.2 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, IconButton } 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
import { LeftArrowIcon, RightArrowIcon } from "../general/Icons"
20
import { ZoomableImage } from "./ZoomableImage"
21

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

    
30
const ItemDetail = (props: ItemDetailProps) => {
31
    const item = props.item;
32

    
33
    const [selectedImage, setSelectedImage] = useState<number>(0);
34

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

    
37

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

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

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

    
71

    
72

    
73

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

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

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

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

    
106
                                    <HStack style={{ alignItems: "center", marginTop: 5 }}>
107
                                        <IconButton variant="ghost" style={{ height: 30, width: 30, borderRadius: 15 }} onPress={() => {
108
                                            if (item.images && selectedImage - 1 >= 0) {
109
                                                setSelectedImage(selectedImage - 1);
110
                                            }
111
                                        }} icon={<LeftArrowIcon color="#49454F" />} />
112

    
113
                                        <ZoomableImage fullScreen={false} size={Dimensions.get('window').width - 110} imageUri={IMAGE_URL + "/" + item.images[selectedImage].imageUrl} />
114

    
115
                                        <IconButton variant="ghost" style={{ height: 30, width: 30, borderRadius: 15 }} onPress={() => {
116
                                            if (item.images && selectedImage + 1 < item.images.length) {
117
                                                setSelectedImage(selectedImage + 1);
118
                                            }
119
                                        }} icon={<RightArrowIcon color="#49454F" />} />
120
                                    </HStack>
121
                                    <ScrollView horizontal marginBottom={2.5} marginTop={5}>
122

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

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

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

    
202
                            }
203

    
204
                        </VStack>
205
                    </ScrollView >
206
                )
207
        }</>
208
    );
209
}
210

    
211

    
212

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

    
215
    const [showRelatedComments, setShowRelatedComments] = useState(true);
216

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

    
219
    const dispatch = useDispatch<AppDispatch>();
220

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

    
229
    const toggleRelatedComments = useCallback(() => {
230
        if (!props.notesLoading) {
231

    
232
            let value = !showRelatedComments;
233

    
234
            setShowRelatedComments(value);
235
            dispatch(getItemNotes({ item: props.item, relatedComments: value }));
236
        }
237
    }, [showRelatedComments]);
238

    
239

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

    
254
    return (
255

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

    
271

    
272

    
273
const ItemView = (props: ItemDetailProps) => {
274

    
275
    // item shown / note shown
276
    const [itemShown, setItemShown] = React.useState(true);
277

    
278
    const { itemLoading, notesLoading, navigation } = props;
279

    
280
    const handleItemClick = () => {
281
        setItemShown(true);
282
    }
283

    
284
    const handleNotesClick = () => {
285
        setItemShown(false);
286
    }
287

    
288
    return (
289
        <Box>
290

    
291
            <Box height={Dimensions.get('window').height - 145}>
292
                {itemShown ? (
293
                    <ItemDetail itemLoading={props.itemLoading} item={props.item} notesLoading={props.notesLoading} notes={props.notes} navigation={props.navigation} />
294
                ) : (
295
                    <ItemNotes item={props.item} notesLoading={props.notesLoading} notes={props.notes} navigation={props.navigation} />
296
                )}
297
            </Box>
298

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

    
330
export default ItemView
(2-2/3)