Projekt

Obecné

Profil

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