Projekt

Obecné

Profil

Stáhnout (6.04 KB) Statistiky
| Větev: | Tag: | Revize:
1 97deff21 Fantič
import { createAsyncThunk } from "@reduxjs/toolkit"
2
import { getItemRequest } from "../../api/itemservice"
3
import { getItemNotesRequest } from "../../api/notesservice"
4 d3c12593 Fantič
import { Certainty, Concordance, Item } from "../../types/item";
5 7c4cd2cd Fantič
6 97deff21 Fantič
7
export const getItem = createAsyncThunk(
8
    "item/getItem",
9
    async (itemId: string) => {
10
        try {
11 3a4bf532 Fantič
            console.log("GET item/getItem/" + itemId);
12 7c4cd2cd Fantič
13 88d2df9a Fantič
            const response = await getItemRequest(itemId);
14 3a4bf532 Fantič
        
15 7c4cd2cd Fantič
            // data with image
16
            if (response.status === 200 && response.data.object.length > 1) {
17 356c8bce Fantič
                const authorName = response.data.object[1]?.name?.[0]?.getty_data?.display_name || null;
18
                const imageUrl = response.data.object[1]?.images?.[0]?.file || null;
19
                const title = response.data.object[1]?.images?.[0]?.text || null;
20
                const institution = {
21
                    name: response.data.object[1]?.institution || null,
22
                    inventoryNumber: response.data.object[1]?.id_number || null,
23
                    country: response.data.object[1]?.country || null,
24
                    city: response.data.object[1]?.city || null
25
                };
26
                const repository = response.data.object[1]?.repository || null;
27
                const provenance = response.data.object[1]?.provenance || null;
28
                const description = response.data.object[1]?.physical_description || null;
29
                
30 97deff21 Fantič
                return {
31 a7264b57 Fantič
                    id: itemId,
32 7c4cd2cd Fantič
                    fullView: true,
33 72afb023 Fantič
                    concordances: response.data.concordances ?
34
                         [{id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown}].concat(response.data.concordances)
35
                         :
36
                         [{id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown}],
37 356c8bce Fantič
                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name || null,
38
                    workName: response.data.object[0]?.caption || null,
39
                    inventoryItem: response.data.text || null,
40
                    searchSubjects: response.data.search_subject || null,
41
                    authorName,
42
                    imageUrl,
43
                    title,
44
                    institution,
45
                    repository,
46
                    provenance,
47
                    description
48 7c4cd2cd Fantič
                }
49
            }
50
            // data without image
51
            else if (response.status === 200 && response.data.object.length == 1) {
52
                return {
53 a7264b57 Fantič
                    id: itemId,
54 7c4cd2cd Fantič
                    fullView: false,
55 72afb023 Fantič
                    concordances: response.data.concordances ?
56
                    [{id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown}].concat(response.data.concordances)
57
                    :
58
                    [{id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown}],
59 356c8bce Fantič
                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name || null,
60
                    workName: response.data.object[0]?.caption || null,
61
                    inventoryItem: response.data.text || null,
62
                    searchSubjects: response.data.search_subject || null,
63 97deff21 Fantič
                }
64 88d2df9a Fantič
            }
65
            else {
66 356c8bce Fantič
                console.log("Error " + response.data);
67 97deff21 Fantič
                return Promise.reject(response.data ? response.data : "Error")
68
            }
69
        } catch (err: any) {
70 356c8bce Fantič
            
71
            console.log(err);
72 97deff21 Fantič
            return Promise.reject(err.response.data)
73
        }
74
    }
75
)
76
77 c03fd43c Fantič
  export const setConcordances = (concordances: Concordance[]) => {
78
    return {
79
      type: "item/setConcordances",
80
      payload: {
81
        concordances,
82
      },
83
    };
84
  };
85 97deff21 Fantič
86
// export const getItemConcordances = createAsyncThunk(
87
//     "item/getItemConcordances",
88
//     async (itemId : string) => {
89
//         try {
90
//             const response = await getItemConcordancesRequest(itemId)
91
//             console.log(response)
92
//             if (response.status === 200) {
93
//                 return {
94
//                     // TODO set item concordances
95
//                 }
96
//             } else {
97
//                 return Promise.reject(response.data ? response.data : "Error")
98
//             }
99
//         } catch (err: any) {
100
//             return Promise.reject(err.response.data)
101
//         }
102
//     }
103
// )
104
105
export const getItemNotes = createAsyncThunk(
106
    "item/getItemNotes",
107 d3c12593 Fantič
    async ({item, relatedComments}: {item: Item, relatedComments: boolean}) => {
108 97deff21 Fantič
        try {
109 d3c12593 Fantič
            console.log("GET notes/getNotes/" + item.id);
110 88d2df9a Fantič
111 d3c12593 Fantič
            const response = await getItemNotesRequest(item, relatedComments);
112 88d2df9a Fantič
113 97deff21 Fantič
            if (response.status === 200) {
114 88d2df9a Fantič
                if(response.data.length > 0){
115
116
                    let notes = [];
117
                    for(let i = 0; i < response.data.length; i++){
118
                        let note = response.data[i];
119
                        notes.push({
120 bb690a9a Fantič
                            uuid: (note as any).uuid,
121 88d2df9a Fantič
                            username: (note as any).created_by,
122
                            userId: (note as any).created_by_id,
123 b88520f8 Fantič
                            note: (note as any).note,
124 88d2df9a Fantič
                            avatarUrl: (note as any).avatar,
125
                            items: (note as any).items,
126
                            createdTime: (note as any).created, 
127
                            updatedTime: (note as any).updated,
128
                            noteColor: (note as any).note_color,
129 3a226033 Fantič
                            reply_to: (note as any).reply_to
130 88d2df9a Fantič
                        })
131
                    }
132 3a4bf532 Fantič
133 88d2df9a Fantič
                    return {
134
                        notes,
135
                    }
136
                }
137
                else{
138
                    // no notes for this item
139
                    return {
140
                        notes: [],
141
                    }
142 97deff21 Fantič
                }
143 88d2df9a Fantič
            }
144
            else {
145 97deff21 Fantič
                return Promise.reject(response.data ? response.data : "Error")
146
            }
147
        } catch (err: any) {
148
            return Promise.reject(err.response.data)
149
        }
150
    }
151
)