Projekt

Obecné

Profil

Stáhnout (5.49 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 356c8bce Fantič
import { Certainty, Concordance } 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 356c8bce Fantič
                    concordances:  [{id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown}].concat(response.data.concordances),
34
                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name || null,
35
                    workName: response.data.object[0]?.caption || null,
36
                    inventoryItem: response.data.text || null,
37
                    searchSubjects: response.data.search_subject || null,
38
                    authorName,
39
                    imageUrl,
40
                    title,
41
                    institution,
42
                    repository,
43
                    provenance,
44
                    description
45 7c4cd2cd Fantič
                }
46
            }
47
            // data without image
48
            else if (response.status === 200 && response.data.object.length == 1) {
49
                return {
50 a7264b57 Fantič
                    id: itemId,
51 7c4cd2cd Fantič
                    fullView: false,
52 356c8bce Fantič
                    concordances: [{id: itemId, cert: Certainty.Unknown}].concat(response.data.concordances),
53
                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name || null,
54
                    workName: response.data.object[0]?.caption || null,
55
                    inventoryItem: response.data.text || null,
56
                    searchSubjects: response.data.search_subject || null,
57 97deff21 Fantič
                }
58 88d2df9a Fantič
            }
59
            else {
60 356c8bce Fantič
                console.log("Error " + response.data);
61 97deff21 Fantič
                return Promise.reject(response.data ? response.data : "Error")
62
            }
63
        } catch (err: any) {
64 356c8bce Fantič
            
65
            console.log(err);
66 97deff21 Fantič
            return Promise.reject(err.response.data)
67
        }
68
    }
69
)
70
71 c03fd43c Fantič
  export const setConcordances = (concordances: Concordance[]) => {
72
    return {
73
      type: "item/setConcordances",
74
      payload: {
75
        concordances,
76
      },
77
    };
78
  };
79 97deff21 Fantič
80
// export const getItemConcordances = createAsyncThunk(
81
//     "item/getItemConcordances",
82
//     async (itemId : string) => {
83
//         try {
84
//             const response = await getItemConcordancesRequest(itemId)
85
//             console.log(response)
86
//             if (response.status === 200) {
87
//                 return {
88
//                     // TODO set item concordances
89
//                 }
90
//             } else {
91
//                 return Promise.reject(response.data ? response.data : "Error")
92
//             }
93
//         } catch (err: any) {
94
//             return Promise.reject(err.response.data)
95
//         }
96
//     }
97
// )
98
99
export const getItemNotes = createAsyncThunk(
100
    "item/getItemNotes",
101
    async (itemId: string) => {
102
        try {
103 356c8bce Fantič
            console.log("GET notes/getNotes/" + itemId);
104 88d2df9a Fantič
105
            const response = await getItemNotesRequest(itemId);
106
107 97deff21 Fantič
            if (response.status === 200) {
108 88d2df9a Fantič
                if(response.data.length > 0){
109
110
                    let notes = [];
111
                    for(let i = 0; i < response.data.length; i++){
112
                        let note = response.data[i];
113
                        notes.push({
114
                            username: (note as any).created_by,
115
                            userId: (note as any).created_by_id,
116 b88520f8 Fantič
                            note: (note as any).note,
117 88d2df9a Fantič
                            avatarUrl: (note as any).avatar,
118
                            items: (note as any).items,
119
                            createdTime: (note as any).created, 
120
                            updatedTime: (note as any).updated,
121
                            noteColor: (note as any).note_color,
122
                        })
123
                    }
124 3a4bf532 Fantič
125 88d2df9a Fantič
                    return {
126
                        notes,
127
                    }
128
                }
129
                else{
130
                    // no notes for this item
131
                    return {
132
                        notes: [],
133
                    }
134 97deff21 Fantič
                }
135 88d2df9a Fantič
            }
136
            else {
137 97deff21 Fantič
                return Promise.reject(response.data ? response.data : "Error")
138
            }
139
        } catch (err: any) {
140
            return Promise.reject(err.response.data)
141
        }
142
    }
143
)