Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displays author name on merge #9920

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions openlibrary/components/MergeUI/AuthorRoleTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
{{role[field].key.slice("/authors/".length)}}
</a>
</div>
<div v-else-if="field == 'name'">
{{role[field]}}
</div>
<div v-else>{{a[k]}}</div>
</div>
</td>
Expand Down
36 changes: 34 additions & 2 deletions openlibrary/components/MergeUI/MergeTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</thead>
<tbody>
<MergeRow
v-for="record in records"
v-for="record in enhancedRecords"
:key="record.key"
:record="record"
:fields="fields"
Expand Down Expand Up @@ -47,7 +47,7 @@ import _ from 'lodash';
import Vue from 'vue';
import AsyncComputed from 'vue-async-computed';
import MergeRow from './MergeRow.vue';
import { merge, get_editions, get_lists, get_bookshelves, get_ratings } from './utils.js';
import { merge, get_editions, get_lists, get_bookshelves, get_ratings, get_author_names } from './utils.js';
import CONFIGS from '../configs.js';

Vue.use(AsyncComputed);
Expand Down Expand Up @@ -113,6 +113,28 @@ export default {
return records;
},

/** The records, with extra helpful metadata attached for display. Should NOT be saved to Open Library */
async enhancedRecords(){
Copy link
Collaborator

@cdrini cdrini Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid someone in the future accidentally saving!

Suggested change
async enhancedRecords(){
/** The records, with extra helpful metadata attached for display. Should NOT be saved to Open Library */
async enhancedRecords(){

if (!this.records) return null;

let author_names;

try {
author_names = await get_author_names(this.records);
} catch (error) {
console.error('Error creating enhancedRecords:', error);
}

const enhanced_records = _.cloneDeep(this.records)

for (const record of enhanced_records) {
for (const entry of record.authors) {
entry.name = author_names[entry.author.key.slice('/authors/'.length)];
}
}
return enhanced_records
},

async editions() {
if (!this.records) return null;

Expand Down Expand Up @@ -474,6 +496,16 @@ li.excerpt-item {
}

.field-authors {
td.author-author {
display: inline-block;
padding-right: 2em;
}

td.author-name {
display: inline-block;
font-weight: bold;
}

thead, td.author-index, td.author-type {
display: none;
}
Expand Down
34 changes: 34 additions & 0 deletions openlibrary/components/MergeUI/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,40 @@ function save_many(items, comment, action, data) {
});
}

/**
* Fetches name associated with the author key
* @param {Object[]} works
* @returns {Promise<Record<string,object>} A response to the request
*/
export async function get_author_names(works) {
const authorIds = _.uniq(works).flatMap(record =>
record.authors.map(authorEntry => authorEntry.author.key)
)

const queryParams = new URLSearchParams({
q: `key:(${authorIds.join(' OR ')})`,
mode: 'everything',
fields: 'key,name',
})

const response = await fetch(`${CONFIGS.OL_BASE_SEARCH}/search/authors.json?${queryParams}`)

if (!response.ok) {
throw new Error('Failed to fetch author data');
}

const results = await response.json()

const authorDirectory = {}

for (const doc of results.docs) {
authorDirectory[doc.key] = doc.name;
}

return authorDirectory
}


// /**
// * @param {Object} record
// * @param {string} comment
Expand Down