diff options
Diffstat (limited to 'server/sonar-web/src/main/js/apps/project-admin/store')
4 files changed, 140 insertions, 1 deletions
diff --git a/server/sonar-web/src/main/js/apps/project-admin/store/actions.js b/server/sonar-web/src/main/js/apps/project-admin/store/actions.js index cad25a70e7a..083a28bfa92 100644 --- a/server/sonar-web/src/main/js/apps/project-admin/store/actions.js +++ b/server/sonar-web/src/main/js/apps/project-admin/store/actions.js @@ -23,6 +23,7 @@ import { dissociateProject } from '../../../api/quality-profiles'; import { getProfileByKey } from './rootReducer'; +import { getProjectLinks, createLink } from '../../../api/projectLinks'; export const RECEIVE_PROFILES = 'RECEIVE_PROFILES'; export const receiveProfiles = profiles => ({ @@ -68,3 +69,36 @@ export const setProjectProfile = (projectKey, oldKey, newKey) => dispatch(setProjectProfileAction(projectKey, oldKey, newKey)); }); }; + +export const RECEIVE_PROJECT_LINKS = 'RECEIVE_PROJECT_LINKS'; +export const receiveProjectLinks = (projectKey, links) => ({ + type: RECEIVE_PROJECT_LINKS, + projectKey, + links +}); + +export const fetchProjectLinks = projectKey => dispatch => { + getProjectLinks(projectKey).then(links => { + dispatch(receiveProjectLinks(projectKey, links)); + }); +}; + +export const ADD_PROJECT_LINK = 'ADD_PROJECT_LINK'; +const addProjectLink = (projectKey, link) => ({ + type: ADD_PROJECT_LINK, + projectKey, + link +}); + +export const createProjectLink = (projectKey, name, url) => dispatch => { + return createLink(projectKey, name, url).then(link => { + dispatch(addProjectLink(projectKey, link)); + }); +}; + +export const DELETE_PROJECT_LINK = 'DELETE_PROJECT_LINK'; +export const deleteProjectLink = (projectKey, linkId) => ({ + type: DELETE_PROJECT_LINK, + projectKey, + linkId +}); diff --git a/server/sonar-web/src/main/js/apps/project-admin/store/links.js b/server/sonar-web/src/main/js/apps/project-admin/store/links.js new file mode 100644 index 00000000000..9a79d3d7905 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/project-admin/store/links.js @@ -0,0 +1,45 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import keyBy from 'lodash/keyBy'; +import omit from 'lodash/omit'; +import { RECEIVE_PROJECT_LINKS, DELETE_PROJECT_LINK } from './actions'; +import { ADD_PROJECT_LINK } from './actions'; + +const links = (state = {}, action = {}) => { + if (action.type === RECEIVE_PROJECT_LINKS) { + const newLinksById = keyBy(action.links, 'id'); + return { ...state, ...newLinksById }; + } + + if (action.type === ADD_PROJECT_LINK) { + return { ...state, [action.link.id]: action.link }; + } + + if (action.type === DELETE_PROJECT_LINK) { + return omit(state, action.linkId); + } + + return state; +}; + +export default links; + +export const getLink = (state, id) => + state[id]; diff --git a/server/sonar-web/src/main/js/apps/project-admin/store/linksByProject.js b/server/sonar-web/src/main/js/apps/project-admin/store/linksByProject.js new file mode 100644 index 00000000000..dd48ff45f98 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/project-admin/store/linksByProject.js @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import without from 'lodash/without'; +import { + RECEIVE_PROJECT_LINKS, + DELETE_PROJECT_LINK, + ADD_PROJECT_LINK +} from './actions'; + +const linksByProject = (state = {}, action = {}) => { + if (action.type === RECEIVE_PROJECT_LINKS) { + const linkIds = action.links.map(link => link.id); + return { ...state, [action.projectKey]: linkIds }; + } + + if (action.type === ADD_PROJECT_LINK) { + const byProject = state[action.projectKey] || []; + const ids = [...byProject, action.link.id]; + return { ...state, [action.projectKey]: ids }; + } + + if (action.type === DELETE_PROJECT_LINK) { + const ids = without(state[action.projectKey], action.linkId); + return { ...state, [action.projectKey]: ids }; + } + + return state; +}; + +export default linksByProject; + +export const getLinks = (state, projectKey) => state[projectKey] || []; diff --git a/server/sonar-web/src/main/js/apps/project-admin/store/rootReducer.js b/server/sonar-web/src/main/js/apps/project-admin/store/rootReducer.js index 5f5dc3d7899..e8174f53aa0 100644 --- a/server/sonar-web/src/main/js/apps/project-admin/store/rootReducer.js +++ b/server/sonar-web/src/main/js/apps/project-admin/store/rootReducer.js @@ -23,10 +23,14 @@ import profiles, { getAllProfiles as nextGetAllProfiles } from './profiles'; import profilesByProject, { getProfiles } from './profilesByProject'; +import links, { getLink } from './links'; +import linksByProject, { getLinks } from './linksByProject'; const rootReducer = combineReducers({ profiles, - profilesByProject + profilesByProject, + links, + linksByProject }); export default rootReducer; @@ -40,3 +44,10 @@ export const getAllProfiles = state => export const getProjectProfiles = (state, projectKey) => getProfiles(state.profilesByProject, projectKey) .map(profileKey => getProfileByKey(state, profileKey)); + +export const getLinkById = (state, linkId) => + getLink(state.links, linkId); + +export const getProjectLinks = (state, projectKey) => + getLinks(state.linksByProject, projectKey) + .map(linkId => getLinkById(state, linkId)); |