Skip to content

Commit

Permalink
Chore: Migrate RoomActionsView to Typescript (#3750)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolmello authored Mar 2, 2022
1 parent 6b3730c commit 6626510
Show file tree
Hide file tree
Showing 20 changed files with 213 additions and 153 deletions.
2 changes: 1 addition & 1 deletion app/actions/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ERoomType } from '../definitions/ERoomType';
import { ROOM } from './actionsTypes';

// TYPE RETURN RELATED
type ISelected = Record<string, string>;
type ISelected = string[];

export interface ITransferData {
roomId: string;
Expand Down
10 changes: 5 additions & 5 deletions app/actions/settings.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { Action } from 'redux';

import { ISettings, TSettings } from '../reducers/settings';
import { TSettingsState, TSupportedSettings, TSettingsValues } from '../reducers/settings';
import { SETTINGS } from './actionsTypes';

interface IAddSettings extends Action {
payload: ISettings;
payload: TSettingsState;
}

interface IUpdateSettings extends Action {
payload: { id: string; value: TSettings };
payload: { id: TSupportedSettings; value: TSettingsValues };
}

export type IActionSettings = IAddSettings & IUpdateSettings;

export function addSettings(settings: ISettings): IAddSettings {
export function addSettings(settings: TSettingsState): IAddSettings {
return {
type: SETTINGS.ADD,
payload: settings
};
}

export function updateSettings(id: string, value: TSettings): IUpdateSettings {
export function updateSettings(id: TSupportedSettings, value: TSettingsValues): IUpdateSettings {
return {
type: SETTINGS.UPDATE,
payload: { id, value }
Expand Down
2 changes: 1 addition & 1 deletion app/constants/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,4 @@ export default {
Canned_Responses_Enable: {
type: 'valueAsBoolean'
}
};
} as const;
5 changes: 3 additions & 2 deletions app/definitions/ISubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum SubscriptionType {
DIRECT = 'd',
CHANNEL = 'c',
OMNICHANNEL = 'l',
E2E = 'e2e',
E2E = 'e2e', // FIXME: this is not a type of subscription
THREAD = 'thread' // FIXME: this is not a type of subscription
}

Expand All @@ -36,7 +36,7 @@ export interface ISubscription {
_updatedAt?: string; // from server
v?: IVisitor;
f: boolean;
t: SubscriptionType;
t: string; // TODO: we need to review this type later
ts: string | Date;
ls: Date;
name: string;
Expand Down Expand Up @@ -81,6 +81,7 @@ export interface ISubscription {
usernames?: string[];
visitor?: IVisitor;
departmentId?: string;
status?: string;
servedBy?: IServedBy;
livechatData?: any;
tags?: string[];
Expand Down
1 change: 1 addition & 0 deletions app/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface IBaseScreen<T extends Record<string, object | undefined>, S ext
route: RouteProp<T, S>;
dispatch: Dispatch;
theme: string;
isMasterDetail: boolean;
}

export * from './redux';
Expand Down
6 changes: 3 additions & 3 deletions app/definitions/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ import { IRoles } from '../../reducers/roles';
import { IRoom } from '../../reducers/room';
import { ISelectedUsers } from '../../reducers/selectedUsers';
import { IServer } from '../../reducers/server';
import { ISettings } from '../../reducers/settings';
import { TSettingsState } from '../../reducers/settings';
import { IShare } from '../../reducers/share';
import { IPermissionsState } from '../../reducers/permissions';
import { IEnterpriseModules } from '../../reducers/enterpriseModules';

export interface IApplicationState {
settings: ISettings;
meteor: IConnect;
settings: TSettingsState;
login: ILogin;
meteor: IConnect;
server: IServer;
selectedUsers: ISelectedUsers;
app: IApp;
Expand Down
2 changes: 1 addition & 1 deletion app/lib/encryption/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const E2E_BANNER_TYPE = {
REQUEST_PASSWORD: 'REQUEST_PASSWORD',
SAVE_PASSWORD: 'SAVE_PASSWORD'
};
export const E2E_ROOM_TYPES = {
export const E2E_ROOM_TYPES: Record<string, string> = {
d: 'd',
p: 'p'
};
4 changes: 2 additions & 2 deletions app/lib/methods/getRoomInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IRoom } from '../../definitions';
import { IRoom, SubscriptionType } from '../../definitions';
import { getSubscriptionByRoomId } from '../database/services/Subscription';
import RocketChat from '../rocketchat';

Expand All @@ -10,7 +10,7 @@ const getRoomInfo = async (rid: string): Promise<Pick<IRoom, 'rid' | 'name' | 'f
rid,
name: result.name,
fname: result.fname,
t: result.t
t: result.t as SubscriptionType
};
}

Expand Down
8 changes: 6 additions & 2 deletions app/reducers/settings.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { addSettings, clearSettings, updateSettings } from '../actions/settings';
import { mockedStore } from './mockedStore';
import { initialState } from './settings';
import { initialState, TSettingsState } from './settings';

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

const settings = { API_Use_REST_For_DDP_Calls: true, FileUpload_MaxFileSize: 600857600, Jitsi_URL_Room_Prefix: 'RocketChat' };
const settings: TSettingsState = {
API_Use_REST_For_DDP_Calls: true,
FileUpload_MaxFileSize: 600857600,
Jitsi_URL_Room_Prefix: 'RocketChat'
};

it('should return modified store after call addSettings action', () => {
mockedStore.dispatch(addSettings(settings));
Expand Down
12 changes: 8 additions & 4 deletions app/reducers/settings.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { IActionSettings } from '../actions/settings';
import { SETTINGS } from '../actions/actionsTypes';
import settings from '../constants/settings';

export type TSettings = string | number | boolean | string[];
export type TSupportedSettings = keyof typeof settings;
export type TSettingsValues = string | number | boolean | string[];

export type ISettings = Record<string, TSettings>;
export type TSettingsState = {
[K in TSupportedSettings]?: TSettingsValues;
};

export const initialState: ISettings = {};
export const initialState: TSettingsState = {};

export default (state = initialState, action: IActionSettings): ISettings => {
export default (state = initialState, action: IActionSettings): TSettingsState => {
switch (action.type) {
case SETTINGS.ADD:
return {
Expand Down
42 changes: 22 additions & 20 deletions app/stacks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,37 @@ export type ChatsStackParamList = {
NewMessageStackNavigator: any;
NewMessageStack: undefined;
RoomsListView: undefined;
RoomView?: {
rid: string;
t: SubscriptionType;
tmid?: string;
message?: object; // TODO: TMessageModel?
name?: string;
fname?: string;
prid?: string;
room?: TSubscriptionModel | { rid: string; t: string; name?: string; fname?: string; prid?: string };
jumpToMessageId?: string;
jumpToThreadId?: string;
roomUserId?: string | null;
usedCannedResponse?: string;
};
RoomView:
| {
rid: string;
t: SubscriptionType;
tmid?: string;
message?: object; // TODO: TMessageModel?
name?: string;
fname?: string;
prid?: string;
room?: TSubscriptionModel | { rid: string; t: string; name?: string; fname?: string; prid?: string };
jumpToMessageId?: string;
jumpToThreadId?: string;
roomUserId?: string | null;
usedCannedResponse?: string;
}
| undefined; // Navigates back to RoomView already on stack
RoomActionsView: {
room?: ISubscription;
room: TSubscriptionModel;
member: any;
rid: string;
t: SubscriptionType;
joined: boolean;
};
SelectListView: {
data: IRoom[];
data?: IRoom[];
title: string;
infoText: string;
infoText?: string;
nextAction: (selected: string[]) => void;
showAlert: () => void;
isSearch: boolean;
onSearch: (text: string) => Partial<IRoom[]>;
showAlert?: () => void;
isSearch?: boolean;
onSearch?: (text: string) => Promise<Partial<IRoom[]> | any>;
isRadio?: boolean;
};
RoomInfoView: {
Expand Down
12 changes: 7 additions & 5 deletions app/utils/goRoom.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ChatsStackParamList } from '../stacks/types';
import Navigation from '../lib/Navigation';
import RocketChat from '../lib/rocketchat';
import { IVisitor, SubscriptionType } from '../definitions/ISubscription';
import { ISubscription, IVisitor, SubscriptionType, TSubscriptionModel } from '../definitions/ISubscription';

export interface IGoRoomItem {
interface IGoRoomItem {
search?: boolean; // comes from spotlight
username?: string;
t?: SubscriptionType;
Expand All @@ -13,12 +13,14 @@ export interface IGoRoomItem {
visitor?: IVisitor;
}

export type TGoRoomItem = IGoRoomItem | TSubscriptionModel | ISubscription;

const navigate = ({
item,
isMasterDetail,
...props
}: {
item: IGoRoomItem;
item: TGoRoomItem;
isMasterDetail: boolean;
navigationMethod?: () => ChatsStackParamList;
}) => {
Expand All @@ -45,13 +47,13 @@ export const goRoom = async ({
isMasterDetail = false,
...props
}: {
item: IGoRoomItem;
item: TGoRoomItem;
isMasterDetail: boolean;
navigationMethod?: any;
jumpToMessageId?: string;
usedCannedResponse?: string;
}): Promise<void> => {
if (item.t === SubscriptionType.DIRECT && item?.search) {
if (!('id' in item) && item.t === SubscriptionType.DIRECT && item?.search) {
// if user is using the search we need first to join/create room
try {
const { username } = item;
Expand Down
2 changes: 1 addition & 1 deletion app/views/CannedResponseDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const CannedResponseDetail = ({ navigation, route }: ICannedResponseDetailProps)
t: room.t,
fname: name
}),
t: room.t,
t: room.t as any,
roomUserId: RocketChat.getUidDirectMessage(room),
usedCannedResponse: item.text
};
Expand Down
2 changes: 1 addition & 1 deletion app/views/CannedResponsesListView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const CannedResponsesListView = ({ navigation, route }: ICannedResponsesListView
t: room.t,
fname: name
}),
t: room.t,
t: room.t as any,
roomUserId: RocketChat.getUidDirectMessage(room),
usedCannedResponse: item.text
};
Expand Down
Loading

0 comments on commit 6626510

Please sign in to comment.