-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webui): prefill author selection when bulk editing books
- Loading branch information
Showing
3 changed files
with
57 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,26 @@ | ||
import {groupBy, mapValues} from 'lodash' | ||
import {AuthorDto} from '@/types/komga-books' | ||
import {flatMap, groupBy, intersection, mapValues, reduce, uniq} from 'lodash' | ||
import {AuthorDto, BookDto} from '@/types/komga-books' | ||
|
||
type AuthorsByRole = {[role: string]: string[]} | ||
|
||
// return an object where keys are roles, and values are string[] | ||
export function groupAuthorsByRole (authors: AuthorDto[]): any { | ||
export function groupAuthorsByRole (authors: AuthorDto[]): AuthorsByRole { | ||
return mapValues(groupBy(authors, 'role'), | ||
authors => authors.map((author: AuthorDto) => author.name)) | ||
} | ||
|
||
// create an object where keys are roles and values are arrays of authors | ||
// we're using intersection to only include authors that are present on all books of each roles | ||
export function buildManyAuthorsByRole(books: BookDto[]): AuthorsByRole { | ||
const allAuthorsByRoles = books.map(book => groupAuthorsByRole(book.metadata.authors)) | ||
const roleKeys = uniq(flatMap(allAuthorsByRoles, Object.keys)) | ||
return reduce(roleKeys, (acc: {[key: string]: string[]}, key) => { | ||
const values = allAuthorsByRoles.map(authorsByRole => authorsByRole[key] || []) | ||
const intersect = intersection(...values.filter(arr => arr.length > 0)) | ||
|
||
if (intersect.length > 0) { | ||
acc[key] = intersect | ||
} | ||
return acc | ||
}, {}) | ||
} |