From 003f9fca68987dc3404ea1de14991530419bf6d5 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Tue, 21 Dec 2021 12:03:59 -0300 Subject: [PATCH] chore: type selectedUsers action and reducer and improvement in the code of other files --- .../{actionsTypes.js => actionsTypes.ts} | 4 +- app/actions/activeUsers.ts | 9 ++-- app/actions/selectedUsers.js | 28 ------------ app/actions/selectedUsers.ts | 43 +++++++++++++++++++ app/reducers/activeUsers.ts | 15 ++----- .../{selectedUsers.js => selectedUsers.ts} | 10 ++++- app/types/index.ts | 15 +++++++ app/types/redux/index.ts | 13 +++--- 8 files changed, 85 insertions(+), 52 deletions(-) rename app/actions/{actionsTypes.js => actionsTypes.ts} (96%) delete mode 100644 app/actions/selectedUsers.js create mode 100644 app/actions/selectedUsers.ts rename app/reducers/{selectedUsers.js => selectedUsers.ts} (67%) diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.ts similarity index 96% rename from app/actions/actionsTypes.js rename to app/actions/actionsTypes.ts index 852ce83ea..ad2d1718d 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.ts @@ -2,8 +2,8 @@ const REQUEST = 'REQUEST'; const SUCCESS = 'SUCCESS'; const FAILURE = 'FAILURE'; const defaultTypes = [REQUEST, SUCCESS, FAILURE]; -function createRequestTypes(base, types = defaultTypes) { - const res = {}; +function createRequestTypes(base = {}, types = defaultTypes): Record { + const res: Record = {}; types.forEach(type => (res[type] = `${base}_${type}`)); return res; } diff --git a/app/actions/activeUsers.ts b/app/actions/activeUsers.ts index 3e6271ee9..1ba58c798 100644 --- a/app/actions/activeUsers.ts +++ b/app/actions/activeUsers.ts @@ -1,14 +1,15 @@ import { Action } from 'redux'; -import { ActiveUsers } from '../reducers/activeUsers'; +import { IActiveUsers } from '../reducers/activeUsers'; import { SET_ACTIVE_USERS } from './actionsTypes'; export interface SetActiveUsers extends Action { - type: typeof SET_ACTIVE_USERS; - activeUsers: ActiveUsers; + activeUsers: IActiveUsers; } -export const setActiveUsers = (activeUsers: ActiveUsers): SetActiveUsers => ({ +export type IActionActiveUsers = SetActiveUsers; + +export const setActiveUsers = (activeUsers: IActiveUsers): SetActiveUsers => ({ type: SET_ACTIVE_USERS, activeUsers }); diff --git a/app/actions/selectedUsers.js b/app/actions/selectedUsers.js deleted file mode 100644 index 65fbb0015..000000000 --- a/app/actions/selectedUsers.js +++ /dev/null @@ -1,28 +0,0 @@ -import * as types from './actionsTypes'; - -export function addUser(user) { - return { - type: types.SELECTED_USERS.ADD_USER, - user - }; -} - -export function removeUser(user) { - return { - type: types.SELECTED_USERS.REMOVE_USER, - user - }; -} - -export function reset() { - return { - type: types.SELECTED_USERS.RESET - }; -} - -export function setLoading(loading) { - return { - type: types.SELECTED_USERS.SET_LOADING, - loading - }; -} diff --git a/app/actions/selectedUsers.ts b/app/actions/selectedUsers.ts new file mode 100644 index 000000000..844aa3473 --- /dev/null +++ b/app/actions/selectedUsers.ts @@ -0,0 +1,43 @@ +import { Action } from 'redux'; + +import { IUser } from '../types'; +import * as types from './actionsTypes'; + +type User = { + user: IUser; +}; + +type IAction = Action & User; + +interface SetLoading extends Action { + loading: boolean; +} + +export type IActionSelectedUsers = IAction & SetLoading; + +export function addUser(user: IUser): IAction { + return { + type: types.SELECTED_USERS.ADD_USER, + user + }; +} + +export function removeUser(user: IUser): IAction { + return { + type: types.DEEP_LINKING, + user + }; +} + +export function reset(): Action { + return { + type: types.SELECTED_USERS.RESET + }; +} + +export function setLoading(loading: boolean): SetLoading { + return { + type: types.SELECTED_USERS.SET_LOADING, + loading + }; +} diff --git a/app/reducers/activeUsers.ts b/app/reducers/activeUsers.ts index 306c24e7e..47d8cd2cd 100644 --- a/app/reducers/activeUsers.ts +++ b/app/reducers/activeUsers.ts @@ -1,20 +1,13 @@ -import { SetActiveUsers } from '../actions/activeUsers'; +import { ActiveUser, ApplicationActions } from '../types'; import { SET_ACTIVE_USERS } from '../actions/actionsTypes'; -type UserStatus = 'online' | 'offline'; - -interface ActiveUser { - readonly status: UserStatus; - readonly statusText?: string; -} - -export interface ActiveUsers { +export interface IActiveUsers { [key: string]: ActiveUser; } -const initialState: ActiveUsers = {}; +const initialState: IActiveUsers = {}; -export default function activeUsers(state = initialState, action: SetActiveUsers): ActiveUsers { +export default function activeUsers(state = initialState, action: ApplicationActions): IActiveUsers { switch (action.type) { case SET_ACTIVE_USERS: return { diff --git a/app/reducers/selectedUsers.js b/app/reducers/selectedUsers.ts similarity index 67% rename from app/reducers/selectedUsers.js rename to app/reducers/selectedUsers.ts index 42d7982c1..bbc7bc996 100644 --- a/app/reducers/selectedUsers.js +++ b/app/reducers/selectedUsers.ts @@ -1,11 +1,17 @@ +import { IUser, ApplicationActions } from '../types'; import { SELECTED_USERS } from '../actions/actionsTypes'; -const initialState = { +export interface ISelectedUsers { + users: IUser[]; + loading: boolean; +} + +const initialState: ISelectedUsers = { users: [], loading: false }; -export default function (state = initialState, action) { +export default function (state = initialState, action: ApplicationActions): ISelectedUsers { switch (action.type) { case SELECTED_USERS.ADD_USER: return { diff --git a/app/types/index.ts b/app/types/index.ts index ea8f9adb4..6bb354fdf 100644 --- a/app/types/index.ts +++ b/app/types/index.ts @@ -7,4 +7,19 @@ export interface BaseScreen { theme: string; } +export interface IUser { + _id: string; + name: string; + fname: string; + search?: boolean; + // username is used when is from searching + username?: string; +} + +type UserStatus = 'online' | 'offline'; +export interface ActiveUser { + status: UserStatus; + statusText?: string; +} + export * from './redux'; diff --git a/app/types/redux/index.ts b/app/types/redux/index.ts index a52e979f3..fe61c50a5 100644 --- a/app/types/redux/index.ts +++ b/app/types/redux/index.ts @@ -1,12 +1,15 @@ -import { SetActiveUsers } from '../../actions/activeUsers'; -import { ActiveUsers } from '../../reducers/activeUsers'; +import { IActionSelectedUsers } from '../../actions/selectedUsers'; +import { IActionActiveUsers } from '../../actions/activeUsers'; +// REDUCERS +import { IActiveUsers } from '../../reducers/activeUsers'; +import { ISelectedUsers } from '../../reducers/selectedUsers'; export interface ApplicationState { settings: any; login: any; meteor: any; server: any; - selectedUsers: any; + selectedUsers: ISelectedUsers; createChannel: any; app: any; room: any; @@ -14,7 +17,7 @@ export interface ApplicationState { sortPreferences: any; share: any; customEmojis: any; - activeUsers: ActiveUsers; + activeUsers: IActiveUsers; usersTyping: any; inviteLinks: any; createDiscussion: any; @@ -25,4 +28,4 @@ export interface ApplicationState { roles: any; } -export type ApplicationActions = SetActiveUsers; +export type ApplicationActions = IActionActiveUsers & IActionSelectedUsers;