Commit 856b857b authored by Jure's avatar Jure

Merge branch '9-collection-teams' into 'master'

Add actions and reducers for getting the teams of a collection from the API

Closes #9

See merge request !112
parents 2b932bed c4234cfd
Pipeline #2625 passed with stage
in 1 minute and 39 seconds
......@@ -49,6 +49,40 @@ export function getCollections (options) {
}
}
function getCollectionTeamsRequest () {
return {
type: T.GET_COLLECTION_TEAMS_REQUEST
}
}
function getCollectionTeamsFailure (error) {
return {
type: T.GET_COLLECTION_TEAMS_FAILURE,
error: error
}
}
function getCollectionTeamsSuccess (teams) {
return {
type: T.GET_COLLECTION_TEAMS_SUCCESS,
teams,
receivedAt: Date.now()
}
}
export function getCollectionTeams (collection) {
return dispatch => {
dispatch(getCollectionTeamsRequest())
let url = collectionUrl(collection, 'teams')
return api.get(url).then(
teams => dispatch(getCollectionTeamsSuccess(teams)),
err => dispatch(getCollectionTeamsFailure(err))
)
}
}
function createCollectionRequest (collection) {
return {
type: T.CREATE_COLLECTION_REQUEST,
......
......@@ -12,6 +12,10 @@ export const GET_COLLECTION_REQUEST = 'GET_COLLECTION_REQUEST'
export const GET_COLLECTION_SUCCESS = 'GET_COLLECTION_SUCCESS'
export const GET_COLLECTION_FAILURE = 'GET_COLLECTION_FAILURE'
export const GET_COLLECTION_TEAMS_REQUEST = 'GET_COLLECTION_TEAMS_REQUEST'
export const GET_COLLECTION_TEAMS_SUCCESS = 'GET_COLLECTION_TEAMS_SUCCESS'
export const GET_COLLECTION_TEAMS_FAILURE = 'GET_COLLECTION_TEAMS_FAILURE'
export const UPDATE_COLLECTION_REQUEST = 'UPDATE_COLLECTION_REQUEST'
export const UPDATE_COLLECTION_SUCCESS = 'UPDATE_COLLECTION_SUCCESS'
export const UPDATE_COLLECTION_FAILURE = 'UPDATE_COLLECTION_FAILURE'
......
......@@ -3,12 +3,14 @@ import {
CREATE_TEAM_SUCCESS,
UPDATE_TEAM_SUCCESS,
DELETE_TEAM_SUCCESS,
GET_COLLECTION_TEAMS_SUCCESS,
LOGOUT_SUCCESS
} from '../actions/types'
import clone from 'lodash/clone'
import findIndex from 'lodash/findIndex'
import difference from 'lodash/difference'
import unionBy from 'lodash/unionBy'
export default function (state = [], action) {
const teams = clone(state)
......@@ -35,6 +37,7 @@ export default function (state = [], action) {
case GET_TEAMS_SUCCESS: return clone(action.teams)
case DELETE_TEAM_SUCCESS: return removeTeams()
case LOGOUT_SUCCESS: return []
case GET_COLLECTION_TEAMS_SUCCESS: return unionBy(state, action.teams, 'id')
}
return state
......
......@@ -40,6 +40,20 @@ describe('Collection actions', () => {
expect(filteredCollection).not.toHaveProperty('created')
})
describeAction('getCollectionTeams', {
firstarg: {id: 123},
types: {
request: T.GET_COLLECTION_TEAMS_REQUEST,
success: T.GET_COLLECTION_TEAMS_SUCCESS,
failure: T.GET_COLLECTION_TEAMS_FAILURE
},
properties: {
request: ['type'],
success: ['type', 'teams', 'receivedAt'],
failure: ['type', 'error']
}
})
let newcol
describeAction('createCollection', {
......@@ -57,8 +71,6 @@ describe('Collection actions', () => {
success: ['type', 'collection'],
failure: ['type', 'isFetching', 'collection', 'error']
}
}, (action, data) => {
newcol = data.CREATE_COLLECTION_SUCCESS.collection
})
describeAction('getCollection', {
......
......@@ -52,6 +52,7 @@ const describeAction = actions => (key, opts, cb) => {
const data = {}
let action
// TODO this is broken because failures in the callback are not reported
if (cb) afterAll(() => cb(action, data))
beforeEach(mockApi)
......@@ -105,12 +106,7 @@ const describeAction = actions => (key, opts, cb) => {
fetcher(dispatch, mockGetState)
expect(dispatched).toBeTruthy()
expect(
dispatched.type
).toBe(
opts.types.request,
`Received dispatched object with wrong type: \n${JSON.stringify(dispatched, null, 2)}`
)
expect(dispatched.type).toBe(opts.types.request)
if (properties) {
expect(dispatched).toHaveProperties(properties)
}
......@@ -133,10 +129,7 @@ const describeAction = actions => (key, opts, cb) => {
)
expect(dispatched).toBeTruthy()
expect(dispatched.type).toBe(
opts.types.success,
`Received dispatched object with wrong type: \n${JSON.stringify(dispatched, null, 2)}`
)
expect(dispatched.type).toBe(opts.types.success)
if (properties) {
expect(dispatched).toHaveProperties(properties)
}
......@@ -161,10 +154,7 @@ const describeAction = actions => (key, opts, cb) => {
)
expect(dispatched).toBeTruthy()
expect(dispatched.type).toBe(
opts.types.failure,
`Received dispatched object with wrong type: \n${JSON.stringify(dispatched, null, 2)}`
)
expect(dispatched.type).toBe(opts.types.failure)
if (properties) {
expect(dispatched).toHaveProperties(properties)
}
......
......@@ -51,6 +51,15 @@ describe('teams reducers', () => {
expect(actual).toEqual([])
})
it('getCollectionTeam success', () => {
const extraTeam = {id: '4321', name: 'Another team'}
const actual = reducer(mockstate, {
type: T.GET_COLLECTION_TEAMS_SUCCESS,
teams: [extraTeam, mockteam]
})
expect(actual).toEqual([mockteam, extraTeam])
})
it('returns same state for unrecognised action', () => {
const state = []
const actual = reducer(state, {
......
Markdown is supported
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