Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Migrate redux module createChannel to typescript #3602

Merged
merged 14 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions app/actions/createChannel.js

This file was deleted.

41 changes: 41 additions & 0 deletions app/actions/createChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Action } from 'redux';

import { TCreateChannelResult } from '../reducers/createChannel';
import { CREATE_CHANNEL } from './actionsTypes';

interface ICreateChannelRequest extends Action {
data: TCreateChannelResult;
}

interface ICreateChannelSuccess extends Action {
data: TCreateChannelResult;
}

interface ICreateChannelFailure extends Action {
err: any;
isTeam: boolean;
}

export type TActionCreateChannel = ICreateChannelRequest & ICreateChannelSuccess & ICreateChannelFailure;

export function createChannelRequest(data: TCreateChannelResult): ICreateChannelRequest {
return {
type: CREATE_CHANNEL.REQUEST,
data
};
}

export function createChannelSuccess(data: TCreateChannelResult): ICreateChannelSuccess {
return {
type: CREATE_CHANNEL.SUCCESS,
data
};
}

export function createChannelFailure(err: any, isTeam: boolean): ICreateChannelFailure {
return {
type: CREATE_CHANNEL.FAILURE,
err,
isTeam
};
}
5 changes: 4 additions & 1 deletion app/definitions/redux/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ACTIONS
import { TActionActiveUsers } from '../../actions/activeUsers';
import { TActionCreateChannel } from '../../actions/createChannel';
import { TActionCustomEmojis } from '../../actions/customEmojis';
import { TActionEncryption } from '../../actions/encryption';
import { TActionInviteLinks } from '../../actions/inviteLinks';
Expand All @@ -13,6 +14,7 @@ import { TActionUserTyping } from '../../actions/usersTyping';
// REDUCERS
import { IActiveUsers } from '../../reducers/activeUsers';
import { IConnect } from '../../reducers/connect';
import { ICreateChannel } from '../../reducers/createChannel';
import { IEncryption } from '../../reducers/encryption';
import { IInviteLinks } from '../../reducers/inviteLinks';
import { IRoles } from '../../reducers/roles';
Expand All @@ -27,7 +29,7 @@ export interface IApplicationState {
meteor: IConnect;
server: IServer;
selectedUsers: ISelectedUsers;
createChannel: any;
createChannel: ICreateChannel;
app: any;
room: any;
rooms: any;
Expand All @@ -54,5 +56,6 @@ export type TApplicationActions = TActionActiveUsers &
TActionEncryption &
TActionSortPreferences &
TActionUserTyping &
TActionCreateChannel &
TActionsShare &
TActionServer;
36 changes: 0 additions & 36 deletions app/reducers/createChannel.js

This file was deleted.

44 changes: 44 additions & 0 deletions app/reducers/createChannel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createChannelRequest, createChannelSuccess, createChannelFailure } from '../actions/createChannel';
import { initialState } from './createChannel';
import { mockedStore } from './mockedStore';

describe('test reducer', () => {
const data = {
name: 'test',
users: ['diego', 'karla'],
type: true,
readOnly: true,
broadcast: true,
encrypted: true,
isTeam: true,
teamId: 'xxx'
};

it('should return initial state', () => {
const { createChannel } = mockedStore.getState();
expect(createChannel).toEqual(initialState);
});

it('should return correct createChannel state after dispatch createChannelRequest action', () => {
mockedStore.dispatch(createChannelRequest(data));
const { createChannel } = mockedStore.getState();
expect(createChannel).toEqual({ isFetching: true, failure: false, error: {}, result: {} });
});

it('should return correct createChannel state after dispatch createChannelSuccess action', () => {
mockedStore.dispatch(createChannelSuccess(data));
const { createChannel } = mockedStore.getState();
expect(createChannel).toEqual({ isFetching: false, failure: false, result: { ...data }, error: {} });
});

it('should return correct createChannel state after dispatch createChannelFailure action', () => {
mockedStore.dispatch(createChannelFailure({ err: true }, true));
const { createChannel } = mockedStore.getState();
expect(createChannel).toEqual({
isFetching: false,
failure: true,
result: { ...data },
error: { err: true }
});
});
});
61 changes: 61 additions & 0 deletions app/reducers/createChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { TApplicationActions } from '../definitions';
import { CREATE_CHANNEL } from '../actions/actionsTypes';

interface ICreateChannelResult {
name: string;
users: string[];
teamId: string;
type: boolean;
readOnly: boolean;
encrypted: boolean;
broadcast: boolean;
isTeam: boolean;
}

interface ICreateChannelResultOnlyGroup {
group: boolean;
}

export type TCreateChannelResult = ICreateChannelResult | ICreateChannelResultOnlyGroup;

export interface ICreateChannel {
isFetching: boolean;
failure: boolean;
result: TCreateChannelResult | {};
error: any;
}

export const initialState: ICreateChannel = {
isFetching: false,
failure: false,
result: {},
error: {}
};

export default function (state = initialState, action: TApplicationActions): ICreateChannel {
switch (action.type) {
case CREATE_CHANNEL.REQUEST:
return {
...state,
isFetching: true,
failure: false,
error: {}
};
case CREATE_CHANNEL.SUCCESS:
return {
...state,
isFetching: false,
failure: false,
result: action.data
};
case CREATE_CHANNEL.FAILURE:
return {
...state,
isFetching: false,
failure: true,
error: action.err
};
default:
return state;
}
}
42 changes: 12 additions & 30 deletions app/views/CreateChannelView.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import React from 'react';
import { connect } from 'react-redux';
import { Dispatch } from 'redux';
import { StackNavigationProp } from '@react-navigation/stack';
import { RouteProp } from '@react-navigation/native';
import { FlatList, ScrollView, StyleSheet, Switch, Text, View, SwitchProps } from 'react-native';
import { dequal } from 'dequal';

import * as List from '../containers/List';
import TextInput from '../presentation/TextInput';
import Loading from '../containers/Loading';
import { createChannelRequest as createChannelRequestAction } from '../actions/createChannel';
import { removeUser as removeUserAction } from '../actions/selectedUsers';
import { createChannelRequest } from '../actions/createChannel';
import { removeUser } from '../actions/selectedUsers';
import KeyboardView from '../presentation/KeyboardView';
import scrollPersistTaps from '../utils/scrollPersistTaps';
import I18n from '../i18n';
Expand All @@ -26,6 +23,7 @@ import SafeAreaView from '../containers/SafeAreaView';
import RocketChat from '../lib/rocketchat';
import sharedStyles from './Styles';
import { ChatsStackParamList } from '../stacks/types';
import { IApplicationState, IBaseScreen } from '../definitions';

const styles = StyleSheet.create({
container: {
Expand Down Expand Up @@ -75,12 +73,6 @@ interface IOtherUser {
fname: string;
}

interface ICreateFunction extends Omit<ICreateChannelViewState, 'channelName' | 'permissions'> {
name: string;
users: string[];
teamId: string;
}

interface ICreateChannelViewState {
channelName: string;
type: boolean;
Expand All @@ -91,12 +83,8 @@ interface ICreateChannelViewState {
permissions: boolean[];
}

interface ICreateChannelViewProps {
navigation: StackNavigationProp<ChatsStackParamList, 'CreateChannelView'>;
route: RouteProp<ChatsStackParamList, 'CreateChannelView'>;
interface ICreateChannelViewProps extends IBaseScreen<ChatsStackParamList, 'CreateChannelView'> {
baseUrl: string;
create: (data: ICreateFunction) => void;
removeUser: (user: IOtherUser) => void;
error: object;
failure: boolean;
isFetching: boolean;
Expand All @@ -107,7 +95,6 @@ interface ICreateChannelViewProps {
token: string;
roles: string[];
};
theme: string;
teamId: string;
createPublicChannelPermission: string[];
createPrivateChannelPermission: string[];
Expand Down Expand Up @@ -223,7 +210,7 @@ class CreateChannelView extends React.Component<ICreateChannelViewProps, ICreate

submit = () => {
const { channelName, type, readOnly, broadcast, encrypted, isTeam } = this.state;
const { users: usersProps, isFetching, create } = this.props;
const { users: usersProps, isFetching, dispatch } = this.props;

if (!channelName.trim() || isFetching) {
return;
Expand All @@ -233,7 +220,7 @@ class CreateChannelView extends React.Component<ICreateChannelViewProps, ICreate
const users = usersProps.map(user => user.name);

// create channel or team
create({
const data = {
name: channelName,
users,
type,
Expand All @@ -242,15 +229,15 @@ class CreateChannelView extends React.Component<ICreateChannelViewProps, ICreate
encrypted,
isTeam,
teamId: this.teamId!
});

};
dispatch(createChannelRequest(data));
Review.pushPositiveEvent();
};

removeUser = (user: IOtherUser) => {
logEvent(events.CR_REMOVE_USER);
const { removeUser } = this.props;
removeUser(user);
const { dispatch } = this.props;
dispatch(removeUser(user));
};

renderSwitch = ({ id, value, label, onValueChange, disabled = false }: ISwitch) => {
Expand Down Expand Up @@ -434,7 +421,7 @@ class CreateChannelView extends React.Component<ICreateChannelViewProps, ICreate
}
}

const mapStateToProps = (state: any) => ({
const mapStateToProps = (state: IApplicationState) => ({
baseUrl: state.server.server,
isFetching: state.createChannel.isFetching,
encryptionEnabled: state.encryption.enabled,
Expand All @@ -444,9 +431,4 @@ const mapStateToProps = (state: any) => ({
createPrivateChannelPermission: state.permissions['create-p']
});

const mapDispatchToProps = (dispatch: Dispatch) => ({
create: (data: ICreateFunction) => dispatch(createChannelRequestAction(data)),
removeUser: (user: IOtherUser) => dispatch(removeUserAction(user))
});

export default connect(mapStateToProps, mapDispatchToProps)(withTheme(CreateChannelView));
export default connect(mapStateToProps)(withTheme(CreateChannelView));
Loading