Projekt

Obecné

Profil

Stáhnout (5.97 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 } from "../../types/item";
5

    
6

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

    
13
            const response = await getItemRequest(itemId);
14
        
15
            // data with image
16
            if (response.status === 200 && response.data.object.length > 1) {
17
                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
                return {
31
                    id: itemId,
32
                    fullView: true,
33
                    concordances: response.data.concordances ?
34
                         [{id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown}].concat(response.data.concordances)
35
                         :
36
                         [{id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown}],
37
                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name || null,
38
                    workName: response.data.object[0]?.caption || null,
39
                    inventoryItem: response.data.text || null,
40
                    searchSubjects: response.data.search_subject || null,
41
                    authorName,
42
                    imageUrl,
43
                    title,
44
                    institution,
45
                    repository,
46
                    provenance,
47
                    description
48
                }
49
            }
50
            // data without image
51
            else if (response.status === 200 && response.data.object.length == 1) {
52
                return {
53
                    id: itemId,
54
                    fullView: false,
55
                    concordances: response.data.concordances ?
56
                    [{id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown}].concat(response.data.concordances)
57
                    :
58
                    [{id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown}],
59
                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name || null,
60
                    workName: response.data.object[0]?.caption || null,
61
                    inventoryItem: response.data.text || null,
62
                    searchSubjects: response.data.search_subject || null,
63
                }
64
            }
65
            else {
66
                console.log("Error " + response.data);
67
                return Promise.reject(response.data ? response.data : "Error")
68
            }
69
        } catch (err: any) {
70
            
71
            console.log(err);
72
            return Promise.reject(err.response.data)
73
        }
74
    }
75
)
76

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

    
86
// export const getItemConcordances = createAsyncThunk(
87
//     "item/getItemConcordances",
88
//     async (itemId : string) => {
89
//         try {
90
//             const response = await getItemConcordancesRequest(itemId)
91
//             console.log(response)
92
//             if (response.status === 200) {
93
//                 return {
94
//                     // TODO set item concordances
95
//                 }
96
//             } else {
97
//                 return Promise.reject(response.data ? response.data : "Error")
98
//             }
99
//         } catch (err: any) {
100
//             return Promise.reject(err.response.data)
101
//         }
102
//     }
103
// )
104

    
105
export const getItemNotes = createAsyncThunk(
106
    "item/getItemNotes",
107
    async (itemId: string) => {
108
        try {
109
            console.log("GET notes/getNotes/" + itemId);
110

    
111
            const response = await getItemNotesRequest(itemId);
112

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

    
116
                    let notes = [];
117
                    for(let i = 0; i < response.data.length; i++){
118
                        let note = response.data[i];
119
                        notes.push({
120
                            uuid: (note as any).uuid,
121
                            username: (note as any).created_by,
122
                            userId: (note as any).created_by_id,
123
                            note: (note as any).note,
124
                            avatarUrl: (note as any).avatar,
125
                            items: (note as any).items,
126
                            createdTime: (note as any).created, 
127
                            updatedTime: (note as any).updated,
128
                            noteColor: (note as any).note_color,
129
                            reply_to: (note as any).reply_to
130
                        })
131
                    }
132

    
133
                    return {
134
                        notes,
135
                    }
136
                }
137
                else{
138
                    // no notes for this item
139
                    return {
140
                        notes: [],
141
                    }
142
                }
143
            }
144
            else {
145
                return Promise.reject(response.data ? response.data : "Error")
146
            }
147
        } catch (err: any) {
148
            return Promise.reject(err.response.data)
149
        }
150
    }
151
)
(1-1/5)