Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3b97c34
chore: migrate activeUsers reducer and action to TS
dnlsilva Dec 21, 2021
34e6ece
chore: init types folder and set redux and BaseScreen interface
dnlsilva Dec 21, 2021
9d7450b
chore: remove mapDispatchToProps to use dispatch prop and clear some …
dnlsilva Dec 21, 2021
003f9fc
chore: type selectedUsers action and reducer and improvement in the c…
dnlsilva Dec 21, 2021
010676a
chore: move IUser to base types
dnlsilva Dec 21, 2021
dc83f76
chore: move state props to ISelectedUsersViewProps
dnlsilva Dec 21, 2021
f169f6e
chore: create mocketStore
dnlsilva Dec 21, 2021
69f2743
chore: remove applyAppStateMiddleware
dnlsilva Dec 21, 2021
b80c063
test: create activeUser and selectedUser tests
dnlsilva Dec 21, 2021
b5bda75
test: add more selectedUsers tests
dnlsilva Dec 22, 2021
b56f604
chore: fix action type
dnlsilva Dec 22, 2021
ff0925c
chore: move types to definition folder and fix imports
dnlsilva Dec 22, 2021
ef0d2bf
chore: remove unused const
dnlsilva Dec 22, 2021
d12b28b
chore: migrate redux tests to reducer folder and add eslint jest plugin
dnlsilva Dec 22, 2021
f0977dd
chore: exprot initial state and then import on tests
dnlsilva Dec 22, 2021
bad5643
chore: move interfaces to reducer and import on screen
dnlsilva Dec 22, 2021
928fac1
chore: set eslint-plugin-jest version to 24.7.0
dnlsilva Dec 23, 2021
c471195
chore: fix IUser import
dnlsilva Dec 23, 2021
48e4640
chore: update interfaces and types names
dnlsilva Dec 29, 2021
0cf9765
chore: update definitions
dnlsilva Dec 29, 2021
bc92a4f
chore: update IBaseScreen definitions
dnlsilva Dec 29, 2021
16e04c7
chore: init reducer/app migration to ts
dnlsilva Jan 7, 2022
701361a
chore: add tests and migrate RootEnum
dnlsilva Jan 7, 2022
ba29316
wip: migrate fixed consts to RootEnum
dnlsilva Jan 7, 2022
93a1cfe
Merge branch 'develop' into chore/migration-ts-redux-app
dnlsilva Jan 11, 2022
a44a695
chore: remove redux action inferences
dnlsilva Jan 11, 2022
d551424
Merge branch 'develop' into chore/migration-ts-redux-app
diegolmello Jan 13, 2022
d5281bc
Merge branch 'develop' into chore/migration-ts-redux-app
dnlsilva Jan 26, 2022
9e092d5
Merge branch 'develop' into chore/migration-ts-redux-app
dnlsilva Feb 2, 2022
1934a74
fix types
dnlsilva Feb 2, 2022
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
12 changes: 6 additions & 6 deletions app/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { connect } from 'react-redux';
import { SetUsernameStackParamList, StackParamList } from './definitions/navigationTypes';
import Navigation from './lib/Navigation';
import { defaultHeader, getActiveRouteName, navigationTheme } from './utils/navigation';
import { ROOT_INSIDE, ROOT_LOADING, ROOT_OUTSIDE, ROOT_SET_USERNAME } from './actions/app';
import { RootEnum } from './definitions';
// Stacks
import AuthLoadingView from './views/AuthLoadingView';
// SetUsername Stack
Expand Down Expand Up @@ -56,13 +56,13 @@ const App = React.memo(({ root, isMasterDetail }: { root: string; isMasterDetail
}}>
<Stack.Navigator screenOptions={{ headerShown: false, animationEnabled: false }}>
<>
{root === ROOT_LOADING ? <Stack.Screen name='AuthLoading' component={AuthLoadingView} /> : null}
{root === ROOT_OUTSIDE ? <Stack.Screen name='OutsideStack' component={OutsideStack} /> : null}
{root === ROOT_INSIDE && isMasterDetail ? (
{root === RootEnum.ROOT_LOADING ? <Stack.Screen name='AuthLoading' component={AuthLoadingView} /> : null}
{root === RootEnum.ROOT_OUTSIDE ? <Stack.Screen name='OutsideStack' component={OutsideStack} /> : null}
{root === RootEnum.ROOT_INSIDE && isMasterDetail ? (
<Stack.Screen name='MasterDetailStack' component={MasterDetailStack} />
) : null}
{root === ROOT_INSIDE && !isMasterDetail ? <Stack.Screen name='InsideStack' component={InsideStack} /> : null}
{root === ROOT_SET_USERNAME ? <Stack.Screen name='SetUsernameStack' component={SetUsernameStack} /> : null}
{root === RootEnum.ROOT_INSIDE && !isMasterDetail ? <Stack.Screen name='InsideStack' component={InsideStack} /> : null}
{root === RootEnum.ROOT_SET_USERNAME ? <Stack.Screen name='SetUsernameStack' component={SetUsernameStack} /> : null}
</>
</Stack.Navigator>
</NavigationContainer>
Expand Down
4 changes: 2 additions & 2 deletions app/actions/actionsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const REQUEST = 'REQUEST';
const SUCCESS = 'SUCCESS';
const FAILURE = 'FAILURE';
const defaultTypes = [REQUEST, SUCCESS, FAILURE];
function createRequestTypes(base = {}, types = defaultTypes): Record<any, any> {
const res: Record<any, any> = {};
function createRequestTypes(base = {}, types = defaultTypes): Record<string, string> {
const res: Record<string, string> = {};
types.forEach(type => (res[type] = `${base}_${type}`));
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion app/actions/activeUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Action } from 'redux';
import { IActiveUsers } from '../reducers/activeUsers';
import { SET_ACTIVE_USERS } from './actionsTypes';

export interface ISetActiveUsers extends Action {
interface ISetActiveUsers extends Action {
activeUsers: IActiveUsers;
}

Expand Down
39 changes: 0 additions & 39 deletions app/actions/app.js

This file was deleted.

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

import { RootEnum } from '../definitions';
import { APP } from './actionsTypes';

interface IAppStart extends Action {
root: RootEnum;
text?: string;
}

interface ISetMasterDetail extends Action {
isMasterDetail: boolean;
}

export type TActionApp = IAppStart & ISetMasterDetail;

interface Params {
root: RootEnum;
[key: string]: any;
}

export function appStart({ root, ...args }: Params): IAppStart {
return {
type: APP.START,
root,
...args
};
}

export function appReady(): Action {
return {
type: APP.READY
};
}

export function appInit(): Action {
return {
type: APP.INIT
};
}

export function appInitLocalSettings(): Action {
return {
type: APP.INIT_LOCAL_SETTINGS
};
}

export function setMasterDetail(isMasterDetail: boolean): ISetMasterDetail {
return {
type: APP.SET_MASTER_DETAIL,
isMasterDetail
};
}
1 change: 1 addition & 0 deletions app/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export interface IBaseScreen<T extends Record<string, object | undefined>, S ext
}

export * from './redux';
export * from './redux/TRootEnum';
6 changes: 6 additions & 0 deletions app/definitions/redux/TRootEnum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum RootEnum {
ROOT_OUTSIDE = 'outside',
ROOT_INSIDE = 'inside',
ROOT_LOADING = 'loading',
ROOT_SET_USERNAME = 'setUsername'
}
7 changes: 5 additions & 2 deletions 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 { TActionApp } from '../../actions/app';
import { TActionCreateChannel } from '../../actions/createChannel';
import { TActionCustomEmojis } from '../../actions/customEmojis';
import { TActionEncryption } from '../../actions/encryption';
Expand All @@ -13,6 +14,7 @@ import { TActionSortPreferences } from '../../actions/sortPreferences';
import { TActionUserTyping } from '../../actions/usersTyping';
// REDUCERS
import { IActiveUsers } from '../../reducers/activeUsers';
import { IApp } from '../../reducers/app';
import { IConnect } from '../../reducers/connect';
import { ICreateChannel } from '../../reducers/createChannel';
import { IEncryption } from '../../reducers/encryption';
Expand All @@ -29,8 +31,8 @@ export interface IApplicationState {
meteor: IConnect;
server: IServer;
selectedUsers: ISelectedUsers;
app: IApp;
createChannel: ICreateChannel;
app: any;
room: any;
rooms: any;
sortPreferences: any;
Expand Down Expand Up @@ -58,4 +60,5 @@ export type TApplicationActions = TActionActiveUsers &
TActionUserTyping &
TActionCreateChannel &
TActionsShare &
TActionServer;
TActionServer &
TActionApp;
44 changes: 44 additions & 0 deletions app/reducers/app.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { appStart, appInit, setMasterDetail } from '../actions/app';
import { initialState } from './app';
import { mockedStore } from './mockedStore';
import { RootEnum } from '../definitions';
import { APP_STATE } from '../actions/actionsTypes';

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

it('should return root state after dispatch appStart action', () => {
mockedStore.dispatch(appStart({ root: RootEnum.ROOT_INSIDE }));
const { root } = mockedStore.getState().app;
expect(root).toEqual(RootEnum.ROOT_INSIDE);
});

it('should return ready state after dispatch appInit action', () => {
mockedStore.dispatch(appInit());
const { ready } = mockedStore.getState().app;
expect(ready).toEqual(false);
});

it('should return ready state after dispatch setMasterDetail action', () => {
mockedStore.dispatch(setMasterDetail(false));
const { isMasterDetail } = mockedStore.getState().app;
expect(isMasterDetail).toEqual(false);
});

it('should return correct state after app go to foreground', () => {
mockedStore.dispatch({ type: APP_STATE.FOREGROUND });
const { foreground, background } = mockedStore.getState().app;
expect(foreground).toEqual(true);
expect(background).toEqual(false);
});

it('should return correct state after app go to background', () => {
mockedStore.dispatch({ type: APP_STATE.BACKGROUND });
const { foreground, background } = mockedStore.getState().app;
expect(foreground).toEqual(false);
expect(background).toEqual(true);
});
});
19 changes: 15 additions & 4 deletions app/reducers/app.js → app/reducers/app.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { TActionApp } from '../actions/app';
import { RootEnum } from '../definitions';
import { APP, APP_STATE } from '../actions/actionsTypes';

const initialState = {
root: null,
export interface IApp {
root?: RootEnum;
isMasterDetail: boolean;
text?: string;
ready: boolean;
foreground: boolean;
background: boolean;
}

export const initialState: IApp = {
root: undefined,
isMasterDetail: false,
text: null,
text: undefined,
ready: false,
foreground: true,
background: false
};

export default function app(state = initialState, action) {
export default function app(state = initialState, action: TActionApp): IApp {
switch (action.type) {
case APP_STATE.FOREGROUND:
return {
Expand Down
7 changes: 4 additions & 3 deletions app/sagas/deepLinking.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { inviteLinksRequest, inviteLinksSetToken } from '../actions/inviteLinks'
import database from '../lib/database';
import RocketChat from '../lib/rocketchat';
import EventEmitter from '../utils/events';
import { ROOT_INSIDE, ROOT_OUTSIDE, appInit, appStart } from '../actions/app';
import { appInit, appStart } from '../actions/app';
import { localAuthenticate } from '../utils/localAuthentication';
import { goRoom } from '../utils/goRoom';
import { loginRequest } from '../actions/login';
import log from '../utils/log';
import { RootEnum } from '../definitions';

const roomTypes = {
channel: 'c',
Expand Down Expand Up @@ -41,7 +42,7 @@ const popToRoot = function popToRoot({ isMasterDetail }) {
};

const navigate = function* navigate({ params }) {
yield put(appStart({ root: ROOT_INSIDE }));
yield put(appStart({ root: RootEnum.ROOT_INSIDE }));
if (params.path || params.rid) {
let type;
let name;
Expand Down Expand Up @@ -192,7 +193,7 @@ const handleOpen = function* handleOpen({ params }) {
yield fallbackNavigation();
return;
}
yield put(appStart({ root: ROOT_OUTSIDE }));
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
yield put(serverInitAdd(server));
yield delay(1000);
EventEmitter.emit('NewServer', { server: host });
Expand Down
9 changes: 5 additions & 4 deletions app/sagas/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import RocketChat from '../lib/rocketchat';
import log from '../utils/log';
import database from '../lib/database';
import { localAuthenticate } from '../utils/localAuthentication';
import { ROOT_OUTSIDE, appReady, appStart } from '../actions/app';
import { appReady, appStart } from '../actions/app';
import { RootEnum } from '../definitions';

export const initLocalSettings = function* initLocalSettings() {
const sortPreferences = yield RocketChat.getSortPreferences();
Expand All @@ -22,7 +23,7 @@ const restore = function* restore() {
let userId = yield UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`);

if (!server) {
yield put(appStart({ root: ROOT_OUTSIDE }));
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
} else if (!userId) {
const serversDB = database.servers;
const serversCollection = serversDB.get('servers');
Expand All @@ -38,7 +39,7 @@ const restore = function* restore() {
}
}
}
yield put(appStart({ root: ROOT_OUTSIDE }));
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
} else {
const serversDB = database.servers;
const serverCollections = serversDB.get('servers');
Expand All @@ -56,7 +57,7 @@ const restore = function* restore() {
yield put(appReady({}));
} catch (e) {
log(e);
yield put(appStart({ root: ROOT_OUTSIDE }));
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
}
};

Expand Down
15 changes: 8 additions & 7 deletions app/sagas/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import { Q } from '@nozbe/watermelondb';

import * as types from '../actions/actionsTypes';
import { ROOT_INSIDE, ROOT_LOADING, ROOT_OUTSIDE, ROOT_SET_USERNAME, appStart } from '../actions/app';
import { appStart } from '../actions/app';
import { selectServerRequest, serverFinishAdd } from '../actions/server';
import { loginFailure, loginSuccess, logout, setUser } from '../actions/login';
import { roomsRequest } from '../actions/rooms';
Expand All @@ -20,6 +20,7 @@ import { encryptionInit, encryptionStop } from '../actions/encryption';
import UserPreferences from '../lib/userPreferences';
import { inquiryRequest, inquiryReset } from '../ee/omnichannel/actions/inquiry';
import { isOmnichannelStatusAvailable } from '../ee/omnichannel/lib';
import { RootEnum } from '../definitions';

const getServer = state => state.server.server;
const loginWithPasswordCall = args => RocketChat.loginWithPassword(args);
Expand All @@ -38,7 +39,7 @@ const handleLoginRequest = function* handleLoginRequest({ credentials, logoutOnE
if (!result.username) {
yield put(serverFinishAdd());
yield put(setUser(result));
yield put(appStart({ root: ROOT_SET_USERNAME }));
yield put(appStart({ root: RootEnum.ROOT_SET_USERNAME }));
} else {
const server = yield select(getServer);
yield localAuthenticate(server);
Expand Down Expand Up @@ -167,7 +168,7 @@ const handleLoginSuccess = function* handleLoginSuccess({ user }) {
yield put(setUser(user));
EventEmitter.emit('connected');

yield put(appStart({ root: ROOT_INSIDE }));
yield put(appStart({ root: RootEnum.ROOT_INSIDE }));
const inviteLinkToken = yield select(state => state.inviteLinks.token);
if (inviteLinkToken) {
yield put(inviteLinksRequest(inviteLinkToken));
Expand All @@ -179,15 +180,15 @@ const handleLoginSuccess = function* handleLoginSuccess({ user }) {

const handleLogout = function* handleLogout({ forcedByServer }) {
yield put(encryptionStop());
yield put(appStart({ root: ROOT_LOADING, text: I18n.t('Logging_out') }));
yield put(appStart({ root: RootEnum.ROOT_LOADING, text: I18n.t('Logging_out') }));
const server = yield select(getServer);
if (server) {
try {
yield call(logoutCall, { server });

// if the user was logged out by the server
if (forcedByServer) {
yield put(appStart({ root: ROOT_OUTSIDE }));
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
showErrorAlert(I18n.t('Logged_out_by_server'), I18n.t('Oops'));
yield delay(300);
EventEmitter.emit('NewServer', { server });
Expand All @@ -209,10 +210,10 @@ const handleLogout = function* handleLogout({ forcedByServer }) {
}
}
// if there's no servers, go outside
yield put(appStart({ root: ROOT_OUTSIDE }));
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
}
} catch (e) {
yield put(appStart({ root: ROOT_OUTSIDE }));
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
log(e);
}
}
Expand Down
Loading