Projekt

Obecné

Profil

« Předchozí | Další » 

Revize c03fd43c

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

re #10454: ItemView concordances switching

Zobrazit rozdíly:

App.tsx
11 11
    <Provider store={store}>
12 12
      <NativeBaseProvider>
13 13
        <VStack space={4} alignItems="center">
14
          {/* TODO: remove placeholder pro navbar */}
15
          <Center w="100%" h="40" bg="indigo.300" rounded="md" shadow={3} />
14
          {/* TODO: remove placeholder pro header / nav */}
15
          <Center w="100%" h="40" bg="primary.100" rounded="md" shadow={3} />
16 16
          <ItemViewPage itemId={'BxlA-2'} />
17 17
          {/* <ItemViewPage itemId={'BxlA-5'} /> */}
18 18
        </VStack>
src/pages/ItemViewPage.tsx
2 2
import React, { useState, useEffect } from "react"
3 3
import { useDispatch, useSelector } from "react-redux"
4 4
import { AppDispatch, RootState } from "../stores/store"
5
import { getPreviousConcordance, getNextConcordance, getItem } from "../stores/actions/itemThunks"
5
import { getItem, setConcordances, setSelectedConcordance } from "../stores/actions/itemThunks"
6 6
import { Item, ItemViewState } from "../types/item"
7 7
import { login } from "../stores/actions/userThunks"
8 8

  
......
14 14

  
15 15
  const dispatch = useDispatch<AppDispatch>();
16 16

  
17
  const {item, concordances, selectedConcordance} = useSelector((state: RootState) => state.itemViewState)
17
  const { item, concordances, selectedConcordance } = useSelector((state: RootState) => state.itemViewState)
18 18

  
19 19
  const handleGetPreviousConcordance = async () => {
20
    dispatch(getPreviousConcordance());
20
    if (selectedConcordance - 1 >= 0) {
21
      dispatch(setSelectedConcordance(selectedConcordance - 1));
22
      dispatch(getItem(concordances[selectedConcordance].id));
23
    }
21 24
  };
22 25

  
23 26
  const handleGetNextConcordance = async () => {
24
    dispatch(getNextConcordance());
27
    if (selectedConcordance + 1 < concordances.length) {
28
      dispatch(setSelectedConcordance(selectedConcordance + 1))
29
      dispatch(getItem(concordances[selectedConcordance].id));
30
    }
25 31
  };
26 32

  
33
  // initialize with base item (set concordances from it)
27 34
  useEffect(() => {
28 35
    // TODO remove login from here
29 36
    dispatch(login({ username: "Viktorie", password: "Golem123." }));
30

  
31 37
    dispatch(getItem(props.itemId));
38
    dispatch(setConcordances(item.concordances));
32 39
  }, []);
33 40

  
34
  console.log(concordances);
35
  console.log(item);
36 41

  
37 42
  return (
38 43
    <VStack>
39 44
      <Center>
40

  
41

  
42

  
43 45
        <VStack>
44 46
          <Text>
45 47
            Concordances: {concordances.map((concordance) => concordance.id).join(', ')}
46 48
          </Text>
47 49
          <Text>
48
            Selected: {concordances[selectedConcordance].id} 
50
            {/* Selected: {concordances[selectedConcordance].id} */}
51
            Selected: {selectedConcordance}
49 52
          </Text>
50 53
          <HStack>
51

  
52 54
            <Button onPress={handleGetPreviousConcordance} mr={2}>
53 55
              Previous Concordance
54 56
            </Button>
55 57
            <Button onPress={handleGetNextConcordance}>Next Concordance</Button>
56 58
          </HStack>
59
          <Text>
60
            current item: {item.workName}
61
          </Text>
57 62
        </VStack>
58 63

  
59 64
        <Divider my="2" />
src/stores/actions/itemThunks.ts
1 1
import { createAsyncThunk } from "@reduxjs/toolkit"
2 2
import { getItemRequest } from "../../api/itemservice"
3 3
import { getItemNotesRequest } from "../../api/notesservice"
4
import { ItemViewState } from "../../types/item";
4
import { Concordance, ItemViewState } from "../../types/item";
5 5

  
6 6

  
7 7
export const getItem = createAsyncThunk(
......
16 16

  
17 17
            // data with image
18 18
            if (response.status === 200 && response.data.object.length > 1) {
19
                
20 19
                return {
21 20
                    id: itemId,
22 21
                    fullView: true,
......
28 27

  
29 28
                    inventoryItem: response.data.text,
30 29
                    searchSubjects: response.data.search_subject,
31
                
30

  
32 31
                    // additional fields
33 32
                    authorName: response.data.object[1].name[0].getty_data.display_name,
34 33

  
......
36 35
                    title: response.data.object[1].images[0].text,
37 36

  
38 37
                    institution: {
39
                        name :response.data.object[1].institution,
38
                        name: response.data.object[1].institution,
40 39
                        inventoryNumber: response.data.object[1].id_number,
41 40
                        country: response.data.object[1].country,
42 41
                        city: response.data.object[1].city
......
67 66
    }
68 67
)
69 68

  
70
export const getNextConcordance = createAsyncThunk(
71
    "item/getNextConcordance",
72
    async (_, { getState, dispatch }) => {
73
        const state = getState() as ItemViewState;
74

  
75
        const nextIndex = (state.selectedConcordance + 1) % state.concordances.length;
76

  
77
        // Dispatch the getItem action with the next concordance id
78
        await dispatch(getItem(state.concordances[nextIndex].id));
79

  
80
        // Return the next concordance index for updating the state in the reducer
81
        return nextIndex;
82
    }
83
);
84

  
85
export const getPreviousConcordance = createAsyncThunk(
86
    "item/getPreviousConcordance",
87
    async (_, { getState, dispatch }) => {
88
        const state = getState() as ItemViewState;
89
        const previousIndex = (state.selectedConcordance - 1 + state.concordances.length) % state.concordances.length;
90
        await dispatch(getItem(state.concordances[previousIndex].id));
91
        return previousIndex;
92
    }
93
);
69
export const setSelectedConcordance = (selectedConcordance: number) => {
70
    return {
71
      type: "item/setSelectedConcordance",
72
      payload: {
73
        selectedConcordance,
74
      },
75
    };
76
  };
77

  
78
  export const setConcordances = (concordances: Concordance[]) => {
79
    return {
80
      type: "item/setConcordances",
81
      payload: {
82
        concordances,
83
      },
84
    };
85
  };
94 86

  
95 87
// export const getItemConcordances = createAsyncThunk(
96 88
//     "item/getItemConcordances",
src/stores/reducers/itemSlice.ts
1 1
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
2
import { getItem, getItemNotes, getNextConcordance, getPreviousConcordance } from "../actions/itemThunks"
2
import { getItem, getItemNotes } from "../actions/itemThunks"
3 3
import { Certainty, ItemViewState, Item } from "../../types/item";
4 4

  
5 5

  
......
22 22
    name: "item",
23 23
    initialState: initialState,
24 24
    reducers: {
25
        setSelectedConcordance: (state, action) => {
26
            state.selectedConcordance = action.payload.selectedConcordance;
27
        },
28
        setConcordances: (state, action) => {
29
            state.concordances = action.payload.concordances;
30
        }
25 31
    },
26 32
    extraReducers: (builder) => {
27 33
        // getItem
28 34
        builder.addCase(getItem.fulfilled, (state, action) => {
29 35

  
30
            state.item.id= action.payload.id;
31
            state.item.authorDisplayName= action.payload.authorDisplayName;
32
            state.item.workName= action.payload.workName;
33
            state.item.concordances= action.payload.concordances;
34
            state.item.searchSubjects= action.payload.searchSubjects;
35
            state.item.inventoryItem= action.payload.inventoryItem;
36
            state.item.fullView= action.payload.fullView;
37
        
36
            state.item.id = action.payload.id;
37
            state.item.authorDisplayName = action.payload.authorDisplayName;
38
            state.item.workName = action.payload.workName;
39
            state.item.concordances = action.payload.concordances;
40
            state.item.searchSubjects = action.payload.searchSubjects;
41
            state.item.inventoryItem = action.payload.inventoryItem;
42
            state.item.fullView = action.payload.fullView;
43

  
38 44
            if (action.payload.fullView) {
39 45
                state.item.institution = action.payload.institution;
40 46
                state.item.authorName = action.payload.authorName;
......
44 50
                state.item.provenance = action.payload.provenance;
45 51
                state.item.description = action.payload.description;
46 52
            }
47
            else{
53
            else {
48 54
                state.item.institution = undefined;
49 55
                state.item.authorName = undefined;
50 56
                state.item.imageUrl = undefined;
......
53 59
                state.item.provenance = undefined;
54 60
                state.item.description = undefined;
55 61
            }
56
        
57
            // set concordances from item, if selected concordance is 0
58
            if (state.selectedConcordance === 0) {
59
                console.log("Tohle se vola!");
60
                state.concordances = action.payload.concordances;
61
            }
62 62

  
63
            // // set concordances from item, if selected concordance is 0
64
            // if (state.selectedConcordance === 0) {
65
            //     state.concordances = action.payload.concordances;
66
            // }
63 67
        })
64 68
        builder.addCase(getItem.rejected, (state, action) => {
65 69
            state.lastError = action.error.message
66 70
        })
67 71

  
68
        // getNextConcordance
69
        builder.addCase(getNextConcordance.fulfilled, (state, action) => {
70
            state.selectedConcordance = action.payload;
71
        });
72

  
73
        builder.addCase(getNextConcordance.rejected, (state, action) => {
74
            state.lastError = action.error.message;
75
        });
76

  
77
        // getPreviousConcordance
78
        builder.addCase(getPreviousConcordance.fulfilled, (state, action) => {
79
            state.selectedConcordance = action.payload;
80
        });
81

  
82
        builder.addCase(getPreviousConcordance.rejected, (state, action) => {
83
            state.lastError = action.error.message;
84
        });
85

  
86 72
    }
87 73
})
88 74

  
src/types/item.ts
2 2

  
3 3
export type  Item = {
4 4
    id: string
5
    
5

  
6 6
    concordances: Concordance[]
7 7

  
8 8
    authorDisplayName: string

Také k dispozici: Unified diff