Skip to content

Commit

Permalink
[MM-656] Add types for redux funcs related to create issue and attach…
Browse files Browse the repository at this point in the history
… commment modal
  • Loading branch information
raghavaggarwal2308 committed Aug 1, 2024
1 parent f7b2bf2 commit ff8d95d
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 73 deletions.
41 changes: 21 additions & 20 deletions webapp/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {APIError, ConnectedData, GitlabUsersData, LHSData, ShowRhsPluginActionDa
import {Item} from 'src/types/gitlab_items';
import {GlobalState} from 'src/types/store';
import {getPluginState} from 'src/selectors';
import {CommentBody, IssueBody} from 'src/types/gitlab_types';

export function getConnected(reminder = false) {
return async (dispatch: Dispatch<AnyAction>) => {
Expand Down Expand Up @@ -179,7 +180,7 @@ export function getGitlabUser(userID: string) {
};
}

export function openCreateIssueModal(postId) {
export function openCreateIssueModal(postId: string) {
return {
type: ActionTypes.OPEN_CREATE_ISSUE_MODAL,
data: {
Expand All @@ -188,7 +189,7 @@ export function openCreateIssueModal(postId) {
};
}

export function openCreateIssueModalWithoutPost(title, channelId) {
export function openCreateIssueModalWithoutPost(title: string, channelId: string) {
return {
type: ActionTypes.OPEN_CREATE_ISSUE_MODAL_WITHOUT_POST,
data: {
Expand All @@ -204,24 +205,24 @@ export function closeCreateIssueModal() {
};
}

export function createIssue(payload) {
return async (dispatch) => {
export function createIssue(payload: IssueBody) {
return async (dispatch: Dispatch<AnyAction>) => {
let data;
try {
data = await Client.createIssue(payload);
} catch (error) {
return {error};
}

const connected = await dispatch(checkAndHandleNotConnected(data));
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
return {data};
};
}

export function openAttachCommentToIssueModal(postId) {
export function openAttachCommentToIssueModal(postId: string) {
return {
type: ActionTypes.OPEN_ATTACH_COMMENT_TO_ISSUE_MODAL,
data: {
Expand All @@ -236,16 +237,16 @@ export function closeAttachCommentToIssueModal() {
};
}

export function attachCommentToIssue(payload) {
return async (dispatch) => {
export function attachCommentToIssue(payload: CommentBody) {
return async (dispatch: Dispatch<AnyAction>) => {
let data;
try {
data = await Client.attachCommentToIssue(payload);
} catch (error) {
return {error};
}

const connected = await dispatch(checkAndHandleNotConnected(data));
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -254,15 +255,15 @@ export function attachCommentToIssue(payload) {
}

export function getProjects() {
return async (dispatch, getState) => {
return async (dispatch: Dispatch<AnyAction>) => {
let data;
try {
data = await Client.getProjects();
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -276,16 +277,16 @@ export function getProjects() {
};
}

export function getLabelOptions(projectID) {
return async (dispatch, getState) => {
export function getLabelOptions(projectID: number) {
return async (dispatch: Dispatch<AnyAction>) => {
let data;
try {
data = await Client.getLabels(projectID);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -294,16 +295,16 @@ export function getLabelOptions(projectID) {
};
}

export function getMilestoneOptions(projectID) {
return async (dispatch, getState) => {
export function getMilestoneOptions(projectID: number) {
return async (dispatch: Dispatch<AnyAction>) => {
let data;
try {
data = await Client.getMilestones(projectID);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -312,16 +313,16 @@ export function getMilestoneOptions(projectID) {
};
}

export function getAssigneeOptions(projectID) {
return async (dispatch, getState) => {
export function getAssigneeOptions(projectID: number) {
return async (dispatch: Dispatch<AnyAction>, getState: () => GlobalState) => {
let data;
try {
data = await Client.getAssignees(projectID);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
Expand Down
14 changes: 7 additions & 7 deletions webapp/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Options} from 'mattermost-redux/types/client4';

import {Item, TooltipData} from 'src/types/gitlab_items';
import {APIError, ConnectedData, GitlabUsersData, LHSData, SubscriptionData} from 'src/types';
import {CommentBody, IssueBody} from 'src/types/gitlab_types';

export default class Client {
private url = '';
Expand Down Expand Up @@ -41,32 +42,31 @@ export default class Client {
return this.doGet<SubscriptionData>(`${this.url}/channel/${channelID}/subscriptions`);
};


createIssue = async (payload) => {
createIssue = async (payload: IssueBody) => {
return this.doPost(`${this.url}/issue`, payload);
}

attachCommentToIssue = async (payload) => {
attachCommentToIssue = async (payload: CommentBody) => {
return this.doPost(`${this.url}/attachcommenttoissue`, payload);
}

searchIssues = async (searchTerm) => {
searchIssues = async (searchTerm: string) => {
return this.doGet(`${this.url}/searchissues?search=${searchTerm}`);
}

getProjects = async () => {
return this.doGet(`${this.url}/projects`);
}

getLabels = async (projectID) => {
getLabels = async (projectID: number) => {
return this.doGet(`${this.url}/labels?projectID=${projectID}`);
}

getMilestones = async (projectID) => {
getMilestones = async (projectID: number) => {
return this.doGet(`${this.url}/milestones?projectID=${projectID}`);
}

getAssignees = async (projectID) => {
getAssignees = async (projectID: number) => {
return this.doGet(`${this.url}/assignees?projectID=${projectID}`);
}

Expand Down
5 changes: 2 additions & 3 deletions webapp/src/components/gitlab_project_selector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {Theme} from 'mattermost-redux/types/preferences';

import {getProjects} from 'src/actions';
import ReactSelectSetting from 'src/components/react_select_setting';
import {GlobalState} from 'src/types/global_state';
import {getPluginState} from 'src/selectors';
import {getYourProjects} from 'src/selectors';
import {getErrorMessage} from 'src/utils/user_utils';
import {Project, ProjectSelection} from 'src/types/gitlab_types';
import {ErrorType, SelectionType} from 'src/types/common';
Expand All @@ -26,7 +25,7 @@ const GitlabProjectSelector = ({theme, required, onChange, value, addValidate, r
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string>('');

const myProjects = useSelector((state: GlobalState) => getPluginState(state).yourProjects);
const myProjects = useSelector(getYourProjects);

const dispatch = useDispatch();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const CreateIssueForm = ({theme, handleClose, isSubmitting, setIsSubmitting}: Pr
if (post) {
setIssueDescription(post.message);
} else if (channelId) {
setIssueTitle(title.substring(0, MAX_TITLE_LENGTH));
setIssueTitle(title?.substring(0, MAX_TITLE_LENGTH) ?? '');
}
}, []);

Expand All @@ -68,7 +68,7 @@ const CreateIssueForm = ({theme, handleClose, isSubmitting, setIsSubmitting}: Pr
assignees: assignees.map((assignee) => assignee.value),
milestone: milestone?.value,
post_id: postId,
channel_id: channelId,
channel_id: channelId as string,
};

setIsSubmitting(true);
Expand Down Expand Up @@ -186,7 +186,7 @@ const CreateIssueForm = ({theme, handleClose, isSubmitting, setIsSubmitting}: Pr
required={true}
disabled={false}
maxLength={MAX_TITLE_LENGTH}
value={issueTitle}
value={issueTitle ?? ''}
onChange={handleIssueTitleChange}
/>
{issueTitleValidationError}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import {useDispatch, useSelector} from 'react-redux';
import {getPost} from 'mattermost-redux/selectors/entities/posts';
import {isSystemMessage} from 'mattermost-redux/utils/post_utils';

import manifest from 'src/manifest';
import GitLabIcon from 'src/images/icons/gitlab';
import {openAttachCommentToIssueModal} from 'src/actions';
import {GlobalState} from 'src/types/global_state';
import {isUserConnectedToGitlab} from 'src/selectors';
import {getConnected} from 'src/selectors';
import {GlobalState} from 'src/types/store';

interface PropTypes {
postId: string;
Expand All @@ -23,7 +22,7 @@ const AttachCommentToIssuePostMenuAction = ({postId}: PropTypes) => {
const isPostSystemMessage = Boolean(!post || isSystemMessage(post));

return {
show: isUserConnectedToGitlab(state) && !isPostSystemMessage,
show: getConnected(state) && !isPostSystemMessage,
};
});

Expand Down
7 changes: 3 additions & 4 deletions webapp/src/components/post_options/create_issue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {getPost} from 'mattermost-redux/selectors/entities/posts';
import {isSystemMessage} from 'mattermost-redux/utils/post_utils';

import GitLabIcon from 'src/images/icons/gitlab';
import manifest from 'src/manifest';
import {openCreateIssueModal} from 'src/actions';
import {GlobalState} from 'src/types/global_state';
import {isUserConnectedToGitlab} from 'src/selectors';
import {getConnected} from 'src/selectors';
import {GlobalState} from 'src/types/store';

type PropTypes = {
postId: string;
Expand All @@ -22,7 +21,7 @@ const CreateIssuePostMenuAction = ({postId}: PropTypes) => {
const isPostSystemMessage = Boolean(!post || isSystemMessage(post));

return {
show: isUserConnectedToGitlab(state) && !isPostSystemMessage,
show: getConnected(state) && !isPostSystemMessage,
};
});

Expand Down
14 changes: 8 additions & 6 deletions webapp/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {combineReducers, Reducer} from 'redux';
import ActionTypes from '../action_types';
import Constants from '../constants';
import {Item} from 'src/types/gitlab_items';
import {ConnectedData, GitlabUsersData, LHSData, ShowRhsPluginActionData, SubscriptionData} from 'src/types';
import {ConnectedData, CreateIssueModalData, GitlabUsersData, LHSData, ShowRhsPluginActionData, SubscriptionData} from 'src/types';
import {Project} from 'src/types/gitlab_types';

const connected: Reducer<boolean, {type: string, data: ConnectedData}> = (state = false, action) => {
switch (action.type) {
Expand Down Expand Up @@ -130,7 +131,7 @@ const gitlabUsers: Reducer<Record<string, GitlabUsersData>, {type: string, data:
}
};

const isCreateIssueModalVisible = (state = false, action) => {
const isCreateIssueModalVisible = (state = false, action: {type: string}) => {
switch (action.type) {
case ActionTypes.OPEN_CREATE_ISSUE_MODAL:
case ActionTypes.OPEN_CREATE_ISSUE_MODAL_WITHOUT_POST:
Expand All @@ -142,7 +143,7 @@ const isCreateIssueModalVisible = (state = false, action) => {
}
};

const isAttachCommentToIssueModalVisible = (state = false, action) => {
const isAttachCommentToIssueModalVisible = (state = false, action: {type: string}) => {
switch (action.type) {
case ActionTypes.OPEN_ATTACH_COMMENT_TO_ISSUE_MODAL:
return true;
Expand All @@ -153,7 +154,7 @@ const isAttachCommentToIssueModalVisible = (state = false, action) => {
}
};

const postIdForAttachCommentToIssueModal = (state = {}, action) => {
const postIdForAttachCommentToIssueModal = (state = '', action: {type: string, data: {postId: string}}) => {
switch (action.type) {
case ActionTypes.OPEN_ATTACH_COMMENT_TO_ISSUE_MODAL:
return action.data.postId;
Expand All @@ -164,12 +165,13 @@ const postIdForAttachCommentToIssueModal = (state = {}, action) => {
}
};

const createIssueModal = (state = {}, action) => {
const createIssueModal: Reducer<CreateIssueModalData, {type: string, data: CreateIssueModalData}> = (state = {}, action) => {
switch (action.type) {
case ActionTypes.OPEN_CREATE_ISSUE_MODAL:
case ActionTypes.OPEN_CREATE_ISSUE_MODAL_WITHOUT_POST:
return {
...state,

postId: action.data.postId,
title: action.data.title,
channelId: action.data.channelId,
Expand All @@ -181,7 +183,7 @@ const createIssueModal = (state = {}, action) => {
}
};

function yourProjects(state = [], action) {
function yourProjects(state = [] as Project[], action: {type: string, data: Project[]}) {
switch (action.type) {
case ActionTypes.RECEIVED_PROJECTS:
return action.data;
Expand Down
16 changes: 8 additions & 8 deletions webapp/src/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ function mapPrsToDetails(prs?: Item[], details?: Item[]): Item[] {

export const getPluginState = (state: GlobalState) => state[pluginStateKey];

export const isUserConnectedToGitlab = (state) => state[`plugins-${manifest.id}`].connected;

export const getSidebarData = createSelector(
getPluginState,
(pluginState: PluginState): SideBarData => {
Expand All @@ -66,12 +64,12 @@ export const getSidebarData = createSelector(
},
);

export const isCreateIssueModalVisible = (state) => state[`plugins-${manifest.id}`].isCreateIssueModalVisible;
export const isCreateIssueModalVisible = (state: GlobalState) => getPluginState(state).isCreateIssueModalVisible;

export const isAttachCommentToIssueModalVisible = (state) => state[`plugins-${manifest.id}`].isAttachCommentToIssueModalVisible;
export const isAttachCommentToIssueModalVisible = (state: GlobalState) => getPluginState(state).isAttachCommentToIssueModalVisible;

export const getCreateIssueModalContents = (state) => {
const {postId, title, channelId} = state[`plugins-${manifest.id}`].createIssueModal;
export const getCreateIssueModalContents = (state: GlobalState) => {
const {postId, title, channelId} = getPluginState(state).createIssueModal;

const post = postId ? getPost(state, postId) : null;
return {
Expand All @@ -81,13 +79,15 @@ export const getCreateIssueModalContents = (state) => {
};
};

export const getAttachCommentModalContents = (state) => {
const postId = state[`plugins-${manifest.id}`].postIdForAttachCommentToIssueModal;
export const getAttachCommentModalContents = (state: GlobalState) => {
const postId = getPluginState(state).postIdForAttachCommentToIssueModal;
const post = getPost(state, postId);

return post;
};

export const getYourProjects = (state: GlobalState) => getPluginState(state).yourProjects;

export const getConnected = (state: GlobalState) => getPluginState(state).connected;

export const getConnectedGitlabUrl = (state: GlobalState) => getPluginState(state).gitlabURL;
Loading

0 comments on commit ff8d95d

Please sign in to comment.