Projekt

Obecné

Profil

Stáhnout (7.24 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { createAsyncThunk } from "@reduxjs/toolkit"
2
import { getItemRequest } from "../../api/itemservice"
3
import { getItemNotesRequest } from "../../api/notesservice"
4
import { Certainty, Concordance, Item } from "../../types/item";
5
import { Note } from "../../types/note";
6

    
7

    
8
export const getItem = createAsyncThunk(
9
    "item/getItem",
10
    async (itemId: string) => {
11
        try {
12
            console.log("GET item/getItem/" + itemId);
13

    
14
            const response = await getItemRequest(itemId);
15

    
16
            // data with image
17
            if (response.status === 200 && response.data.object.length > 1) {
18
                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

    
31
                return {
32
                    id: itemId,
33
                    fullView: true,
34
                    concordances: response.data.concordances ?
35
                        [{ 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
                    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
                }
50
            }
51
            // data without image
52
            else if (response.status === 200 && response.data.object.length == 1) {
53
                return {
54
                    id: itemId,
55
                    fullView: false,
56
                    concordances: response.data.concordances ?
57
                        [{ 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
                    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
                }
65
            }
66
            else {
67
                console.log("Error " + response.data);
68
                return Promise.reject(response.data ? response.data : "Error")
69
            }
70
        } catch (err: any) {
71

    
72
            console.log(err);
73
            return Promise.reject(err.response.data)
74
        }
75
    }
76
)
77

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

    
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
    async ({ item, relatedComments }: { item: Item, relatedComments: boolean }) => {
109
        try {
110
            console.log("GET notes/getNotes/" + item.id);
111

    
112
            const response = await getItemNotesRequest(item, relatedComments);
113

    
114
            if (response.status === 200) {
115
                if (response.data.length > 0) {
116

    
117
                    let notes = [];
118
                    for (let i = 0; i < response.data.length; i++) {
119
                        let note = response.data[i];
120
                        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
                        notes.push({
143
                            uuid: (note as any).uuid,
144
                            username: (note as any).created_by,
145
                            userId: (note as any).created_by_id,
146
                            note: (note as any).note,
147
                            avatarUrl: (note as any).avatar,
148
                            items: (note as any).items,
149
                            replies: replies,
150
                            createdTime: (note as any).created,
151
                            updatedTime: (note as any).updated,
152
                            noteColor: (note as any).note_color,
153
                            reply_to: (note as any).reply_to
154
                        })
155
                    }
156

    
157
                    return {
158
                        notes,
159
                    }
160
                }
161
                else {
162
                    // no notes for this item
163
                    return {
164
                        notes: [],
165
                    }
166
                }
167
            }
168
            else {
169
                return Promise.reject(response.data ? response.data : "Error")
170
            }
171
        } catch (err: any) {
172
            return Promise.reject(err.response.data)
173
        }
174
    }
175
)
(2-2/6)