Projekt

Obecné

Profil

Stáhnout (9.64 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Center, Box, VStack, Button, HStack, Text, Image, ScrollView, View, Spacer, Card, Heading, Switch, Flex } from "native-base"
2
import React, { useEffect, useState } from "react"
3
import { Item } 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
interface ItemDetailProps {
15
    item: Item,
16
    itemLoading: boolean,
17
    notes: Note[],
18
    notesLoading: boolean,
19
    navigation: any
20
}
21

    
22
const ItemDetail = (props: { item: Item, itemLoading: boolean }) => {
23
    const item = props.item;
24

    
25
    return (<>
26
        {
27
            item && props.itemLoading ?
28
                (<LoadingBox text="Item loading..." />)
29
                :
30
                (
31
                    <ScrollView flex="1">
32
                        <VStack>
33
                            <HStack marginTop="5%">
34
                                <VStack width="75%">
35
                                    <Text>
36
                                        {item.authorDisplayName ? item.authorDisplayName : item.authorName}
37
                                    </Text>
38
                                    <Heading size="sm">
39
                                        {item.workName}
40
                                    </Heading>
41

    
42
                                </VStack>
43
                                <Box width="25%">
44
                                    <Button variant="outline" size="md">
45
                                        {item.concordances[0].id}
46
                                    </Button>
47
                                </Box>
48
                            </HStack>
49
                            <Card shadow="1" marginTop="5%">
50
                                {item.inventoryItem &&
51
                                    <VStack>
52
                                        <Text italic>
53
                                            <Text color="light.500">Inventory item: </Text>
54
                                            {item.inventoryItem}
55
                                        </Text>
56
                                        <Text italic marginTop="2.5%">
57
                                            {item.searchSubjects.map((subject, index) => (
58
                                                index < item.searchSubjects.length - 1 ? (subject + ", ") : (subject)
59
                                            ))}
60
                                        </Text>
61
                                    </VStack>}
62
                            </Card>
63
                            {item.fullView && item.imageUrl && (
64
                                <Center background="primary.100" marginTop="5%">
65
                                    <Text>Resembling object</Text>
66
                                    <Image size={250} alt="image" source={{ uri: IMAGE_URL + "/" + item.imageUrl }} />
67
                                </Center>)}
68
                            {item.fullView && (
69
                                <Card shadow="1" marginTop="5%" marginBottom="5%">
70
                                    {item.authorName &&
71
                                        <HStack>
72
                                            <Text italic marginTop="2.5%">
73
                                                <Text color="light.500">Author name: </Text>
74
                                                {item.authorName}
75
                                            </Text>
76
                                        </HStack>}
77
                                    {item.title &&
78
                                        <HStack>
79
                                            <Text italic marginTop="2.5%">
80
                                                <Text color="light.500">Title: </Text>
81
                                                {item.title}
82
                                            </Text>
83
                                        </HStack>}
84
                                    {item.institution &&
85
                                        <HStack>
86
                                            <Text italic marginTop="2.5%">
87
                                                <Text color="light.500">Institution: </Text>
88
                                                {item.institution.name}, Inv. No. {item.institution.inventoryNumber}, {item.institution.city} {"(" + item.institution.country + ")"}
89
                                            </Text>
90
                                        </HStack>}
91
                                    {item.repository &&
92
                                        <HStack>
93
                                            <Text italic marginTop="2.5%">
94
                                                <Text color="light.500">Repository: </Text>
95
                                                {item.repository}
96
                                            </Text>
97
                                        </HStack>}
98
                                    {item.provenance &&
99
                                        <HStack>
100
                                            <Text italic marginTop="2.5%">
101
                                                <Text color="light.500">Provenance: </Text>
102
                                                {item.provenance}
103
                                            </Text>
104
                                        </HStack>}
105
                                    {item.description &&
106
                                        <HStack>
107
                                            <Text italic marginTop="2.5%">
108
                                                <Text color="light.500">Description: </Text>
109
                                                {item.description}
110
                                            </Text>
111
                                        </HStack>}
112
                                </Card>)}
113
                        </VStack>
114
                    </ScrollView>
115
                )
116
        }</>
117
    );
118
}
119

    
120

    
121

    
122
const ItemNotes = (props: { item: Item, notes: Note[], notesLoading: boolean, navigation: any }) => {
123

    
124
    const [showRelatedComments, setShowRelatedComments] = useState(true);
125

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

    
128
    const dispatch = useDispatch<AppDispatch>();
129

    
130
    // trigger refresh on triggerRefresh increment
131
    useEffect(() => {
132
        if(triggerRefresh > 0){
133
            log.debug("NotesViewPage", "useEffect", "getNotes");
134
            dispatch(getItemNotes({ item: props.item, relatedComments: showRelatedComments }));
135
        }
136
    }, [triggerRefresh])
137

    
138
    const toggleRelatedComments = () => {
139
        if (!props.notesLoading) {
140

    
141
            let value = !showRelatedComments;
142

    
143
            setShowRelatedComments(value);
144
            dispatch(getItemNotes({ item: props.item, relatedComments: value }));
145
        }
146
    }
147

    
148
    const handleCreateItemComment = (newComment: string, replyingTo: { messageId: string; userName: string } | null) => {
149
        if (!requestPending) {
150
            // creating new comment
151
            if (replyingTo != null) {
152
                dispatch(createNote({
153
                    newNote: { note: newComment, reply_to: replyingTo.messageId } as Note
154
                }))
155
            }
156
            else {
157
                dispatch(createNote({
158
                    newNote: { note: newComment, items: props.item.id } as any
159
                }))
160
            }
161
        }
162
    }
163

    
164
    return (
165

    
166
        props.notesLoading ?
167
            (<LoadingBox text="Notes loading..." />)
168
            :
169
            (<VStack height="90%">
170
                <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
                <Box height="120%">
175
                    <NotesListView requestPending={requestPending} notes={props.notes} navigation={props.navigation} handleCreateComment={handleCreateItemComment} />
176
                </Box>
177
            </VStack>)
178
    );
179
}
180

    
181

    
182

    
183
const ItemView = (props: ItemDetailProps) => {
184

    
185
    // item shown / note shown
186
    const [itemShown, setItemShown] = React.useState(true);
187

    
188
    const { itemLoading, notesLoading, navigation } = props;
189

    
190
    const handleItemClick = () => {
191
        setItemShown(true);
192
    }
193

    
194
    const handleNotesClick = () => {
195
        setItemShown(false);
196
    }
197

    
198
    return (
199
        <Box display="flex" flex={1} flexDirection="column" justifyContent="space-between">
200

    
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

    
210

    
211
            {/* item notes switch tab */}
212
            <HStack alignItems="center">
213
                <Button
214
                    variant={itemShown ? "subtle" : "outline"}
215
                    w="50%"
216
                    onPress={handleItemClick}
217
                >
218
                    Item
219
                </Button>
220
                <Button
221
                    variant={itemShown ? "outline" : "subtle"}
222
                    w="50%"
223
                    onPress={handleNotesClick}
224
                >
225
                    Notes
226
                </Button>
227
            </HStack>
228
        </Box>
229
    )
230
}
231

    
232
export default ItemView
(2-2/2)