Projekt

Obecné

Profil

Stáhnout (9.76 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, { useCallback, 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
import { Dimensions } from "react-native";
15

    
16
interface ItemDetailProps {
17
    item: Item,
18
    itemLoading: boolean,
19
    notes: Note[],
20
    notesLoading: boolean,
21
    navigation: any
22
}
23

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

    
27
    return (<>
28
        {
29
            item && props.itemLoading ?
30
                (<LoadingBox text="Item loading..." />)
31
                :
32
                (
33
                    <ScrollView flex="1">
34
                        <VStack>
35
                            <HStack marginTop="5%">
36
                                <VStack width="75%">
37
                                    <Text>
38
                                        {item.authorDisplayName ? item.authorDisplayName : item.authorName}
39
                                    </Text>
40
                                    <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
                                        </Text>
58
                                        <Text italic marginTop="2.5%">
59
                                            {item.searchSubjects.map((subject, index) => (
60
                                                index < item.searchSubjects.length - 1 ? (subject + ", ") : (subject)
61
                                            ))}
62
                                        </Text>
63
                                    </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
                )
118
        }</>
119
    );
120
}
121

    
122

    
123

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

    
126
    const [showRelatedComments, setShowRelatedComments] = useState(true);
127

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

    
130
    const dispatch = useDispatch<AppDispatch>();
131

    
132
    // 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
    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
    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
            }
160
        },
161
        [props.item.id]
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 height={Dimensions.get('window').height - 70 - 160} 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)