Skip to content
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
59 changes: 0 additions & 59 deletions app/actions/login.js

This file was deleted.

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

import { IUser } from '../definitions';
import * as types from './actionsTypes';

interface ICredentials {
resume: string;
user: string;
password: string;
}

interface ILoginRequest extends Action {
credentials: any;
logoutOnError?: boolean;
isFromWebView?: boolean;
}

interface ILoginSuccess extends Action {
user: Partial<IUser>;
}

interface ILoginFailure extends Action {
err: Partial<IUser>;
}

interface ILogout extends Action {
forcedByServer: boolean;
}

interface ISetUser extends Action {
user: Partial<IUser>;
}

interface ISetServices extends Action {
data: Record<string, string>;
}

interface ISetPreference extends Action {
preference: Record<string, any>;
}

interface ISetLocalAuthenticated extends Action {
isLocalAuthenticated: boolean;
}

export type TActionsLogin = ILoginRequest &
ILoginSuccess &
ILoginFailure &
ILogout &
ISetUser &
ISetServices &
ISetPreference &
ISetLocalAuthenticated;

export function loginRequest(
credentials: Partial<ICredentials>,
logoutOnError?: boolean,
isFromWebView?: boolean
): ILoginRequest {
return {
type: types.LOGIN.REQUEST,
credentials,
logoutOnError,
isFromWebView
};
}

export function loginSuccess(user: Partial<IUser>): ILoginSuccess {
return {
type: types.LOGIN.SUCCESS,
user
};
}

export function loginFailure(err: Record<string, any>): ILoginFailure {
return {
type: types.LOGIN.FAILURE,
err
};
}

export function logout(forcedByServer = false): ILogout {
return {
type: types.LOGOUT,
forcedByServer
};
}

export function setUser(user: Partial<IUser>): ISetUser {
return {
type: types.USER.SET,
user
};
}

export function setLoginServices(data: Record<string, any>): ISetServices {
return {
type: types.LOGIN.SET_SERVICES,
data
};
}

export function setPreference(preference: Record<string, any>): ISetPreference {
return {
type: types.LOGIN.SET_PREFERENCE,
preference
};
}

export function setLocalAuthenticated(isLocalAuthenticated: boolean): ISetLocalAuthenticated {
return {
type: types.LOGIN.SET_LOCAL_AUTHENTICATED,
isLocalAuthenticated
};
}
5 changes: 4 additions & 1 deletion app/definitions/IUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Model from '@nozbe/watermelondb/Model';

import { UserStatus } from './UserStatus';
import { IRocketChatRecord } from './IRocketChatRecord';
import { ILoggedUser } from './ILoggedUser';

export interface ILoginToken {
hashedToken: string;
Expand Down Expand Up @@ -93,8 +94,10 @@ export interface IUserSettings {
};
}

export interface IUser extends IRocketChatRecord {
export interface IUser extends IRocketChatRecord, Omit<ILoggedUser, 'username' | 'name' | 'status'> {
_id: string;
id: string;
token: string;
createdAt: Date;
roles: string[];
type: string;
Expand Down
3 changes: 2 additions & 1 deletion app/definitions/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ICreateChannel } from '../../reducers/createChannel';
import { ICreateDiscussion } from '../../reducers/createDiscussion';
import { IEncryption } from '../../reducers/encryption';
import { IInviteLinks } from '../../reducers/inviteLinks';
import { ILogin } from '../../reducers/login';
import { IRoles } from '../../reducers/roles';
import { IRoom } from '../../reducers/room';
import { ISelectedUsers } from '../../reducers/selectedUsers';
Expand All @@ -34,8 +35,8 @@ import { IEnterpriseModules } from '../../reducers/enterpriseModules';

export interface IApplicationState {
settings: ISettings;
login: any;
meteor: IConnect;
login: ILogin;
server: IServer;
selectedUsers: ISelectedUsers;
app: IApp;
Expand Down
3 changes: 2 additions & 1 deletion app/reducers/activeUsers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { clearActiveUsers, setActiveUsers } from '../actions/activeUsers';
import { UserStatus } from '../definitions/UserStatus';
import { IActiveUsers, initialState } from './activeUsers';
import { mockedStore } from './mockedStore';

Expand All @@ -8,7 +9,7 @@ describe('test reducer', () => {
expect(state).toEqual(initialState);
});
it('should return modified store after action', () => {
const activeUsers: IActiveUsers = { any: { status: 'online', statusText: 'any' } };
const activeUsers: IActiveUsers = { any: { status: UserStatus.ONLINE, statusText: 'any' } };
mockedStore.dispatch(setActiveUsers(activeUsers));
const state = mockedStore.getState().activeUsers;
expect(state).toEqual({ ...activeUsers });
Expand Down
6 changes: 3 additions & 3 deletions app/reducers/activeUsers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TApplicationActions } from '../definitions';
import { ACTIVE_USERS } from '../actions/actionsTypes';
import { TApplicationActions } from '../definitions';
import { UserStatus } from '../definitions/UserStatus';

type TUserStatus = 'online' | 'offline' | 'away' | 'busy';
export interface IActiveUser {
status: TUserStatus;
status: UserStatus;
statusText: string;
}

Expand Down
109 changes: 109 additions & 0 deletions app/reducers/login.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {
loginFailure,
loginRequest,
loginSuccess,
logout,
setLocalAuthenticated,
setLoginServices,
setUser
} from '../actions/login';
import { UserStatus } from '../definitions/UserStatus';
import { initialState } from './login';
import { mockedStore } from './mockedStore';

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

it('should return modified store after loginRequest', () => {
mockedStore.dispatch(loginRequest({ user: 'carlitos@email.com', password: '123456' }));
const state = mockedStore.getState().login;
expect(state).toEqual({ ...initialState, isFetching: true, isAuthenticated: false, failure: false, error: {} });
});

it('should return modified store after loginFailure', () => {
mockedStore.dispatch(loginFailure({ error: 'error' }));
const state = mockedStore.getState().login.error.error;
expect(state).toEqual('error');
});

it('should return modified store after loginSuccess', () => {
const user = {
id: 'ajhsiahsa',
token: 'asdasdasdas',
username: 'carlitos',
name: 'Carlitos',
customFields: {
phonenumber: ''
},
emails: [
{
address: 'carlitos@email.com',
verified: true
}
],
roles: ['user'],
isFromWebView: false,
showMessageInMainThread: false,
enableMessageParserEarlyAdoption: false,
status: UserStatus.ONLINE,
statusText: 'online'
};
mockedStore.dispatch(loginSuccess(user));
const state = mockedStore.getState().login.user;
expect(state).toEqual(user);
});

it('should return modified store after setUser', () => {
const user = {
id: 'ajhsiahsa',
token: 'asdasdasdas',
username: 'carlito',
name: 'Carlitos',
customFields: {
phonenumber: ''
},
emails: [
{
address: 'carlitos@email.com',
verified: true
}
],
roles: ['user'],
isFromWebView: false,
showMessageInMainThread: false,
enableMessageParserEarlyAdoption: false
};
mockedStore.dispatch(setUser(user));
const state = mockedStore.getState().login.user.username;
expect(state).toEqual(user.username);
});

// TODO PREFERENCE REDUCER WITH EMPTY PREFERENCE - NON USED?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats wrong here? Is missing something or we can remove this block of code?
If I can help with anything, just let me know

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This action doesn't work, but I'm not sure if it's because at login this object is set or if it doesn't work at all.

// it('should return modified store after setPreference', () => {
// mockedStore.dispatch(setPreference({ showAvatar: true }));
// const state = mockedStore.getState().login;
// console.log(state);
// expect(state).toEqual('error');
// });

it('should return modified store after setLocalAuthenticated', () => {
mockedStore.dispatch(setLocalAuthenticated(true));
const state = mockedStore.getState().login.isLocalAuthenticated;
expect(state).toEqual(true);
});

it('should return modified store after setLoginServices', () => {
mockedStore.dispatch(setLoginServices({ facebook: { clientId: 'xxx' } }));
const state = mockedStore.getState().login.services.facebook.clientId;
expect(state).toEqual('xxx');
});

it('should return modified store after logout', () => {
mockedStore.dispatch(logout());
const state = mockedStore.getState().login;
expect(state).toEqual(initialState);
});
});
Loading