Skip to content

Commit 58bd787

Browse files
Displays author name in Merge UI (#9920)
* added get_author_names function * created enhanced records * added author name and css * Added enhancedRecord to merge function * remove from enhanced records * Update book path to search * throws new error * added comment * used try/catch * updated enhancedRecords to use for loop * add author name as td * Moved bold to css * Small style tweaks to author names in merge table * Fix undefined variable in AuthorRoleTable.vue * Handle cases where authors are missing in MergeTable
1 parent ae0da2e commit 58bd787

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

openlibrary/components/MergeUI/AuthorRoleTable.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
{{role[field].key.slice("/authors/".length)}}
2020
</a>
2121
</div>
22-
<div v-else>{{a[k]}}</div>
22+
<div v-else>{{role[field]}}</div>
2323
</div>
2424
</td>
2525
</tr>

openlibrary/components/MergeUI/MergeTable.vue

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</thead>
99
<tbody>
1010
<MergeRow
11-
v-for="record in records"
11+
v-for="record in enhancedRecords"
1212
:key="record.key"
1313
:record="record"
1414
:fields="fields"
@@ -47,7 +47,7 @@ import _ from 'lodash';
4747
import Vue from 'vue';
4848
import AsyncComputed from 'vue-async-computed';
4949
import MergeRow from './MergeRow.vue';
50-
import { merge, get_editions, get_lists, get_bookshelves, get_ratings } from './utils.js';
50+
import { merge, get_editions, get_lists, get_bookshelves, get_ratings, get_author_names } from './utils.js';
5151
import CONFIGS from '../configs.js';
5252
5353
Vue.use(AsyncComputed);
@@ -113,6 +113,28 @@ export default {
113113
return records;
114114
},
115115
116+
/** The records, with extra helpful metadata attached for display. Should NOT be saved to Open Library */
117+
async enhancedRecords(){
118+
if (!this.records) return null;
119+
120+
let author_names;
121+
122+
try {
123+
author_names = await get_author_names(this.records);
124+
} catch (error) {
125+
console.error('Error creating enhancedRecords:', error);
126+
}
127+
128+
const enhanced_records = _.cloneDeep(this.records)
129+
130+
for (const record of enhanced_records) {
131+
for (const entry of (record.authors || [])) {
132+
entry.name = author_names[entry.author.key.slice('/authors/'.length)];
133+
}
134+
}
135+
return enhanced_records
136+
},
137+
116138
async editions() {
117139
if (!this.records) return null;
118140
@@ -474,6 +496,10 @@ li.excerpt-item {
474496
}
475497
476498
.field-authors {
499+
td.author-author {
500+
padding-right: 6px;
501+
}
502+
477503
thead, td.author-index, td.author-type {
478504
display: none;
479505
}

openlibrary/components/MergeUI/utils.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,42 @@ function save_many(items, comment, action, data) {
230230
});
231231
}
232232

233+
/**
234+
* Fetches name associated with the author key
235+
* @param {Object[]} works
236+
* @returns {Promise<Record<string,object>} A response to the request
237+
*/
238+
export async function get_author_names(works) {
239+
const authorIds = _.uniq(works).flatMap(record =>
240+
(record.authors || []).map(authorEntry => authorEntry.author.key)
241+
)
242+
243+
if (!authorIds.length) return {};
244+
245+
const queryParams = new URLSearchParams({
246+
q: `key:(${authorIds.join(' OR ')})`,
247+
mode: 'everything',
248+
fields: 'key,name',
249+
})
250+
251+
const response = await fetch(`${CONFIGS.OL_BASE_SEARCH}/search/authors.json?${queryParams}`)
252+
253+
if (!response.ok) {
254+
throw new Error('Failed to fetch author data');
255+
}
256+
257+
const results = await response.json()
258+
259+
const authorDirectory = {}
260+
261+
for (const doc of results.docs) {
262+
authorDirectory[doc.key] = doc.name;
263+
}
264+
265+
return authorDirectory
266+
}
267+
268+
233269
// /**
234270
// * @param {Object} record
235271
// * @param {string} comment

0 commit comments

Comments
 (0)