Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
0x43
eBookHub-frontend
Commits
2b26b297
Commit
2b26b297
authored
Apr 12, 2019
by
Mycha de Vrees
Browse files
refs
#3
more abstract stuff
parent
c336f5aa
Pipeline
#356
passed with stage
in 1 minute and 20 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/store/modules/book.js
View file @
2b26b297
...
...
@@ -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
})
...
...
src/transformers/BookTransformer.js
View file @
2b26b297
...
...
@@ -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
src/transformers/Transformer.js
View file @
2b26b297
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
test/unit/specs/BookTransformer.spec.js
View file @
2b26b297
...
...
@@ -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
'
)
})
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment