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