Projekt

Obecné

Profil

Stáhnout (8.88 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 fb120216 Fantič
import { Certainty, Concordance, Item, ItemObject } 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 a27b3427 Fantič
15 7c4cd2cd Fantič
            // data with image
16
            if (response.status === 200 && response.data.object.length > 1) {
17 e0741b92 Fantič
                // TODO IMAGES to array :-)
18 c1161e82 Fantič
                const authorName = response.data?.object[1]?.name?.[0]?.getty_data?.display_name ?? undefined;
19 fb120216 Fantič
                const images : ItemObject[] = []
20 c1161e82 Fantič
                for(let i = 1; i < response.data.object.length; i++){
21 fb120216 Fantič
                    images.push({
22
                        imageUrl: response.data.object[i]?.images?.[0]?.file ?? undefined, 
23
                        title:  response.data.object[i]?.images?.[0]?.text ?? undefined, 
24
                        relationship_type: response.data.object[i].relationship_type ?? undefined, 
25
                        cert: response.data.object[i].cert ?? Certainty.Unknown ,
26
27
                        author: response.data?.object[i]?.name?.[0]?.getty_data?.display_name ?? undefined,
28
                        description: response.data?.object[i]?.physical_description ?? undefined,
29
                        institution: {
30
                            name: response.data?.object[i]?.institution ?? undefined,
31
                            inventoryNumber: response.data?.object[i]?.id_number ?? undefined,
32
                            country: response.data?.object[i]?.country ?? undefined,
33
                            city: response.data?.object[i]?.city ?? undefined
34
                        },
35
                        provenance: response.data?.object[i]?.provenance ?? undefined,
36
                        repository: response.data?.object[i]?.repository ?? undefined
37
                    })
38 e0741b92 Fantič
                }
39
                
40 356c8bce Fantič
                const institution = {
41 c1161e82 Fantič
                    name: response.data?.object[1]?.institution ?? undefined,
42
                    inventoryNumber: response.data?.object[1]?.id_number ?? undefined,
43
                    country: response.data?.object[1]?.country ?? undefined,
44
                    city: response.data?.object[1]?.city ?? undefined
45 356c8bce Fantič
                };
46 c1161e82 Fantič
                const repository = response.data?.object[1]?.repository ?? undefined;
47
                const provenance = response.data?.object[1]?.provenance ?? undefined;
48
                const description = response.data?.object[1]?.physical_description ?? undefined;
49 a27b3427 Fantič
50 97deff21 Fantič
                return {
51 a7264b57 Fantič
                    id: itemId,
52 7c4cd2cd Fantič
                    fullView: true,
53 72afb023 Fantič
                    concordances: response.data.concordances ?
54 c1161e82 Fantič
                        [{ id: itemId, cert: response.data?.object[1]?.cert ?? Certainty.Unknown }].concat(response.data.concordances)
55 a27b3427 Fantič
                        :
56 c1161e82 Fantič
                        [{ id: itemId, cert: response.data?.object[1]?.cert ?? Certainty.Unknown }],
57
                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name ?? undefined,
58
                    workName: response.data.object[0]?.caption ?? undefined,
59
                    inventoryItem: response.data.text ?? undefined,
60
                    searchSubjects: response.data.search_subject ?? undefined,
61 8ff3394e Fantič
                    prevItem: response.data.prevItem ?? undefined,
62
                    nextItem: response.data.nextItem ?? undefined,
63 f4af30c8 Fantič
                    room: response.data.room ?? undefined,
64 356c8bce Fantič
                    authorName,
65 e0741b92 Fantič
                    images,
66 356c8bce Fantič
                    institution,
67
                    repository,
68
                    provenance,
69
                    description
70 7c4cd2cd Fantič
                }
71
            }
72
            // data without image
73
            else if (response.status === 200 && response.data.object.length == 1) {
74
                return {
75 a7264b57 Fantič
                    id: itemId,
76 7c4cd2cd Fantič
                    fullView: false,
77 72afb023 Fantič
                    concordances: response.data.concordances ?
78 c1161e82 Fantič
                        [{ id: itemId, cert: response.data?.object[1]?.cert ?? Certainty.Unknown }].concat(response.data.concordances)
79 a27b3427 Fantič
                        :
80 c1161e82 Fantič
                        [{ id: itemId, cert: response.data?.object[1]?.cert ?? Certainty.Unknown }],
81
                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name ?? undefined,
82
                    workName: response.data.object[0]?.caption ?? undefined,
83 8ff3394e Fantič
                    prevItem: response.data.prevItem ?? undefined,
84
                    nextItem: response.data.nextItem ?? undefined,
85 c1161e82 Fantič
                    inventoryItem: response.data.text ?? undefined,
86
                    searchSubjects: response.data.search_subject ?? undefined,
87 f4af30c8 Fantič
                    room: response.data.room ?? undefined,
88 97deff21 Fantič
                }
89 88d2df9a Fantič
            }
90
            else {
91 356c8bce Fantič
                console.log("Error " + response.data);
92 97deff21 Fantič
                return Promise.reject(response.data ? response.data : "Error")
93
            }
94
        } catch (err: any) {
95 a27b3427 Fantič
96 356c8bce Fantič
            console.log(err);
97 97deff21 Fantič
            return Promise.reject(err.response.data)
98
        }
99
    }
100
)
101
102 a27b3427 Fantič
export const setConcordances = (concordances: Concordance[]) => {
103 c03fd43c Fantič
    return {
104 a27b3427 Fantič
        type: "item/setConcordances",
105
        payload: {
106
            concordances,
107
        },
108 c03fd43c Fantič
    };
109 a27b3427 Fantič
};
110 97deff21 Fantič
111
// export const getItemConcordances = createAsyncThunk(
112
//     "item/getItemConcordances",
113
//     async (itemId : string) => {
114
//         try {
115
//             const response = await getItemConcordancesRequest(itemId)
116
//             console.log(response)
117
//             if (response.status === 200) {
118
//                 return {
119
//                     // TODO set item concordances
120
//                 }
121
//             } else {
122
//                 return Promise.reject(response.data ? response.data : "Error")
123
//             }
124
//         } catch (err: any) {
125
//             return Promise.reject(err.response.data)
126
//         }
127
//     }
128
// )
129
130
export const getItemNotes = createAsyncThunk(
131
    "item/getItemNotes",
132 a27b3427 Fantič
    async ({ item, relatedComments }: { item: Item, relatedComments: boolean }) => {
133 97deff21 Fantič
        try {
134 d3c12593 Fantič
            console.log("GET notes/getNotes/" + item.id);
135 88d2df9a Fantič
136 d3c12593 Fantič
            const response = await getItemNotesRequest(item, relatedComments);
137 88d2df9a Fantič
138 97deff21 Fantič
            if (response.status === 200) {
139 c1161e82 Fantič
                if (response.data && response.data.length > 0) {
140 88d2df9a Fantič
141
                    let notes = [];
142 a27b3427 Fantič
                    for (let i = 0; i < response.data.length; i++) {
143 88d2df9a Fantič
                        let note = response.data[i];
144 a27b3427 Fantič
                        let replies : any = undefined
145
146
                        // convert replies
147
                        if(note.replies){
148
                            replies = [];
149
                            for(let i = 0; i < note.replies.length; i++){
150
                                const reply = note.replies[i];
151
                                replies.push({
152
                                    uuid: (reply as any).uuid,
153
                                    username: (reply as any).created_by,
154
                                    userId: (reply as any).created_by_id,
155
                                    note: (reply as any).note,
156
                                    avatarUrl: (reply as any).avatar,
157
                                    items: (reply as any).items,
158
                                    createdTime: (reply as any).created,
159
                                    updatedTime: (reply as any).updated,
160
                                    noteColor: (reply as any).note_color,
161
                                    reply_to: note.uuid
162
                                })
163
                            }
164
                        }
165
166 88d2df9a Fantič
                        notes.push({
167 bb690a9a Fantič
                            uuid: (note as any).uuid,
168 88d2df9a Fantič
                            username: (note as any).created_by,
169
                            userId: (note as any).created_by_id,
170 b88520f8 Fantič
                            note: (note as any).note,
171 88d2df9a Fantič
                            avatarUrl: (note as any).avatar,
172
                            items: (note as any).items,
173 a27b3427 Fantič
                            replies: replies,
174
                            createdTime: (note as any).created,
175 88d2df9a Fantič
                            updatedTime: (note as any).updated,
176
                            noteColor: (note as any).note_color,
177 3a226033 Fantič
                            reply_to: (note as any).reply_to
178 88d2df9a Fantič
                        })
179
                    }
180 3a4bf532 Fantič
181 88d2df9a Fantič
                    return {
182
                        notes,
183
                    }
184
                }
185 a27b3427 Fantič
                else {
186 88d2df9a Fantič
                    // no notes for this item
187
                    return {
188
                        notes: [],
189
                    }
190 97deff21 Fantič
                }
191 88d2df9a Fantič
            }
192
            else {
193 97deff21 Fantič
                return Promise.reject(response.data ? response.data : "Error")
194
            }
195
        } catch (err: any) {
196
            return Promise.reject(err.response.data)
197
        }
198
    }
199
)