Commit 2b26b297 authored by Mycha de Vrees's avatar Mycha de Vrees
Browse files

refs #3 more abstract stuff

parent c336f5aa
Pipeline #356 passed with stage
in 1 minute and 20 seconds
......@@ -123,7 +123,7 @@ const actions = {
const newCtx = CtxTransformer.transformCtx(ctx)
const params = { contentType: 'application/json', bookId: id, ...newCtx }
const response = await getAuthors(params)
const authors = BookTransformer.fetchBookAuthors(response)
const authors = BookTransformer.mapResponse(response)
commit(BOOK_FETCH_AUTHORS_SUCCESS, authors)
} catch (error) {
notifyError({vm, error})
......
......@@ -75,20 +75,14 @@ function fetchMultipleBooks (response) {
* @returns {Object}
*/
function fetchSingleBook (response) {
const authors = []
const book = response.data.data
const obj = {
id: response.data.data.id,
...response.data.data.attributes
id: book.id,
...book.attributes
}
if (Object.hasOwnProperty.call(book.relationships, 'authors') && book.relationships.authors.data.length > 0) {
obj.authors = _fetchBooksIncludes({book: book, response, type: 'authors', typeAlias: 'author', manytomany: true})
}
response.data.data.relationships.authors.data.map((author) => {
const foundAuthor = _.find(response.data.included, (included) => included.type === 'author' && included.id === author.id)
const authorObj = {
id: foundAuthor.id,
...foundAuthor.attributes
}
authors.push(authorObj)
obj.authors = authors
})
return obj
}
......@@ -110,25 +104,6 @@ class BookTransformer extends Transformer {
// or just fetch a single book.
return fetchSingleBook(response)
}
/**
* Fetch book author
* @param response
* @returns {Array}
*/
static fetchBookAuthors (response) {
const authors = []
if (Array.isArray(response.data.data)) {
response.data.data.map((author) => {
const obj = {
id: author.id,
title: author.attributes.name
}
authors.push(obj)
})
}
return authors
}
}
export default BookTransformer
function _mapResponse (response) {
const newArr = []
if (Array.isArray(response.data.data)) {
response.data.data.map((include) => {
const obj = {
id: include.id,
title: include.attributes.name
}
newArr.push(obj)
})
}
return newArr
}
/**
* Transformer class
* @author Mycha de Vrees
......@@ -20,6 +34,10 @@ class Transformer {
}
}
}
static mapResponse (response) {
return _mapResponse(response)
}
}
export default Transformer
......@@ -19,7 +19,7 @@ describe('BookTransformer fetch singlebody', () => {
describe('BookTrasnformer fetch authors', () => {
const authors = require('./BookTransformer/fixtures/authors.json')
const transformAuthors = BookTransformer.fetchBookAuthors(authors)
const transformAuthors = BookTransformer.mapResponse(authors)
it('should contain the correct data format', () => {
expect(transformAuthors[0].title).toEqual('Jerker Eriksson')
})
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment