Projekt

Obecné

Profil

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