Skip to content

Commit

Permalink
Convert files related to redux from js to ts (#462)
Browse files Browse the repository at this point in the history
* [MM-222] Convert reducer and action types file to typescript and removed some unused code

* [MM-222] Converted select file to typescript and fixes some css warnings

* [MM-222] Convert actions file to typescript

* [MM-222] Updated reducer, used reducer type for store and added more types

* [MM-222] Resolved conflicts from master

* [MM-222] Migrate a component to TS and minor review fixes

* [MM-222] Fix flaky test

* [MM-520] Review fixes

* [MM-520] Fix ts error in index file

* [MM-542] Review fixes

* MM-222: converted client file to TS

* [MM-222]: Added type to the client promises

* [MM-222]: Added more types to the client.ts file

* [MM-222]: updated function return type and added few types to client.ts

* [MM-222]: fixed linting issue

* [MM-222]: resolved conflicts

* [MM-222]: removed null type from client.ts API response

* [MM-222]: Added some additional types

* Added lint types

* [MM-656] Add types for redux funcs related to create issue and attach commment modal

* fixed lint of type any being used

* [MM-222]: removed link tool tip data type

* using single quote for strings

---------

Co-authored-by: ayusht2810 <ayush.thakur@brightscout.com>
Co-authored-by: kshitij katiyar <kshitij.katiyar@brightscout.com>
  • Loading branch information
3 people authored Nov 29, 2024
1 parent 8837c71 commit 25b4802
Show file tree
Hide file tree
Showing 20 changed files with 351 additions and 316 deletions.
File renamed without changes.
135 changes: 56 additions & 79 deletions webapp/src/actions/index.js → webapp/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import {getCurrentChannelId, getCurrentUserId} from 'mattermost-redux/selectors/

import {PostTypes} from 'mattermost-redux/action_types';

import {AnyAction, Dispatch} from 'redux';

import Client from '../client';
import ActionTypes from '../action_types';
import manifest from '../manifest';
import {APIError, ConnectedData, GitlabUsersData, LHSData, ShowRhsPluginActionData, SubscriptionData} from 'src/types';
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) => {
let data;
return async (dispatch: Dispatch<AnyAction>) => {
let data: ConnectedData;
try {
data = await Client.getConnected(reminder);
} catch (error) {
Expand All @@ -24,8 +30,8 @@ export function getConnected(reminder = false) {
};
}

function checkAndHandleNotConnected(data) {
return async (dispatch) => {
function checkAndHandleNotConnected(data: APIError) {
return async (dispatch: Dispatch<AnyAction>) => {
if (data && data.id === 'not_connected') {
dispatch({
type: ActionTypes.RECEIVED_CONNECTED,
Expand All @@ -34,24 +40,24 @@ function checkAndHandleNotConnected(data) {
gitlab_username: '',
gitlab_client_id: '',
settings: {},
},
} as ConnectedData,
});
return false;
}
return true;
};
}

export function getReviewDetails(prList) {
return async (dispatch, getState) => {
let data;
export function getReviewDetails(prList: Item[]) {
return async (dispatch: Dispatch<AnyAction>) => {
let data: Item | APIError;
try {
data = await Client.getPrsDetails(prList);
} 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 @@ -65,16 +71,16 @@ export function getReviewDetails(prList) {
};
}

export function getYourPrDetails(prList) {
return async (dispatch, getState) => {
let data;
export function getYourPrDetails(prList: Item[]) {
return async (dispatch: Dispatch<AnyAction>) => {
let data: Item | APIError;
try {
data = await Client.getPrsDetails(prList);
} 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 @@ -88,45 +94,16 @@ export function getYourPrDetails(prList) {
};
}

export function getMentions() {
return async (dispatch, getState) => {
let data;
try {
data = await Client.getMentions();
} catch (error) {
return {error};
}

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

dispatch({
type: ActionTypes.RECEIVED_MENTIONS,
data,
});

return {data};
};
}

export function getLHSData() {
return async (dispatch, getState) => {
let data;
return async (dispatch: Dispatch<AnyAction>) => {
let data: LHSData | APIError;
try {
data = await Client.getLHSData();
} 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 @@ -144,14 +121,14 @@ export function getLHSData() {
* Stores "showRHSPlugin" action returned by
* "registerRightHandSidebarComponent" in plugin initialization.
*/
export function setShowRHSAction(showRHSPluginAction) {
export function setShowRHSAction(showRHSPluginAction: () => ShowRhsPluginActionData) {
return {
type: ActionTypes.RECEIVED_SHOW_RHS_ACTION,
showRHSPluginAction,
};
}

export function updateRHSState(rhsState) {
export function updateRHSState(rhsState: string) {
return {
type: ActionTypes.UPDATE_RHS_STATE,
state: rhsState,
Expand All @@ -160,13 +137,13 @@ export function updateRHSState(rhsState) {

const GITLAB_USER_GET_TIMEOUT_MILLISECONDS = 1000 * 60 * 60; // 1 hour

export function getGitlabUser(userID) {
return async (dispatch, getState) => {
export function getGitlabUser(userID: string) {
return async (dispatch: Dispatch<AnyAction>, getState: () => GlobalState) => {
if (!userID) {
return {};
}

const user = getState()[`plugins-${manifest.id}`].gitlabUsers[userID];
const user = getPluginState(getState()).gitlabUsers[userID];
if (
user &&
user.last_try &&
Expand All @@ -179,11 +156,11 @@ export function getGitlabUser(userID) {
return {data: user};
}

let data;
let data: GitlabUsersData;
try {
data = await Client.getGitlabUser(userID);
} catch (error) {
if (error.status === 404) {
} catch (error: unknown) {
if ((error as APIError).status === 404) {
dispatch({
type: ActionTypes.RECEIVED_GITLAB_USER,
userID,
Expand All @@ -203,7 +180,7 @@ export function getGitlabUser(userID) {
};
}

export function openCreateIssueModal(postId) {
export function openCreateIssueModal(postId: string) {
return {
type: ActionTypes.OPEN_CREATE_ISSUE_MODAL,
data: {
Expand All @@ -212,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 @@ -228,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 @@ -260,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 @@ -278,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 @@ -300,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 @@ -318,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 @@ -336,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 All @@ -354,13 +331,13 @@ export function getAssigneeOptions(projectID) {
};
}

export function getChannelSubscriptions(channelId) {
return async (dispatch) => {
export function getChannelSubscriptions(channelId: string) {
return async (dispatch: Dispatch<AnyAction>) => {
if (!channelId) {
return {};
}

let subscriptions;
let subscriptions: SubscriptionData;
try {
subscriptions = await Client.getChannelSubscriptions(channelId);
} catch (error) {
Expand All @@ -379,8 +356,8 @@ export function getChannelSubscriptions(channelId) {
};
}

export function sendEphemeralPost(message) {
return (dispatch, getState) => {
export function sendEphemeralPost(message: string) {
return (dispatch: Dispatch<AnyAction>, getState: () => GlobalState) => {
const timestamp = Date.now();
const state = getState();

Expand Down
Loading

0 comments on commit 25b4802

Please sign in to comment.