Projekt

Obecné

Profil

Stáhnout (5.49 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:  [{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
                }
46
            }
47
            // data without image
48
            else if (response.status === 200 && response.data.object.length == 1) {
49
                return {
50
                    id: itemId,
51
                    fullView: false,
52
                    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
                }
58
            }
59
            else {
60
                console.log("Error " + response.data);
61
                return Promise.reject(response.data ? response.data : "Error")
62
            }
63
        } catch (err: any) {
64
            
65
            console.log(err);
66
            return Promise.reject(err.response.data)
67
        }
68
    }
69
)
70

    
71
  export const setConcordances = (concordances: Concordance[]) => {
72
    return {
73
      type: "item/setConcordances",
74
      payload: {
75
        concordances,
76
      },
77
    };
78
  };
79

    
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
            console.log("GET notes/getNotes/" + itemId);
104

    
105
            const response = await getItemNotesRequest(itemId);
106

    
107
            if (response.status === 200) {
108
                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
                            note: (note as any).note,
117
                            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

    
125
                    return {
126
                        notes,
127
                    }
128
                }
129
                else{
130
                    // no notes for this item
131
                    return {
132
                        notes: [],
133
                    }
134
                }
135
            }
136
            else {
137
                return Promise.reject(response.data ? response.data : "Error")
138
            }
139
        } catch (err: any) {
140
            return Promise.reject(err.response.data)
141
        }
142
    }
143
)
(1-1/4)