Skip to content

Commit

Permalink
[MM-199] Convert files related to redux from js to ts (#743)
Browse files Browse the repository at this point in the history
* [MM-199] Convert files related to redux from js to ts

* [MM-199]: Fixed review comments

* [MM-199] Update type file and import types explicitly

---------

Co-authored-by: kshitij <kshitij.katiyar@brightscout.com>
Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
  • Loading branch information
3 people committed May 14, 2024
1 parent da4c4df commit 936e5c3
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 138 deletions.
File renamed without changes.
89 changes: 47 additions & 42 deletions webapp/src/actions/index.js → webapp/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {DispatchFunc} from 'mattermost-redux/types/actions';

import {getPluginState} from '../selectors';

import {GetStateFunc} from '../types/store';

import Client from '../client';
import ActionTypes from '../action_types';

import manifest from '../manifest';
import {APIError, PrsDetailsData, ShowRhsPluginActionData} from '../types/github_types';

export function getConnected(reminder = false) {
return async (dispatch) => {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getConnected(reminder);
Expand All @@ -24,8 +29,8 @@ export function getConnected(reminder = false) {
};
}

function checkAndHandleNotConnected(data) {
return async (dispatch) => {
function checkAndHandleNotConnected(data: {id: string}) {
return async (dispatch: DispatchFunc) => {
if (data && data.id === 'not_connected') {
dispatch({
type: ActionTypes.RECEIVED_CONNECTED,
Expand All @@ -42,16 +47,16 @@ function checkAndHandleNotConnected(data) {
};
}

export function getReviewsDetails(prList) {
return async (dispatch, getState) => {
export function getReviewsDetails(prList: PrsDetailsData[]) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getPrsDetails(prList);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -66,15 +71,15 @@ export function getReviewsDetails(prList) {
}

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

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -89,15 +94,15 @@ export function getRepos() {
}

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

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -111,16 +116,16 @@ export function getSidebarContent() {
};
}

export function getYourPrsDetails(prList) {
return async (dispatch, getState) => {
export function getYourPrsDetails(prList: PrsDetailsData[]) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getPrsDetails(prList);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -134,16 +139,16 @@ export function getYourPrsDetails(prList) {
};
}

export function getLabelOptions(repo) {
return async (dispatch, getState) => {
export function getLabelOptions(repo: string) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getLabels(repo);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -152,16 +157,16 @@ export function getLabelOptions(repo) {
};
}

export function getAssigneeOptions(repo) {
return async (dispatch, getState) => {
export function getAssigneeOptions(repo: string) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getAssignees(repo);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -170,16 +175,16 @@ export function getAssigneeOptions(repo) {
};
}

export function getMilestoneOptions(repo) {
return async (dispatch, getState) => {
export function getMilestoneOptions(repo: string) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.getMilestones(repo);
} catch (error) {
return {error};
}

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -189,15 +194,15 @@ export function getMilestoneOptions(repo) {
}

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

const connected = await checkAndHandleNotConnected(data)(dispatch, getState);
const connected = await checkAndHandleNotConnected(data)(dispatch);
if (!connected) {
return {error: data};
}
Expand All @@ -213,13 +218,13 @@ export function getMentions() {

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

export function getGitHubUser(userID) {
return async (dispatch, getState) => {
export function getGitHubUser(userID: string) {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
if (!userID) {
return {};
}

const user = getState()[`plugins-${manifest.id}`].githubUsers[userID];
const user = getPluginState(getState()).githubUsers[userID];
if (user && user.last_try && Date.now() - user.last_try < GITHUB_USER_GET_TIMEOUT_MILLISECONDS) {
return {};
}
Expand All @@ -231,8 +236,8 @@ export function getGitHubUser(userID) {
let data;
try {
data = await Client.getGitHubUser(userID);
} catch (error) {
if (error.status === 404) {
} catch (error: unknown) {
if ((error as APIError).status_code === 404) {
dispatch({
type: ActionTypes.RECEIVED_GITHUB_USER,
userID,
Expand All @@ -256,21 +261,21 @@ export function getGitHubUser(userID) {
* 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,
};
}

export function openCreateIssueModal(postId) {
export function openCreateIssueModal(postId: string) {
return {
type: ActionTypes.OPEN_CREATE_ISSUE_MODAL,
data: {
Expand All @@ -279,7 +284,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 @@ -295,16 +300,16 @@ export function closeCreateIssueModal() {
};
}

export function createIssue(payload) {
return async (dispatch) => {
export function createIssue(payload: CreateIssuePayload) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.createIssue(payload);
} catch (error) {
return {error};
}

const connected = await dispatch(checkAndHandleNotConnected(data));
const connected = await checkAndHandleNotConnected(data);
if (!connected) {
return {error: data};
}
Expand All @@ -313,7 +318,7 @@ export function createIssue(payload) {
};
}

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

export function attachCommentToIssue(payload) {
return async (dispatch) => {
export function attachCommentToIssue(payload: AttachCommentToIssuePayload) {
return async (dispatch: DispatchFunc) => {
let data;
try {
data = await Client.attachCommentToIssue(payload);
} catch (error) {
return {error};
}

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

0 comments on commit 936e5c3

Please sign in to comment.