Projekt

Obecné

Profil

« Předchozí | Další » 

Revize b88520f8

Přidáno uživatelem Fantič před téměř 2 roky(ů)

re #10454: ItemView: switching notes / item view

Zobrazit rozdíly:

src/components/item/ItemDetail.tsx
1
import { Center, Box, Heading, VStack, FormControl, Link, Input, Button, HStack, Text, Divider } from "native-base"
2
import React, { useState } from "react"
3
import { Item } from "../../types/item"
4
import { Note } from "../../types/note"
5

  
6
interface ItemDetailProps {
7
    item: Item,
8
    notes: Note[]
9
}
10

  
11

  
12
const ItemView = (props: {item: Item}) => {
13
    const item = props.item;
14
    return (
15
        <Center>
16
            {item && (
17
                <VStack>
18
                    <Text>
19
                        id: {item.id}
20
                    </Text>
21
                    <Text>
22
                        name: {item.workName}
23
                    </Text>
24
                </VStack>
25
            )}
26

  
27
        </Center>
28
    )
29
}
30

  
31
const ItemDetail = (props: ItemDetailProps) => {
32

  
33
    return (
34
        <ItemView item={props.item}></ItemView>
35
    )
36
}
37

  
38
export default ItemDetail
src/components/item/ItemView.tsx
1
import { Center, Box, Heading, VStack, FormControl, Link, Input, Button, HStack, Text, Divider } from "native-base"
2
import React, { useState } from "react"
3
import { Item } from "../../types/item"
4
import { Note } from "../../types/note"
5

  
6
interface ItemDetailProps {
7
    item: Item,
8
    notes: Note[]
9
}
10

  
11

  
12
const ItemDetail = (props: { item: Item }) => {
13
    const item = props.item;
14
    return (
15
        <Center>
16
            {
17
                item && (
18
                    <VStack>
19
                        <Text>id: {item.id}</Text>
20
                        <Text>name: {item.workName}</Text>
21
                    </VStack>
22
                )
23
            }
24
        </Center>
25
    );
26
}
27

  
28
const ItemNotes = (props: { notes: Note[] }) => {
29
    const notes = props.notes;
30
    return (
31
        <Center>
32
            {
33
                notes && notes.length > 0 ? (
34
                    <VStack>
35
                        {notes.map((note, index) => (
36
                            <Box key={index}>
37
                                <Text>{note.username}</Text>
38
                                <Text>{note.note}</Text>
39
                            </Box>
40
                        ))}
41
                    </VStack>
42
                ) : (
43
                    <Text>There are no notes for selected item.</Text>
44
                )
45
            }
46
        </Center>
47
    );
48
}
49

  
50

  
51
const ItemView = (props: ItemDetailProps) => {
52

  
53
    // item shown / note shown
54
    const [itemShown, setItemShown] = React.useState(true);
55

  
56
    const handleItemClick = () => {
57
        setItemShown(true);
58
    }
59

  
60
    const handleNotesClick = () => {
61
        setItemShown(false);
62
    }
63

  
64

  
65
    return (
66
        <>
67
            {itemShown ? (
68
                <ItemDetail item={props.item} />
69
            ) : (
70
                <ItemNotes notes={props.notes} />
71
            )}
72
            <HStack mt={4}>
73
                <Button
74
                    colorScheme={itemShown ? "blue" : undefined}
75
                    onPress={handleItemClick}
76
                >
77
                    Item
78
                </Button>
79
                <Button
80
                    colorScheme={!itemShown ? "blue" : undefined}
81
                    onPress={handleNotesClick}
82
                >
83
                    Notes
84
                </Button>
85
            </HStack>
86
        </>
87
    )
88
}
89

  
90
export default ItemView
src/pages/ItemViewPage.tsx
3 3
import { useDispatch, useSelector } from "react-redux"
4 4
import { AppDispatch, RootState } from "../stores/store"
5 5
import { getItem, getItemNotes, setConcordances, setSelectedConcordance } from "../stores/actions/itemThunks"
6
import { Item, ItemViewState } from "../types/item"
7 6
import { login } from "../stores/actions/userThunks"
8 7
import { SceneRendererProps, TabView } from "react-native-tab-view"
9 8
import { useWindowDimensions } from "react-native"
10
import ItemDetail from "../components/item/ItemDetail"
9
import ItemView from "../components/item/ItemView"
10

  
11 11

  
12 12
interface ItemViewProps {
13 13
  itemId: string
......
65 65

  
66 66
  // item changes
67 67
  useEffect(() => {
68
    
68

  
69 69
    if (item && item.id) {
70 70
      if (selectedConcordance == 0) {
71 71
        dispatch(setConcordances(item.concordances));
......
81 81
  return (
82 82
    <TabView
83 83
      navigationState={{ index: selectedConcordance, routes }}
84
      renderScene={() => <ItemDetail item={item} />}
84
      renderScene={() => <ItemView item={item} notes={notes} />}
85 85
      onIndexChange={handleTabChange}
86 86
      initialLayout={{ width: layout.width }}
87 87
    />
src/stores/actions/itemThunks.ts
122 122
                        notes.push({
123 123
                            username: (note as any).created_by,
124 124
                            userId: (note as any).created_by_id,
125
                            note: (note as any).note,
125 126
                            avatarUrl: (note as any).avatar,
126 127
                            items: (note as any).items,
127 128
                            createdTime: (note as any).created, 
src/types/note.ts
1 1
export interface Note {
2 2
    username: string
3 3
    userId: string
4
    note: string // text
4 5
    avatarUrl: string
5 6
    items: string[]
6 7
    createdTime: Date 

Také k dispozici: Unified diff