Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Correctly load integration creators and convert integration actions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilander authored and crspeller committed Nov 2, 2018
1 parent 9eda08b commit 695879c
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 187 deletions.
237 changes: 69 additions & 168 deletions actions/integration_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,195 +3,96 @@

import request from 'superagent';
import * as IntegrationActions from 'mattermost-redux/actions/integrations';
import {getProfilesByIds, getUser} from 'mattermost-redux/actions/users';
import {getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {getProfilesByIds} from 'mattermost-redux/actions/users';
import {getUser} from 'mattermost-redux/selectors/entities/users';

import store from 'stores/redux_store.jsx';
import {Integrations} from 'utils/constants.jsx';

const dispatch = store.dispatch;
const getState = store.getState;

export async function loadIncomingHooks(complete) {
const {data} = await IntegrationActions.getIncomingHooks('', 0, 10000)(dispatch, getState);
if (data) {
loadProfilesForIncomingHooks(data);
}

if (complete) {
complete(data);
}
}

export async function loadIncomingHooksForTeam(teamId, complete) {
const {data} = await IntegrationActions.getIncomingHooks(teamId, 0, 10000)(dispatch, getState);
if (data) {
loadProfilesForIncomingHooks(data);
}

if (complete) {
complete(data);
}
}

function loadProfilesForIncomingHooks(hooks) {
const state = getState();
const profilesToLoad = {};
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (!getUser(state, hook.user_id)) {
profilesToLoad[hook.user_id] = true;
export function loadIncomingHooksAndProfilesForTeam(teamId, page = 0, perPage = Integrations.PAGE_SIZE) {
return async (dispatch) => {
const {data} = await dispatch(IntegrationActions.getIncomingHooks(teamId, page, perPage));
if (data) {
dispatch(loadProfilesForIncomingHooks(data));
}
}

const list = Object.keys(profilesToLoad);
if (list.length === 0) {
return;
}

getProfilesByIds(list)(dispatch, getState);
};
}

export async function loadOutgoingHooks(complete) {
const {data} = await IntegrationActions.getOutgoingHooks('', '', 0, 10000)(dispatch, getState);
if (data) {
loadProfilesForOutgoingHooks(data);
}

if (complete) {
complete(data);
}
}

export async function loadOutgoingHooksForTeam(teamId, complete) {
const {data} = await IntegrationActions.getOutgoingHooks('', teamId, 0, 10000)(dispatch, getState);
if (data) {
loadProfilesForOutgoingHooks(data);
}

if (complete) {
complete(data);
}
}

function loadProfilesForOutgoingHooks(hooks) {
const state = getState();
const profilesToLoad = {};
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (!getUser(state, hook.creator_id)) {
profilesToLoad[hook.creator_id] = true;
export function loadProfilesForIncomingHooks(hooks) {
return async (dispatch, getState) => {
const state = getState();
const profilesToLoad = {};
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (!getUser(state, hook.user_id)) {
profilesToLoad[hook.user_id] = true;
}
}
}

const list = Object.keys(profilesToLoad);
if (list.length === 0) {
return;
}

getProfilesByIds(list)(dispatch, getState);
}

export async function loadTeamCommands(complete) {
const {data} = await dispatch(IntegrationActions.getCustomTeamCommands(getCurrentTeamId(getState())));
if (data) {
loadProfilesForCommands(data);
}

if (complete) {
complete(data);
}
}

function loadProfilesForCommands(commands) {
const state = getState();
const profilesToLoad = {};
for (let i = 0; i < commands.length; i++) {
const command = commands[i];
if (!getUser(state, command.creator_id)) {
profilesToLoad[command.creator_id] = true;
const list = Object.keys(profilesToLoad);
if (list.length === 0) {
return;
}
}

const list = Object.keys(profilesToLoad);
if (list.length === 0) {
return;
}

getProfilesByIds(list)(dispatch, getState);
}

export async function addIncomingHook(hook, success, error) {
const {data, error: err} = await IntegrationActions.createIncomingHook(hook)(dispatch, getState);
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
}

export async function updateIncomingHook(hook, success, error) {
const {data, error: err} = await IntegrationActions.updateIncomingHook(hook)(dispatch, getState);
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
}

export async function addOutgoingHook(hook, success, error) {
const {data, error: err} = await IntegrationActions.createOutgoingHook(hook)(dispatch, getState);
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
dispatch(getProfilesByIds(list));
};
}

export async function updateOutgoingHook(hook, success, error) {
const {data, error: err} = await IntegrationActions.updateOutgoingHook(hook)(dispatch, getState);
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
export function loadOutgoingHooksAndProfilesForTeam(teamId, page = 0, perPage = Integrations.PAGE_SIZE) {
return async (dispatch) => {
const {data} = await dispatch(IntegrationActions.getOutgoingHooks('', teamId, page, perPage));
if (data) {
dispatch(loadProfilesForOutgoingHooks(data));
}
};
}

export function deleteIncomingHook(id) {
IntegrationActions.removeIncomingHook(id)(dispatch, getState);
}
export function loadProfilesForOutgoingHooks(hooks) {
return async (dispatch, getState) => {
const state = getState();
const profilesToLoad = {};
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
if (!getUser(state, hook.creator_id)) {
profilesToLoad[hook.creator_id] = true;
}
}

export function deleteOutgoingHook(id) {
IntegrationActions.removeOutgoingHook(id)(dispatch, getState);
}
const list = Object.keys(profilesToLoad);
if (list.length === 0) {
return;
}

export function regenOutgoingHookToken(id) {
IntegrationActions.regenOutgoingHookToken(id)(dispatch, getState);
dispatch(getProfilesByIds(list));
};
}

export async function addCommand(command, success, error) {
const {data, error: err} = await IntegrationActions.addCommand(command)(dispatch, getState);
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
export function loadCommandsAndProfilesForTeam(teamId) {
return async (dispatch) => {
const {data} = await dispatch(IntegrationActions.getCustomTeamCommands(teamId));
if (data) {
dispatch(loadProfilesForCommands(data));
}
};
}

export async function editCommand(command, success, error) {
const {data, error: err} = await IntegrationActions.editCommand(command)(dispatch, getState);
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
}
export function loadProfilesForCommands(commands) {
return async (dispatch, getState) => {
const state = getState();
const profilesToLoad = {};
for (let i = 0; i < commands.length; i++) {
const command = commands[i];
if (!getUser(state, command.creator_id)) {
profilesToLoad[command.creator_id] = true;
}
}

export function deleteCommand(id) {
IntegrationActions.deleteCommand(id)(dispatch, getState);
}
const list = Object.keys(profilesToLoad);
if (list.length === 0) {
return;
}

export function regenCommandToken(id) {
IntegrationActions.regenCommandToken(id)(dispatch, getState);
dispatch(getProfilesByIds(list));
};
}

export function getYoutubeVideoInfo(googleKey, videoId, success, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class CommandsContainer extends React.PureComponent {
/**
* The function to call to fetch team commands
*/
getCustomTeamCommands: PropTypes.func.isRequired,
loadCommandsAndProfilesForTeam: PropTypes.func.isRequired,
}).isRequired,

/**
Expand All @@ -75,7 +75,7 @@ export default class CommandsContainer extends React.PureComponent {

componentDidMount() {
if (this.props.enableCommands) {
this.props.actions.getCustomTeamCommands(this.props.team.id).then(
this.props.actions.loadCommandsAndProfilesForTeam(this.props.team.id).then(
() => this.setState({loading: false})
);
}
Expand Down
5 changes: 3 additions & 2 deletions components/integrations/commands_container/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {getCustomTeamCommands} from 'mattermost-redux/actions/integrations';
import {getCommands} from 'mattermost-redux/selectors/entities/integrations';
import {getUsers} from 'mattermost-redux/selectors/entities/users';
import {getConfig} from 'mattermost-redux/selectors/entities/general';

import {loadCommandsAndProfilesForTeam} from 'actions/integration_actions';

import CommandsContainer from './commands_container.jsx';

function mapStateToProps(state) {
Expand All @@ -24,7 +25,7 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators({
getCustomTeamCommands,
loadCommandsAndProfilesForTeam,
}, dispatch),
};
}
Expand Down
4 changes: 3 additions & 1 deletion components/integrations/installed_incoming_webhooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {haveITeamPermission} from 'mattermost-redux/selectors/entities/roles';
import {Permissions} from 'mattermost-redux/constants';
import {getConfig} from 'mattermost-redux/selectors/entities/general';

import {loadIncomingHooksAndProfilesForTeam} from 'actions/integration_actions';

import InstalledIncomingWebhooks from './installed_incoming_webhooks.jsx';

function mapStateToProps(state) {
Expand All @@ -37,7 +39,7 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators({
getIncomingHooks: Actions.getIncomingHooks,
loadIncomingHooksAndProfilesForTeam,
removeIncomingHook: Actions.removeIncomingHook,
}, dispatch),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class InstalledIncomingWebhooks extends React.PureComponent {
/**
* The function to call for incomingWebhook List and for the status of api
*/
getIncomingHooks: PropTypes.func,
loadIncomingHooksAndProfilesForTeam: PropTypes.func,
}),

/**
Expand All @@ -70,16 +70,14 @@ export default class InstalledIncomingWebhooks extends React.PureComponent {
constructor(props) {
super(props);

this.deleteIncomingWebhook = this.deleteIncomingWebhook.bind(this);
this.incomingWebhookCompare = this.incomingWebhookCompare.bind(this);
this.state = {
loading: true,
};
}

componentDidMount() {
if (this.props.enableIncomingWebhooks) {
this.props.actions.getIncomingHooks(
this.props.actions.loadIncomingHooksAndProfilesForTeam(
this.props.teamId,
Constants.Integrations.START_PAGE_NUM,
Constants.Integrations.PAGE_SIZE
Expand All @@ -93,7 +91,7 @@ export default class InstalledIncomingWebhooks extends React.PureComponent {
this.props.actions.removeIncomingHook(incomingWebhook.id);
}

incomingWebhookCompare(a, b) {
incomingWebhookCompare = (a, b) => {
let displayNameA = a.display_name;
if (!displayNameA) {
const channelA = this.props.channels[a.channel_id];
Expand Down
4 changes: 3 additions & 1 deletion components/integrations/installed_outgoing_webhooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {haveITeamPermission} from 'mattermost-redux/selectors/entities/roles';
import {Permissions} from 'mattermost-redux/constants';
import {getConfig} from 'mattermost-redux/selectors/entities/general';

import {loadOutgoingHooksAndProfilesForTeam} from 'actions/integration_actions';

import InstalledOutgoingWebhook from './installed_outgoing_webhooks.jsx';

function mapStateToProps(state) {
Expand All @@ -36,7 +38,7 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators({
getOutgoingHooks: Actions.getOutgoingHooks,
loadOutgoingHooksAndProfilesForTeam,
removeOutgoingHook: Actions.removeOutgoingHook,
regenOutgoingHookToken: Actions.regenOutgoingHookToken,
}, dispatch),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class InstalledOutgoingWebhooks extends React.PureComponent {
/**
* The function to call for outgoingWebhook List and for the status of api
*/
getOutgoingHooks: PropTypes.func,
loadOutgoingHooksAndProfilesForTeam: PropTypes.func,

/**
* The function to call for regeneration of webhook token
Expand All @@ -84,8 +84,7 @@ export default class InstalledOutgoingWebhooks extends React.PureComponent {

componentDidMount() {
if (this.props.enableOutgoingWebhooks) {
this.props.actions.getOutgoingHooks(
'',
this.props.actions.loadOutgoingHooksAndProfilesForTeam(
this.props.teamId,
Constants.Integrations.START_PAGE_NUM,
Constants.Integrations.PAGE_SIZE
Expand Down
Loading

0 comments on commit 695879c

Please sign in to comment.