Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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 { IUserLogin } from '../reducers/login';
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<IUserLogin>;
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
}

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

interface ILogout extends Action {
forcedByServer: boolean;
}

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

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<IUserLogin>): 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<IUserLogin>): 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
};
}
3 changes: 2 additions & 1 deletion app/definitions/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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 { ISelectedUsers } from '../../reducers/selectedUsers';
import { IServer } from '../../reducers/server';
Expand All @@ -29,8 +30,8 @@ import { IShare } from '../../reducers/share';

export interface IApplicationState {
settings: ISettings;
login: any;
meteor: IConnect;
login: ILogin;
server: IServer;
selectedUsers: ISelectedUsers;
app: IApp;
Expand Down
2 changes: 1 addition & 1 deletion app/reducers/activeUsers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TApplicationActions } from '../definitions';
import { SET_ACTIVE_USERS } from '../actions/actionsTypes';

type TUserStatus = 'online' | 'offline';
export type TUserStatus = 'online' | 'offline';
export interface IActiveUser {
status: TUserStatus;
statusText: string;
Expand Down
106 changes: 106 additions & 0 deletions app/reducers/login.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {
loginFailure,
loginRequest,
loginSuccess,
logout,
setLocalAuthenticated,
setLoginServices,
setUser
} from '../actions/login';
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
};
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);
});
});
37 changes: 34 additions & 3 deletions app/reducers/login.js → app/reducers/login.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import * as types from '../actions/actionsTypes';
import { TActionsLogin } from '../actions/login';
import { TUserStatus } from './activeUsers';

const initialState = {
export interface IUserLogin {
id: string;
token: string;
username: string;
name: string;
language: string;
status: TUserStatus;
statusText: string;
roles: string[];
avatarETag: string;
isFromWebView: boolean;
showMessageInMainThread: boolean;
enableMessageParserEarlyAdoption: boolean;
emails: Record<string, any>[];
customFields: Record<string, string>;
settings: Record<string, string>;
}

export interface ILogin {
user: Partial<IUserLogin> | Record<string, any>;
isLocalAuthenticated: boolean;
isAuthenticated: boolean;
isFetching: boolean;
error: Record<string, any>;
services: Record<string, any>;
failure: boolean;
}

export const initialState: ILogin = {
isLocalAuthenticated: true,
isAuthenticated: false,
isFetching: false,
user: {},
error: {},
services: {}
services: {},
failure: false
};

export default function login(state = initialState, action) {
export default function login(state = initialState, action: TActionsLogin): ILogin {
switch (action.type) {
case types.APP.INIT:
return initialState;
Expand Down
Loading