Projekt

Obecné

Profil

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

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

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

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

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

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

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

    
195
                            }
196

    
197
                        </VStack>
198
                    </ScrollView >
199
                )
200
        }</>
201
    );
202
}
203

    
204

    
205

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

    
208
    const [showRelatedComments, setShowRelatedComments] = useState(true);
209

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

    
212
    const dispatch = useDispatch<AppDispatch>();
213

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

    
222
    const toggleRelatedComments = useCallback(() => {
223
        if (!props.notesLoading) {
224

    
225
            let value = !showRelatedComments;
226

    
227
            setShowRelatedComments(value);
228
            dispatch(getItemNotes({ item: props.item, relatedComments: value }));
229
        }
230
    }, [showRelatedComments]);
231

    
232

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

    
247
    return (
248

    
249
        props.notesLoading ?
250
            (<LoadingBox text="Notes loading..." />)
251
            :
252
            (<VStack paddingLeft={2.5} paddingRight={2.5}>
253
                <Flex direction="row" alignItems="center" justify="flex-end">
254
                    <Text fontSize={"12"}>Show related comments</Text>
255
                    <Switch value={showRelatedComments} onChange={toggleRelatedComments} size="md" />
256
                </Flex>
257
                <Box >
258
                    <NotesListView height={Dimensions.get('window').height - 355} requestPending={requestPending} notes={props.notes} navigation={props.navigation} handleCreateComment={handleCreateItemComment} />
259
                </Box>
260
            </VStack>)
261
    );
262
}
263

    
264

    
265

    
266
const ItemView = (props: ItemDetailProps) => {
267

    
268
    // item shown / note shown
269
    const [itemShown, setItemShown] = React.useState(true);
270

    
271
    const { itemLoading, notesLoading, navigation } = props;
272

    
273
    const handleItemClick = () => {
274
        setItemShown(true);
275
    }
276

    
277
    const handleNotesClick = () => {
278
        setItemShown(false);
279
    }
280

    
281
    return (
282
        <Box>
283

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

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

    
323
export default ItemView
(2-2/2)