[JENKINS-35837] add a User type and fetch it when card stack loads; use that user ID to fetch the correct list of favorites

This commit is contained in:
Cliff Meyers 2016-07-08 12:14:34 -04:00
parent ee32458fd9
commit 2ed91dfb42
4 changed files with 59 additions and 6 deletions

View File

@ -5,7 +5,7 @@ import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import { favoritesSelector } from '../redux/FavoritesStore';
import { userSelector, favoritesSelector } from '../redux/FavoritesStore';
import { actions } from '../redux/FavoritesActions';
import { PipelineCard } from './PipelineCard';
@ -16,10 +16,23 @@ import { PipelineCard } from './PipelineCard';
export default class DashboardCards extends Component {
componentWillMount() {
this._initialize();
}
componentWillReceiveProps() {
this._initialize();
}
_initialize() {
const config = this.context.config;
const { user, favorites } = this.props;
if (config) {
this.props.fetchFavorites(config);
if (!user) {
this.props.fetchUser(config);
} else if (!favorites) {
this.props.fetchFavorites(config, user);
}
}
}
@ -60,8 +73,10 @@ export default class DashboardCards extends Component {
}
DashboardCards.propTypes = {
user: PropTypes.object,
pipelines: PropTypes.array,
favorites: PropTypes.array,
fetchUser: PropTypes.func,
fetchFavorites: PropTypes.func,
};
@ -69,5 +84,9 @@ DashboardCards.contextTypes = {
config: PropTypes.object,
};
const selectors = createSelector([favoritesSelector], (favorites) => ({ favorites }));
const selectors = createSelector(
[userSelector, favoritesSelector],
(user, favorites) => ({ user, favorites })
);
export default connect(selectors, actions)(DashboardCards);

View File

@ -0,0 +1,15 @@
/**
* Created by cmeyers on 7/8/16.
*/
import Immutable from 'immutable';
/* eslint new-cap: [0] */
const { Record } = Immutable;
export const User = Record({
_class: null,
_links: null,
email: null,
fullName: null,
id: null,
});

View File

@ -23,11 +23,22 @@ function parseJSON(response) {
}
export const actions = {
fetchFavorites(config) {
fetchUser(config) {
return (dispatch) => {
const baseUrl = config.getAppURLBase();
// TODO: pull real username value
const username = 'cmeyers';
const url = `${baseUrl}/rest/organizations/jenkins/user/`;
return dispatch(actions.generateData(
url,
ACTION_TYPES.SET_USER
));
};
},
fetchFavorites(config, user) {
return (dispatch) => {
const baseUrl = config.getAppURLBase();
const username = user.id;
const url = `${baseUrl}/rest/users/${username}/favorites/`;
return dispatch(actions.generateData(

View File

@ -5,25 +5,33 @@
import keymirror from 'keymirror';
import Immutable from 'immutable';
import { createSelector } from 'reselect';
import { User } from '../model/User';
/* eslint new-cap: [0] */
const { Record } = Immutable;
const FavoritesState = Record({
user: null,
favorites: null,
});
export const ACTION_TYPES = keymirror({
SET_USER: null,
SET_FAVORITES: null,
});
const actionHandlers = {
[ACTION_TYPES.SET_USER](state, { payload }) {
const user = new User(payload);
return state.set('user', user);
},
[ACTION_TYPES.SET_FAVORITES](state, { payload }) {
return state.set('favorites', payload);
},
};
const favoritesStore = state => state.favoritesStore;
export const userSelector = createSelector([favoritesStore], store => store.user);
export const favoritesSelector = createSelector([favoritesStore], store => store.favorites);
// reducer