Skip to content

Commit

Permalink
feat: Add option to mark projects as favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardokl committed Sep 24, 2016
1 parent 8fb08dd commit b824bfa
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 87 deletions.
126 changes: 82 additions & 44 deletions public/js/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -23254,7 +23254,6 @@

var initialState = {
list: [],
favorites: [],
searching: false
};
// ...state.alternatives.slice(0, action.index),
Expand Down Expand Up @@ -23300,10 +23299,6 @@

var _actionTypes = __webpack_require__(200);

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var initialState = {
result: [],
projects: {}
Expand All @@ -23314,20 +23309,11 @@
var action = arguments[1];

switch (action.type) {
case _actionTypes.FETCH_FAVORITE_PROJECTS:
return _extends({}, state, action.favoriteProjects);
case _actionTypes.REMOVE_PROJECT_FROM_FAVORITES:
return _extends({}, state, {
projects: _extends({}, state.projects, _defineProperty({}, state.result[action.index], false)),
result: [].concat(_toConsumableArray(state.result.slice(0, action.index)), _toConsumableArray(state.result.slice(action.index + 1)))
});
case _actionTypes.ADD_PROJECT_TO_FAVORITES:
var project = action.data.project;

var normalizedProject = _defineProperty({}, project.id, project);

return _extends({}, state, {
result: [project.id].concat(_toConsumableArray(state.result)),
projects: _extends({}, state.projects, _defineProperty({}, project.id, project))
});
return _extends({}, state, action.data.favoriteProjects);
}

return state;
Expand Down Expand Up @@ -23499,7 +23485,7 @@
});
exports.fetchIssueBranchName = exports.createChromeNewTab = exports.searchProjects = exports.toggleProjectFavorite = exports.fetchFavoriteProjects = exports.fetchProjects = exports.removeUserAccessToken = exports.saveUserAccessToken = exports.fetchUserAccessToken = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /* global chrome */
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _api = __webpack_require__(206);

Expand All @@ -23513,6 +23499,10 @@

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /* global chrome */

var fetchBranchNameAttribute = function fetchBranchNameAttribute(tabId) {
return new Promise(function (resolve, reject) {
_api2.default.chrome.getNewBranchButtonAttribute(tabId, 'title').then(resolve).catch(function () {
Expand Down Expand Up @@ -23597,31 +23587,42 @@
};
};

var saveProjectsToStorage = function saveProjectsToStorage(projects) {
_api2.default.chrome.setStorage({ projects: projects });
var fetchStorageFavorites = function fetchStorageFavorites() {
return function (dispatch) {
return new Promise(function (resolve) {
_api2.default.chrome.getStorage('favorites').then(function (response) {
dispatch({
type: action.FETCH_FAVORITE_PROJECTS,
favoriteProjects: response.data
});
resolve(response);
}).catch(resolve);
});
};
};

var fetchProjects = exports.fetchProjects = function fetchProjects() {
var getProjectSchema = function getProjectSchema(project) {
return {
id: project.id,
name: project.name,
nameSpace: project.namespace.name,
webUrl: project.web_url,
sshUrl: project.ssh_url_to_repo
};
};

var fetchGitlabProjects = function fetchGitlabProjects() {
return function (dispatch, getState) {
var state = getState();
var accessToken = getState().user.accessToken;

//dispatch(fetchStorageProjects())
_api2.default.gitlab.fetchProjects({ accessToken: state.user.accessToken }).then(function (response) {
var projects = response.map(function (project) {
return {
id: project.id,
name: project.name,
nameSpace: project.namespace.name,
webUrl: project.web_url,
sshUrl: project.ssh_url_to_repo
};
});

_api2.default.gitlab.fetchProjects({ accessToken: accessToken }).then(function (response) {
var projects = response.map(getProjectSchema);

dispatch({
type: action.FETCH_GITLAB_PROJECTS,
data: projects
});
saveProjectsToStorage(projects);
}).catch(function () {
return dispatch({
type: action.FETCH_GITLAB_PROJECTS,
Expand All @@ -23631,6 +23632,14 @@
};
};

var fetchProjects = exports.fetchProjects = function fetchProjects() {
return function (dispatch) {
dispatch(fetchStorageFavorites()).then(function () {
return dispatch(fetchGitlabProjects());
});
};
};

var fetchFavoriteProjects = exports.fetchFavoriteProjects = function fetchFavoriteProjects() {
return function (dispatch) {
dispatch({
Expand All @@ -23639,13 +23648,26 @@
};
};

var updateFavoritesStorage = function updateFavoritesStorage(favorites) {
return _api2.default.chrome.setStorage({ favorites: favorites });
};

var addProjectToFavorites = function addProjectToFavorites(project) {
return function (dispatch) {
_api2.default.favorites.create({ project: project }).then(function (response) {
return function (dispatch, getState) {
var _getState = getState();

var favoriteProjects = _getState.favoriteProjects;

var favoriteProjectsUpdate = {
result: [project.id].concat(_toConsumableArray(favoriteProjects.result)),
projects: _extends({}, favoriteProjects.projects, _defineProperty({}, project.id, project))
};

updateFavoritesStorage(favoriteProjectsUpdate).then(function () {
return dispatch({
type: action.ADD_PROJECT_TO_FAVORITES,
data: {
project: project
favoriteProjects: favoriteProjectsUpdate
}
});
}).catch(function (error) {
Expand All @@ -23656,14 +23678,27 @@

var removeProjectFromFavorites = function removeProjectFromFavorites(projectId) {
return function (dispatch, getState) {
var favoriteIds = getState().favoriteProjects.result;
var projectIndex = favoriteIds.findIndex(function (id) {
var _getState2 = getState();

var favoriteProjects = _getState2.favoriteProjects;

var projectIndex = favoriteProjects.result.findIndex(function (id) {
return id === projectId;
});
var favoriteProjectsUpdate = {
projects: _extends({}, favoriteProjects.projects, _defineProperty({}, favoriteProjects.result[projectIndex], false)),
result: [].concat(_toConsumableArray(favoriteProjects.result.slice(0, projectIndex)), _toConsumableArray(favoriteProjects.result.slice(projectIndex + 1)))
};

dispatch({
type: action.REMOVE_PROJECT_FROM_FAVORITES,
index: projectIndex
delete favoriteProjectsUpdate.projects[projectId];

updateFavoritesStorage(favoriteProjectsUpdate).then(function () {
return dispatch({
type: action.REMOVE_PROJECT_FROM_FAVORITES,
data: {
favoriteProjects: favoriteProjectsUpdate
}
});
});
};
};
Expand Down Expand Up @@ -24766,6 +24801,7 @@
}),
_react2.default.createElement(_projects2.default, {
list: projects.list,
favoriteProjects: favoriteProjects,
onCreateNewChromeTab: this.props.onCreateNewChromeTab,
onAddProjectToFavorites: this.props.onAddProjectToFavorites
})
Expand Down Expand Up @@ -25034,8 +25070,10 @@
props.list.map(function (project, index) {
return _react2.default.createElement(_projectItem2.default, {
key: index,
className: (0, _classnames2.default)('projects__item', { favorite: project.favorite }),
favorite: project.favorite,
className: (0, _classnames2.default)('projects__item', {
favorite: !!props.favoriteProjects.projects[project.id]
}),
favorite: !!props.favoriteProjects.projects[project.id],
name: project.name,
nameSpace: project.nameSpace,
url: project.webUrl,
Expand Down
90 changes: 66 additions & 24 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,63 +79,105 @@ const fetchStorageProjects = () => (dispatch) => {
})
}

const saveProjectsToStorage = (projects) => {
API.chrome.setStorage({projects})
const fetchStorageFavorites = () => (dispatch) => {
return new Promise(resolve => {
API.chrome.getStorage('favorites')
.then(response => {
dispatch({
type: action.FETCH_FAVORITE_PROJECTS,
favoriteProjects: response.data
})
resolve(response)
})
.catch(resolve)
})
}

export const fetchProjects = () => (dispatch, getState) => {
const state = getState()
const getProjectSchema = (project) => ({
id: project.id,
name: project.name,
nameSpace: project.namespace.name,
webUrl: project.web_url,
sshUrl: project.ssh_url_to_repo
})

const fetchGitlabProjects = () => (dispatch, getState) => {
const {accessToken} = getState().user

//dispatch(fetchStorageProjects())
API.gitlab.fetchProjects({accessToken: state.user.accessToken})
API.gitlab.fetchProjects({accessToken})
.then((response) => {
const projects = response.map(project => ({
id: project.id,
name: project.name,
nameSpace: project.namespace.name,
webUrl: project.web_url,
sshUrl: project.ssh_url_to_repo
}))
const projects = response.map(getProjectSchema)

dispatch({
type: action.FETCH_GITLAB_PROJECTS,
data: projects
})
saveProjectsToStorage(projects)
})
.catch(() => dispatch({
type: action.FETCH_GITLAB_PROJECTS,
data: []
}))
}

export const fetchProjects = () => (dispatch) => {
dispatch(fetchStorageFavorites())
.then(() => dispatch(fetchGitlabProjects()))
}

export const fetchFavoriteProjects = () => (dispatch) => {
dispatch({
type: action.FETCH_FAVORITE_PROJECTS,
})
}

const addProjectToFavorites = (project) => (dispatch) => {
API.favorites.create({project})
.then(response => (
const updateFavoritesStorage = (favorites) => {
return API.chrome.setStorage({favorites})
}

const addProjectToFavorites = (project) => (dispatch, getState) => {
const {favoriteProjects} = getState()
const favoriteProjectsUpdate = {
result: [project.id, ...favoriteProjects.result],
projects: {...favoriteProjects.projects, [project.id]: project}
}

updateFavoritesStorage(favoriteProjectsUpdate)
.then(() => (
dispatch({
type: action.ADD_PROJECT_TO_FAVORITES,
data: {
project,
favoriteProjects: favoriteProjectsUpdate
}
})
))
.catch(error => console.warn(error))
}

const removeProjectFromFavorites = (projectId) => (dispatch, getState) => {
const favoriteIds = getState().favoriteProjects.result
const projectIndex = favoriteIds.findIndex(id => id === projectId)
const {favoriteProjects} = getState()
const projectIndex = favoriteProjects.result.findIndex(id => id === projectId)
const favoriteProjectsUpdate = {
projects: {
...favoriteProjects.projects,
[favoriteProjects.result[projectIndex]]: false
},
result: [
...favoriteProjects.result.slice(0, projectIndex),
...favoriteProjects.result.slice(projectIndex + 1)
]
}

dispatch({
type: action.REMOVE_PROJECT_FROM_FAVORITES,
index: projectIndex,
})
delete favoriteProjectsUpdate.projects[projectId]

updateFavoritesStorage(favoriteProjectsUpdate)
.then(() =>
dispatch({
type: action.REMOVE_PROJECT_FROM_FAVORITES,
data: {
favoriteProjects: favoriteProjectsUpdate
}
})
)
}

const addOrRemoveProjectFromFavorites = (project) => (dispatch) => {
Expand Down
20 changes: 4 additions & 16 deletions src/reducers/favorite-projects.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
FETCH_FAVORITE_PROJECTS,
ADD_PROJECT_TO_FAVORITES,
REMOVE_PROJECT_FROM_FAVORITES
} from 'constants/action-types'
Expand All @@ -10,26 +11,13 @@ const initialState = {

const projects = (state = initialState, action) => {
switch (action.type) {
case FETCH_FAVORITE_PROJECTS:
return {...state, ...action.favoriteProjects}
case REMOVE_PROJECT_FROM_FAVORITES:
return {
...state,
projects: {
...state.projects,
[state.result[action.index]]: false
},
result: [
...state.result.slice(0, action.index),
...state.result.slice(action.index + 1)
]
}
case ADD_PROJECT_TO_FAVORITES:
const {project} = action.data
const normalizedProject = {[project.id]: project}

return {
...state,
result: [project.id, ...state.result],
projects: {...state.projects, [project.id]: project}
...action.data.favoriteProjects
}
}

Expand Down
1 change: 0 additions & 1 deletion src/reducers/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {

const initialState = {
list: [],
favorites: [],
searching: false
}
// ...state.alternatives.slice(0, action.index),
Expand Down
Loading

0 comments on commit b824bfa

Please sign in to comment.