Skip to content

Commit

Permalink
Improve Redux types part 5/Replace DispatchFunc (mattermost#26004)
Browse files Browse the repository at this point in the history
* Remove non-functional code from Login component

* Remove remaining usage of useDispatch<DispatchFunc>

* Remove usage of DispatchFunc from actions/file_actions

* actions/global_actions

* Remove usage of DispatchFunc from actions/marketplace

* Remove DispatchFunc from actions/post_actions

* Remove DispatchFunc from actions/storage

* Remove DispatchFunc from actions/user_actions

* Remove DispatchFunc from actions/views/channel

* Remove DispatchFunc from actions/views/channel_sidebar

* Remove DispatchFunc from actions/views/create_comment

* Remove DispatchFunc from actions/views/lhs.ts

* Remove DispatchFunc from actions/views/rhs.ts

* Remove DispatchFunc from actions/views/onboarding_tasks

* Remove DispatchFunc from actions/views/root

* Remove DispatchFunc from components/logged_in

* Remove DispatchFunc from components/msg_typing

* Remove DispatchFunc from components/permalink_view

* Remove DispatchFunc from components/suggestion

* Remove DispatchFunc from mattermost-redux/actions/posts

* Remove DispatchFunc from mattermost-redux/actions/roles

* Remove DispatchFunc from mattermost-redux/actions/threads

* Remove DispatchFunc from mattermost-redux/actions/timezone

* Remove DispatchFunc from plugins/products

* Make DispatchFunc into Dispatch

* Fix how dispatch is mocked in test for toggleSideBarRightMenuAction
  • Loading branch information
hmhealey authored Jan 24, 2024
1 parent d8e11fe commit 8e165c7
Show file tree
Hide file tree
Showing 72 changed files with 450 additions and 539 deletions.
6 changes: 3 additions & 3 deletions webapp/channels/src/actions/file_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {FileTypes} from 'mattermost-redux/action_types';
import {getLogErrorAction} from 'mattermost-redux/actions/errors';
import {forceLogoutIfNecessary} from 'mattermost-redux/actions/helpers';
import {Client4} from 'mattermost-redux/client';
import type {DispatchFunc, GetStateFunc} from 'mattermost-redux/types/actions';
import type {ThunkActionFunc} from 'mattermost-redux/types/actions';

import type {FilePreviewInfo} from 'components/file_preview/file_preview';

Expand All @@ -28,8 +28,8 @@ export interface UploadFile {
onError: (err: string | ServerError, clientId: string, channelId: string, rootId: string) => void;
}

export function uploadFile({file, name, type, rootId, channelId, clientId, onProgress, onSuccess, onError}: UploadFile) {
return (dispatch: DispatchFunc, getState: GetStateFunc): XMLHttpRequest => {
export function uploadFile({file, name, type, rootId, channelId, clientId, onProgress, onSuccess, onError}: UploadFile): ThunkActionFunc<XMLHttpRequest> {
return (dispatch, getState) => {
dispatch({type: FileTypes.UPLOAD_FILES_REQUEST});

const xhr = new XMLHttpRequest();
Expand Down
8 changes: 5 additions & 3 deletions webapp/channels/src/actions/global_actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,12 @@ describe('actions/global_actions', () => {
});

test('toggleSideBarRightMenuAction', () => {
const dispatchMock = async () => {
return {data: true};
const dispatchMock = (arg: any) => {
if (typeof arg === 'function') {
arg(dispatchMock);
}
};
toggleSideBarRightMenuAction()(dispatchMock);
dispatchMock(toggleSideBarRightMenuAction());
expect(closeRhsMenu).toHaveBeenCalled();
expect(closeRightHandSide).toHaveBeenCalled();
expect(closeLhs).toHaveBeenCalled();
Expand Down
8 changes: 4 additions & 4 deletions webapp/channels/src/actions/global_actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {getConfig, isPerformanceDebuggingEnabled} from 'mattermost-redux/selecto
import {getBool, getIsOnboardingFlowEnabled, isCollapsedThreadsEnabled} from 'mattermost-redux/selectors/entities/preferences';
import {getCurrentTeamId, getMyTeams, getTeam, getMyTeamMember, getTeamMemberships, getActiveTeamsList} from 'mattermost-redux/selectors/entities/teams';
import {getCurrentUser, getCurrentUserId, isFirstAdmin} from 'mattermost-redux/selectors/entities/users';
import type {DispatchFunc, GetStateFunc, NewActionFuncAsync} from 'mattermost-redux/types/actions';
import type {NewActionFuncAsync, ThunkActionFunc} from 'mattermost-redux/types/actions';
import {calculateUnreadCount} from 'mattermost-redux/utils/channel_utils';

import {handleNewPost} from 'actions/post_actions';
Expand Down Expand Up @@ -229,7 +229,7 @@ export function sendAddToChannelEphemeralPost(user: UserProfile, addedUsername:

let lastTimeTypingSent = 0;
export function emitLocalUserTypingEvent(channelId: string, parentPostId: string) {
const userTyping = async (actionDispatch: DispatchFunc, actionGetState: GetStateFunc) => {
const userTyping: NewActionFuncAsync = async (actionDispatch, actionGetState) => {
const state = actionGetState();
const config = getConfig(state);

Expand Down Expand Up @@ -282,8 +282,8 @@ export function emitUserLoggedOutEvent(redirectTo = '/', shouldSignalLogout = tr
});
}

export function toggleSideBarRightMenuAction() {
return (doDispatch: DispatchFunc) => {
export function toggleSideBarRightMenuAction(): ThunkActionFunc<void> {
return (doDispatch) => {
doDispatch(closeRightHandSide());
doDispatch(closeLhs());
doDispatch(closeRhsMenu());
Expand Down
6 changes: 3 additions & 3 deletions webapp/channels/src/actions/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {AppBindingLocations, AppCallResponseTypes} from 'mattermost-redux/consta
import {appsEnabled} from 'mattermost-redux/selectors/entities/apps';
import {getCurrentChannelId} from 'mattermost-redux/selectors/entities/channels';
import {getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import type {DispatchFunc, GetStateFunc, NewActionFuncAsync, ThunkActionFunc} from 'mattermost-redux/types/actions';
import type {NewActionFuncAsync, ThunkActionFunc} from 'mattermost-redux/types/actions';

import {getFilter, getPlugin} from 'selectors/views/marketplace';

Expand Down Expand Up @@ -142,8 +142,8 @@ export function installPlugin(id: string): ThunkActionFunc<void, GlobalState> {
// installApp installed an App using a given URL a call to the `/install-listed` call path.
//
// On success, it also requests the current state of the apps to reflect the newly installed app.
export function installApp(id: string): ThunkActionFunc<Promise<boolean>> {
return async (dispatch: DispatchFunc, getState: GetStateFunc): Promise<boolean> => {
export function installApp(id: string): ThunkActionFunc<Promise<boolean>, GlobalState> {
return async (dispatch, getState) => {
dispatch({
type: ActionTypes.INSTALLING_MARKETPLACE_ITEM,
id,
Expand Down
58 changes: 29 additions & 29 deletions webapp/channels/src/actions/post_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as PostSelectors from 'mattermost-redux/selectors/entities/posts';
import {isCollapsedThreadsEnabled} from 'mattermost-redux/selectors/entities/preferences';
import {getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {getCurrentUserId, isCurrentUserSystemAdmin} from 'mattermost-redux/selectors/entities/users';
import type {DispatchFunc, GetStateFunc, NewActionFunc, NewActionFuncAsync} from 'mattermost-redux/types/actions';
import type {DispatchFunc, NewActionFunc, NewActionFuncAsync, ThunkActionFunc} from 'mattermost-redux/types/actions';
import {canEditPost, comparePosts} from 'mattermost-redux/utils/post_utils';

import {addRecentEmoji, addRecentEmojis} from 'actions/emoji_actions';
Expand Down Expand Up @@ -78,8 +78,8 @@ export function handleNewPost(post: Post, msg?: {data?: NewPostMessageProps & Gr

const getPostsForIds = PostSelectors.makeGetPostsForIds();

export function flagPost(postId: string) {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
export function flagPost(postId: string): NewActionFuncAsync {
return async (dispatch, getState) => {
await dispatch(PostActions.flagPost(postId));
const state = getState() as GlobalState;
const rhsState = getRhsState(state);
Expand All @@ -92,8 +92,8 @@ export function flagPost(postId: string) {
};
}

export function unflagPost(postId: string) {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
export function unflagPost(postId: string): NewActionFuncAsync {
return async (dispatch, getState) => {
await dispatch(PostActions.unflagPost(postId));
const state = getState() as GlobalState;
const rhsState = getRhsState(state);
Expand All @@ -106,8 +106,8 @@ export function unflagPost(postId: string) {
};
}

export function createPost(post: Post, files: FileInfo[]) {
return async (dispatch: DispatchFunc) => {
export function createPost(post: Post, files: FileInfo[]): NewActionFuncAsync {
return async (dispatch) => {
// parse message and emit emoji event
const emojis = matchEmoticons(post.message);
if (emojis) {
Expand All @@ -132,15 +132,15 @@ export function createPost(post: Post, files: FileInfo[]) {
};
}

function storeDraft(channelId: string, draft: null) {
return (dispatch: DispatchFunc) => {
function storeDraft(channelId: string, draft: null): NewActionFunc {
return (dispatch) => {
dispatch(StorageActions.setGlobalItem('draft_' + channelId, draft));
return {data: true};
};
}

function storeCommentDraft(rootPostId: string, draft: null) {
return (dispatch: DispatchFunc) => {
function storeCommentDraft(rootPostId: string, draft: null): NewActionFunc {
return (dispatch) => {
dispatch(StorageActions.setGlobalItem('comment_draft_' + rootPostId, draft));
return {data: true};
};
Expand Down Expand Up @@ -202,16 +202,16 @@ export function addReaction(postId: string, emojiName: string): NewActionFunc {
};
}

export function searchForTerm(term: string) {
return (dispatch: DispatchFunc) => {
export function searchForTerm(term: string): NewActionFunc<boolean, GlobalState> {
return (dispatch) => {
dispatch(RhsActions.updateSearchTerms(term));
dispatch(RhsActions.showSearchResults());
return {data: true};
};
}

function addPostToSearchResults(postId: string) {
return (dispatch: DispatchFunc, getState: GetStateFunc) => {
function addPostToSearchResults(postId: string): NewActionFunc {
return (dispatch, getState) => {
const state = getState();
const results = state.entities.search.results;
const index = results.indexOf(postId);
Expand Down Expand Up @@ -251,10 +251,10 @@ function removePostFromSearchResults(postId: string, state: GlobalState, dispatc
}
}

export function pinPost(postId: string) {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
export function pinPost(postId: string): NewActionFuncAsync<boolean, GlobalState> {
return async (dispatch, getState) => {
await dispatch(PostActions.pinPost(postId));
const state = getState() as GlobalState;
const state = getState();
const rhsState = getRhsState(state);

if (rhsState === RHSStates.PIN) {
Expand All @@ -264,10 +264,10 @@ export function pinPost(postId: string) {
};
}

export function unpinPost(postId: string) {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
export function unpinPost(postId: string): NewActionFuncAsync<boolean, GlobalState> {
return async (dispatch, getState) => {
await dispatch(PostActions.unpinPost(postId));
const state = getState() as GlobalState;
const state = getState();
const rhsState = getRhsState(state);

if (rhsState === RHSStates.PIN) {
Expand Down Expand Up @@ -337,8 +337,8 @@ export function markPostAsUnread(post: Post, location?: string): NewActionFuncAs
};
}

export function markMostRecentPostInChannelAsUnread(channelId: string) {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
export function markMostRecentPostInChannelAsUnread(channelId: string): NewActionFuncAsync {
return async (dispatch, getState) => {
let state = getState();
let postId = PostSelectors.getMostRecentPostIdInChannel(state, channelId);
if (!postId) {
Expand Down Expand Up @@ -392,11 +392,11 @@ export function deleteAndRemovePost(post: Post): NewActionFuncAsync<boolean, Glo
};
}

export function toggleEmbedVisibility(postId: string) {
return (dispatch: DispatchFunc, getState: GetStateFunc) => {
export function toggleEmbedVisibility(postId: string): ThunkActionFunc<void, GlobalState> {
return (dispatch, getState) => {
const state = getState();
const currentUserId = getCurrentUserId(state);
const visible = isEmbedVisible(state as GlobalState, postId);
const visible = isEmbedVisible(state, postId);

dispatch(StorageActions.setGlobalItem(StoragePrefixes.EMBED_VISIBLE + currentUserId + '_' + postId, !visible));
};
Expand All @@ -406,11 +406,11 @@ export function resetEmbedVisibility() {
return StorageActions.actionOnGlobalItemsWithPrefix(StoragePrefixes.EMBED_VISIBLE, () => null);
}

export function toggleInlineImageVisibility(postId: string, imageKey: string) {
return (dispatch: DispatchFunc, getState: GetStateFunc) => {
export function toggleInlineImageVisibility(postId: string, imageKey: string): ThunkActionFunc<void, GlobalState> {
return (dispatch, getState) => {
const state = getState();
const currentUserId = getCurrentUserId(state);
const visible = isInlineImageVisible(state as GlobalState, postId, imageKey);
const visible = isInlineImageVisible(state, postId, imageKey);

dispatch(StorageActions.setGlobalItem(StoragePrefixes.INLINE_IMAGE_VISIBLE + currentUserId + '_' + postId + '_' + imageKey, !visible));
};
Expand Down
2 changes: 1 addition & 1 deletion webapp/channels/src/actions/status_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function loadStatusesForProfilesList(users: UserProfile[] | null): NewAct
};
}

export function loadStatusesForProfilesMap(users: Record<string, UserProfile> | null): NewActionFunc {
export function loadStatusesForProfilesMap(users: Record<string, UserProfile> | UserProfile[] | null): NewActionFunc {
return (dispatch) => {
if (users == null) {
return {data: false};
Expand Down
14 changes: 7 additions & 7 deletions webapp/channels/src/actions/storage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import type {DispatchFunc, GetStateFunc} from 'mattermost-redux/types/actions';
import type {NewActionFunc} from 'mattermost-redux/types/actions';

import {StorageTypes} from 'utils/constants';
import {getPrefix} from 'utils/storage_utils';

export function setItem(name: string, value: string) {
return (dispatch: DispatchFunc, getState: GetStateFunc) => {
export function setItem(name: string, value: string): NewActionFunc {
return (dispatch, getState) => {
const state = getState();
const prefix = getPrefix(state);
dispatch({
Expand All @@ -18,8 +18,8 @@ export function setItem(name: string, value: string) {
};
}

export function removeItem(name: string) {
return (dispatch: DispatchFunc, getState: GetStateFunc) => {
export function removeItem(name: string): NewActionFunc { // HARRISONTODO unused
return (dispatch, getState) => {
const state = getState();
const prefix = getPrefix(state);
dispatch({
Expand All @@ -37,8 +37,8 @@ export function setGlobalItem(name: string, value: any) {
};
}

export function removeGlobalItem(name: string) {
return (dispatch: DispatchFunc) => {
export function removeGlobalItem(name: string): NewActionFunc {
return (dispatch) => {
dispatch({
type: StorageTypes.REMOVE_GLOBAL_ITEM,
data: {name},
Expand Down
Loading

0 comments on commit 8e165c7

Please sign in to comment.