-
-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webui): build author on reset when bulk editing
- Loading branch information
Showing
2 changed files
with
57 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,42 @@ | ||
import {groupBy, mapValues} from 'lodash' | ||
import {AuthorDto} from '@/types/komga-books' | ||
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 | ||
// if the authors in a given roles is different between books, the value is an empty array. | ||
export function buildManyAuthorsByRole(books: BookDto[]): AuthorsByRole { | ||
const authorsByRole = groupAuthorsByRole(books[0].metadata.authors) | ||
const ignoreRoles = [] as string[] | ||
|
||
for (const book of books) { | ||
const bookAuthorsByRole = groupAuthorsByRole(book.metadata.authors) | ||
|
||
for (const role in bookAuthorsByRole) { | ||
if (ignoreRoles.includes(role)) { | ||
continue | ||
} | ||
const authors = authorsByRole[role] || [] | ||
const currentAuthors = bookAuthorsByRole[role] || [] | ||
if (authors.length === 0) { | ||
authorsByRole[role] = currentAuthors | ||
continue | ||
} | ||
// check if the authors are different | ||
// sort the arrays to make sure we compare the same authors | ||
if (currentAuthors && authors.sort().join() !== currentAuthors.sort().join()) { | ||
// remove the role from the authorsByRole | ||
authorsByRole[role] = [] | ||
ignoreRoles.push(role) | ||
} | ||
} | ||
} | ||
|
||
return authorsByRole | ||
} |