Chore: Migrate redux module share to typescript (#3612)

* chore: migrate redux module share to typescript

* chore: fix types

* chore: update types

* chore: migrate redux module share to typescript

* remove double import

* chore: fix import
This commit is contained in:
Gleidson Daniel Silva 2022-02-02 14:46:05 -03:00 committed by GitHub
parent 03459147ed
commit c442a8473d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 136 additions and 54 deletions

View File

@ -1,22 +0,0 @@
import { SHARE } from './actionsTypes';
export function shareSelectServer(server) {
return {
type: SHARE.SELECT_SERVER,
server
};
}
export function shareSetSettings(settings) {
return {
type: SHARE.SET_SETTINGS,
settings
};
}
export function shareSetUser(user) {
return {
type: SHARE.SET_USER,
user
};
}

39
app/actions/share.ts Normal file
View File

@ -0,0 +1,39 @@
import { Action } from 'redux';
import { IShareServer, IShareUser, TShareSettings } from '../reducers/share';
import { SHARE } from './actionsTypes';
interface IShareSelectServer extends Action {
server: IShareServer;
}
interface IShareSetSettings extends Action {
settings: TShareSettings;
}
interface IShareSetUser extends Action {
user: IShareUser;
}
export type TActionsShare = IShareSelectServer & IShareSetSettings & IShareSetUser;
export function shareSelectServer(server: IShareServer): IShareSelectServer {
return {
type: SHARE.SELECT_SERVER,
server
};
}
export function shareSetSettings(settings: TShareSettings): IShareSetSettings {
return {
type: SHARE.SET_SETTINGS,
settings
};
}
export function shareSetUser(user: IShareUser): IShareSetUser {
return {
type: SHARE.SET_USER,
user
};
}

View File

@ -1,23 +1,25 @@
// ACTIONS // ACTIONS
import { TActionServer } from '../../actions/server';
import { TActionActiveUsers } from '../../actions/activeUsers'; import { TActionActiveUsers } from '../../actions/activeUsers';
import { TActionCustomEmojis } from '../../actions/customEmojis'; import { TActionCustomEmojis } from '../../actions/customEmojis';
import { TActionEncryption } from '../../actions/encryption'; import { TActionEncryption } from '../../actions/encryption';
import { TActionInviteLinks } from '../../actions/inviteLinks'; import { TActionInviteLinks } from '../../actions/inviteLinks';
import { IActionRoles } from '../../actions/roles'; import { IActionRoles } from '../../actions/roles';
import { TActionSelectedUsers } from '../../actions/selectedUsers'; import { TActionSelectedUsers } from '../../actions/selectedUsers';
import { TActionServer } from '../../actions/server';
import { IActionSettings } from '../../actions/settings'; import { IActionSettings } from '../../actions/settings';
import { TActionsShare } from '../../actions/share';
import { TActionSortPreferences } from '../../actions/sortPreferences'; import { TActionSortPreferences } from '../../actions/sortPreferences';
import { TActionUserTyping } from '../../actions/usersTyping'; import { TActionUserTyping } from '../../actions/usersTyping';
// REDUCERS // REDUCERS
import { IActiveUsers } from '../../reducers/activeUsers'; import { IActiveUsers } from '../../reducers/activeUsers';
import { IConnect } from '../../reducers/connect';
import { IEncryption } from '../../reducers/encryption'; import { IEncryption } from '../../reducers/encryption';
import { IInviteLinks } from '../../reducers/inviteLinks'; import { IInviteLinks } from '../../reducers/inviteLinks';
import { IRoles } from '../../reducers/roles'; import { IRoles } from '../../reducers/roles';
import { ISelectedUsers } from '../../reducers/selectedUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers';
import { IServer } from '../../reducers/server'; import { IServer } from '../../reducers/server';
import { IConnect } from '../../reducers/connect';
import { ISettings } from '../../reducers/settings'; import { ISettings } from '../../reducers/settings';
import { IShare } from '../../reducers/share';
export interface IApplicationState { export interface IApplicationState {
settings: ISettings; settings: ISettings;
@ -30,7 +32,7 @@ export interface IApplicationState {
room: any; room: any;
rooms: any; rooms: any;
sortPreferences: any; sortPreferences: any;
share: any; share: IShare;
customEmojis: any; customEmojis: any;
activeUsers: IActiveUsers; activeUsers: IActiveUsers;
usersTyping: any; usersTyping: any;
@ -52,4 +54,5 @@ export type TApplicationActions = TActionActiveUsers &
TActionEncryption & TActionEncryption &
TActionSortPreferences & TActionSortPreferences &
TActionUserTyping & TActionUserTyping &
TActionsShare &
TActionServer; TActionServer;

View File

@ -1,29 +0,0 @@
import { SHARE } from '../actions/actionsTypes';
const initialState = {
user: {},
server: {},
settings: {}
};
export default function share(state = initialState, action) {
switch (action.type) {
case SHARE.SELECT_SERVER:
return {
...state,
server: action.server
};
case SHARE.SET_USER:
return {
...state,
user: action.user
};
case SHARE.SET_SETTINGS:
return {
...state,
settings: action.settings
};
default:
return state;
}
}

View File

@ -0,0 +1,41 @@
import { shareSelectServer, shareSetSettings, shareSetUser } from '../actions/share';
import { mockedStore } from './mockedStore';
import { initialState } from './share';
describe('test share reducer', () => {
it('should return initial state', () => {
const state = mockedStore.getState().share;
expect(state).toEqual(initialState);
});
it('should return modified store after shareSelectServer', () => {
const server = {
server: 'https://open.rocket.chat',
version: '4.4.0'
};
mockedStore.dispatch(shareSelectServer(server));
const state = mockedStore.getState().share.server;
expect(state).toEqual(server);
});
it('should return modified store after shareSetSettings', () => {
const settings = {
Admin: false
};
mockedStore.dispatch(shareSetSettings(settings));
const state = mockedStore.getState().share.settings;
expect(state).toEqual(settings);
});
it('should return modified store after shareSetUser', () => {
const user = {
id: 'dig-joy',
token: 'token',
username: 'rocket.chat',
roles: ['admin']
};
mockedStore.dispatch(shareSetUser(user));
const state = mockedStore.getState().share.user;
expect(state).toEqual(user);
});
});

50
app/reducers/share.ts Normal file
View File

@ -0,0 +1,50 @@
import { TActionsShare } from '../actions/share';
import { SHARE } from '../actions/actionsTypes';
export interface IShareServer {
server: string;
version: string;
}
export type TShareSettings = Record<string, string | number | boolean>;
export interface IShareUser {
id: string;
token: string;
username: string;
roles: string[];
}
export interface IShare {
user: IShareUser | {};
server: IShareServer | {};
settings: TShareSettings;
}
export const initialState: IShare = {
user: {},
server: {},
settings: {}
};
export default function share(state = initialState, action: TActionsShare): IShare {
switch (action.type) {
case SHARE.SELECT_SERVER:
return {
...state,
server: action.server
};
case SHARE.SET_USER:
return {
...state,
user: action.user
};
case SHARE.SET_SETTINGS:
return {
...state,
settings: action.settings
};
default:
return state;
}
}