Browse Source

CHANGE | All users list instead executors list on select chat members screen

merge-requests/370/head
Oksana Stepanenko 2 years ago
parent
commit
80ed429fe5
  1. 16
      src/api/chats/responses.interfaces.ts
  2. 24
      src/managers/chat.manager.ts
  3. 11
      src/modules/chats/hooks/use-edit-group-chat.hook.ts
  4. 2
      src/modules/chats/screens/chats.screen.tsx
  5. 19
      src/modules/users/components/users-select-list-with-search.component.tsx
  6. 59
      src/modules/users/hooks/use-fetch-users.hook.ts
  7. 17
      src/modules/users/screens/select-user-list.screen.tsx
  8. 11
      src/repositories/api/chat.repository.ts
  9. 32
      src/services/domain/chats.service.ts
  10. 3
      src/services/domain/index.ts

16
src/api/chats/responses.interfaces.ts

@ -1,7 +1,17 @@ @@ -1,7 +1,17 @@
import { IChat, IChatDetails, IPaginationResult } from '@/shared';
import { IChat, IChatDetails, IPaginationResult } from '@/shared'
export type NewChatGroupId = number
export interface IFetchChatList extends IPaginationResult<IChat> { }
export interface IFetchChatList extends IPaginationResult<IChat> {}
export interface IFetchChatDetail extends IChatDetails {}
export interface IFetchChatDetail extends IChatDetails {}
export interface IPotentialChatMember {
userId: number
firstName: string
middleName: string
lastName: string
avatarUrl: string
}
export interface IFetchPotentialChatMembersResponse
extends IPaginationResult<IPotentialChatMember> {}

24
src/managers/chat.manager.ts

@ -6,6 +6,7 @@ import { @@ -6,6 +6,7 @@ import {
IFixChatPayload,
ISetChatMuted,
} from '@/api/chats/requests.interfaces'
import { IRequestConfig } from '@/api/http.service'
import {
ApiChatRepository,
dbActionsQueueRepository,
@ -491,6 +492,29 @@ export class ChatManager extends Manager { @@ -491,6 +492,29 @@ export class ChatManager extends Manager {
return members
}
public async getUsersForChat(
params: IRequestConfig = {
params: { sortField: 'id', sort: 'DESC', limit: 20 },
},
) {
if (this.isConnected === 'online') {
const resp = await this.chatRepository.getUsersForChatReq(
params.params,
)
return resp
} else {
const _params = { ...params.params }
const dbUsers = await this.dbUserRepository.fetchContacts({
..._params,
})
return {
data: dbUsers,
}
}
}
public getIsConnected() {
return this.isConnected
}

11
src/modules/chats/hooks/use-edit-group-chat.hook.ts

@ -32,6 +32,8 @@ type TNav = StackNavigationProp<{ @@ -32,6 +32,8 @@ type TNav = StackNavigationProp<{
useChatBtnColors?: boolean
disableIds?: number[]
excludeIds?: number[]
resetOnGoBack?: boolean
type?: 'executors' | 'all'
}
}>
@ -41,7 +43,7 @@ export const useEditGroupChat = (id?: number) => { @@ -41,7 +43,7 @@ export const useEditGroupChat = (id?: number) => {
const [previewImg, setPreviewImg] = useState<unknown>()
const [isChatMuted, setMuted] = useState<boolean>(null)
const [chatMembers, setMembers] = useState<IChatMember[]>([])
const [membersIdsToAdd, setMembersIdsToAdd] = useState<(number)[]>([])
const [membersIdsToAdd, setMembersIdsToAdd] = useState<number[]>([])
const [role, setRole] = useState<ChatMemberRole>(ChatMemberRole.Admin)
const selectedChatId = useSelector(selectSelectedChatId)
@ -104,10 +106,7 @@ export const useEditGroupChat = (id?: number) => { @@ -104,10 +106,7 @@ export const useEditGroupChat = (id?: number) => {
{
name: 'Видалити у всіх',
onPress: () => {
setTimeout(
() => handleDeleteForAll(chatId),
300,
)
setTimeout(() => handleDeleteForAll(chatId), 300)
},
},
],
@ -131,6 +130,8 @@ export const useEditGroupChat = (id?: number) => { @@ -131,6 +130,8 @@ export const useEditGroupChat = (id?: number) => {
useChatBtnColors: true,
disableIds: activeMembers.map(it => it.userId),
excludeIds: [accountId],
resetOnGoBack: true,
type: 'all',
})
}

2
src/modules/chats/screens/chats.screen.tsx

@ -27,6 +27,8 @@ export const ChatsScreen: FC<IProps> = ({ navigation }) => { @@ -27,6 +27,8 @@ export const ChatsScreen: FC<IProps> = ({ navigation }) => {
onSubmit: () => navigation.navigate(RouteKey.CreateGroup),
useChatBtnColors: true,
excludeIds: [accountId],
resetOnGoBack: true,
type: 'all',
})
const props = {

19
src/modules/users/components/users-select-list-with-search.component.tsx

@ -1,19 +1,12 @@ @@ -1,19 +1,12 @@
import React, { FC, useEffect, useMemo, useRef, useState } from 'react'
import {
StyleProp,
StyleSheet,
TextStyle,
TouchableOpacity,
View,
ViewStyle,
} from 'react-native'
import React, { FC, useEffect, useMemo, useState } from 'react'
import { StyleProp, StyleSheet, TextStyle, View, ViewStyle } from 'react-native'
import * as _ from 'lodash'
import { $size, IShortUser, Txt } from '@/shared'
import { $size, IShortUser } from '@/shared'
import { SearchWithBtn } from '@/shared/components/forms'
import { useTheme } from '@/shared/hooks/use-theme.hook'
import { UsersSelectList } from '../components'
import { useFetchTaskExecutors } from '@/modules/tasks/hooks'
import { transformUserToSelectList } from '../transforms'
import { useFetchUsersList } from '../hooks'
interface IProps {
selectedUsers: IShortUser[]
@ -24,6 +17,7 @@ interface IProps { @@ -24,6 +17,7 @@ interface IProps {
selectAllBtnStyle?: ViewStyle
disableIds?: number[]
excludeIds?: number[]
type?: 'executors' | 'all'
}
export const UsersSelectListWithSearch: FC<IProps> = ({
@ -35,6 +29,7 @@ export const UsersSelectListWithSearch: FC<IProps> = ({ @@ -35,6 +29,7 @@ export const UsersSelectListWithSearch: FC<IProps> = ({
selectAllBtnStyle,
disableIds,
excludeIds,
type,
}) => {
const { styles } = useTheme(createStyles)
@ -47,7 +42,7 @@ export const UsersSelectListWithSearch: FC<IProps> = ({ @@ -47,7 +42,7 @@ export const UsersSelectListWithSearch: FC<IProps> = ({
setSearchVal,
loadAll,
isLoadedAll,
} = useFetchTaskExecutors()
} = useFetchUsersList({ type })
const transformedUsers = useMemo(() => {
const transformedToSelectListUsers = items?.map(it =>

59
src/modules/users/hooks/use-fetch-users.hook.ts

@ -1,31 +1,56 @@ @@ -1,31 +1,56 @@
import { usersService } from '@/services/domain/users.service'
import { useEffect, useState } from 'react'
import { IUser, useFlatList } from '@/shared'
import { useEffect } from 'react'
import { useState } from 'react'
import { IShortUser, useFlatList } from '@/shared'
import { chatsService, tasksService } from '@/services/domain'
import { ITaskExecutorResponse } from '@/api/tasks/responses.interface'
import { transformExecutorsToShortUsers } from '@/api/tasks/transform'
import _ from 'lodash'
export const useFetchUsers = () => {
const [searchString, setSearchVal] = useState<string>()
interface IProps {
type?: 'executors' | 'all'
}
const api = {
executors: tasksService.getTaskExecutors,
all: params => chatsService.getUsersForChat.bind(chatsService)(params),
}
export const useFetchUsersList = ({ type }: IProps) => {
const [searchString, setSearchVal] = useState<string>(null)
const { items, isLoading, isLoadingNext, loadMore, resetFlatList } =
useFlatList({
fetchItems: params => usersService.fetch({ ...params }),
needInit: false,
loadParams: {
sort: 'DESC',
sortField: 'id',
},
limit: 20,
})
const {
items,
isLoading,
isLoadingNext,
loadMore,
setLoadParams,
loadAll,
isLoadedAll,
} = useFlatList<IShortUser>({
fetchItems: api[_.defaultTo(type, 'executors')],
needInit: true,
serrializatorItems: (_items: ITaskExecutorResponse[]) =>
transformExecutorsToShortUsers(_items),
loadParams: {
sort: 'DESC',
sortField: 'id',
},
limit: 10,
clearWhenReload: false,
})
useEffect(() => {
resetFlatList()
if (searchString !== null) setLoadParams({ searchString })
}, [searchString])
return {
items: items as IUser[],
items: items as IShortUser[],
isLoading,
isLoadingNext,
searchString,
loadMore,
setSearchVal,
loadAll,
isLoadedAll,
}
}

17
src/modules/users/screens/select-user-list.screen.tsx

@ -7,7 +7,12 @@ import { @@ -7,7 +7,12 @@ import {
useTheme,
} from '@/shared'
import { simpleDispatch } from '@/store/store-helpers'
import { SelectAll, SelectUser, UnselectUser } from '@/store/users'
import {
SelectAll,
SelectUser,
UnselectAllUsers,
UnselectUser,
} from '@/store/users'
import { useSelector } from 'react-redux'
import { selectSelectedUsers } from '@/store/users'
import {
@ -26,6 +31,8 @@ interface IProps extends IRouteParams { @@ -26,6 +31,8 @@ interface IProps extends IRouteParams {
useChatBtnColors?: boolean
disableIds?: number[]
excludeIds?: number[]
resetOnGoBack?: boolean
type?: 'executors' | 'all'
}
}
}
@ -41,6 +48,8 @@ export const SelectUserList: FC<IProps> = ({ @@ -41,6 +48,8 @@ export const SelectUserList: FC<IProps> = ({
useChatBtnColors,
disableIds,
excludeIds,
resetOnGoBack,
type,
},
},
}) => {
@ -68,7 +77,10 @@ export const SelectUserList: FC<IProps> = ({ @@ -68,7 +77,10 @@ export const SelectUserList: FC<IProps> = ({
<ScreenLayout
header={{
title,
goBack: () => navigation.goBack(),
goBack: () => {
navigation.goBack()
if (resetOnGoBack) simpleDispatch(new UnselectAllUsers())
},
style: {
marginBottom: $size(20, 18),
paddingTop: $size(10, 10),
@ -98,6 +110,7 @@ export const SelectUserList: FC<IProps> = ({ @@ -98,6 +110,7 @@ export const SelectUserList: FC<IProps> = ({
color: theme?.chats?.selectItems?.$addAllBtnTxt,
}
}
type={type}
/>
</ScreenLayout>
)

11
src/repositories/api/chat.repository.ts

@ -10,9 +10,11 @@ import { @@ -10,9 +10,11 @@ import {
import {
IFetchChatDetail,
IFetchChatList,
IFetchPotentialChatMembersResponse,
NewChatGroupId,
} from '@/api/chats/responses.interfaces'
import { ApiResponse } from '@/api/http.types'
import { IFetchUsersParams } from '@/api/users/requests.interfaces'
import { ApiRepository } from '@/shared/abstract'
import { convertToFormData } from '@/shared/helpers'
@ -77,6 +79,15 @@ export class ApiChatRepository extends ApiRepository { @@ -77,6 +79,15 @@ export class ApiChatRepository extends ApiRepository {
return this.http.post(`chats/${chatId}/read`, {}, {}, 'common/')
}
public async getUsersForChatReq(
params: IFetchUsersParams,
): ApiResponse<IFetchPotentialChatMembersResponse> {
return this.http.get<IFetchPotentialChatMembersResponse>(
'chats/users',
{ params },
)
}
static instance: ApiChatRepository
static getInstance() {

32
src/services/domain/chats.service.ts

@ -14,9 +14,7 @@ import { @@ -14,9 +14,7 @@ import {
IFetchChatDetail,
ISetChatMuted,
} from '@/api/chats/requests.interfaces'
import {
NewChatGroupId,
} from '@/api/chats/responses.interfaces'
import { NewChatGroupId } from '@/api/chats/responses.interfaces'
import { SetUnreadMessagesCount } from '@/store/chats'
import { IChatDetails } from '@/shared/interfaces'
import { Platform } from 'react-native'
@ -25,6 +23,8 @@ import { fsService } from '../system' @@ -25,6 +23,8 @@ import { fsService } from '../system'
import { showUknowError } from '@/shared/helpers'
import { Buffer } from 'buffer'
import { chatMessageManager } from '@/managers/chat-message.manager'
import { IRequestConfig } from '@/api/http.service'
import { chatManager } from '@/managers'
const createGroupChat = async (
params: ICreateGroupChat,
@ -82,7 +82,9 @@ const deleteById = async (chatId: number): Promise<void> => { @@ -82,7 +82,9 @@ const deleteById = async (chatId: number): Promise<void> => {
const getUnreadMessagesCount = async () => {
try {
const { data } = await chatMessageManager.getUnreadMessagesCount.bind(chatMessageManager)()
const { data } = await chatMessageManager.getUnreadMessagesCount.bind(
chatMessageManager,
)()
simpleDispatch(new SetUnreadMessagesCount(data?.count))
} catch (error) {
@ -92,9 +94,12 @@ const getUnreadMessagesCount = async () => { @@ -92,9 +94,12 @@ const getUnreadMessagesCount = async () => {
const shareChat = async (chatId: number | string) => {
try {
const { data }: Record<string, any> = await chatMessageManager.exportMessagesToFile.bind(chatMessageManager)({
chatId: Number(chatId),
})
const { data }: Record<string, any> =
await chatMessageManager.exportMessagesToFile.bind(
chatMessageManager,
)({
chatId: Number(chatId),
})
if (!data) return
@ -121,6 +126,18 @@ const shareChat = async (chatId: number | string) => { @@ -121,6 +126,18 @@ const shareChat = async (chatId: number | string) => {
}
}
export const getUsersForChat = async (
params: IRequestConfig = {
params: { sortField: 'id', sort: 'DESC', limit: 20 },
},
) => {
try {
return await chatManager.getUsersForChat.bind(chatManager)(params)
} catch (err) {
console.log('FETCH POTENTIAL CHAT MEMBERS ERROR: ', err)
}
}
export const chatsService = {
fetch,
fetchDetails,
@ -131,4 +148,5 @@ export const chatsService = { @@ -131,4 +148,5 @@ export const chatsService = {
getUnreadMessagesCount,
createPersonalChat,
shareChat,
getUsersForChat,
}

3
src/services/domain/index.ts

@ -12,4 +12,5 @@ export * from './chat-members.service' @@ -12,4 +12,5 @@ export * from './chat-members.service'
export * from './chat-messages.service'
export * from './actions-queue.service'
export * from './remove.service'
export * from './configs.service'
export * from './configs.service'
export * from './users.service'

Loading…
Cancel
Save