|
@@ -1,21 +1,12 @@
|
|
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
|
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
|
-import {
|
|
|
|
- View,
|
|
|
|
- Text,
|
|
|
|
- TouchableOpacity,
|
|
|
|
- StyleSheet,
|
|
|
|
- Image,
|
|
|
|
- Platform,
|
|
|
|
- TouchableHighlight
|
|
|
|
-} from 'react-native';
|
|
|
|
-import { HorizontalTabView, Input, PageWrapper } from 'src/components';
|
|
|
|
|
|
+import { View, Text, TouchableOpacity, Image, Platform, TouchableHighlight } from 'react-native';
|
|
|
|
+import { HorizontalTabView, Input, PageWrapper, WarningModal } from 'src/components';
|
|
import { NAVIGATION_PAGES } from 'src/types';
|
|
import { NAVIGATION_PAGES } from 'src/types';
|
|
import { useFocusEffect, useNavigation } from '@react-navigation/native';
|
|
import { useFocusEffect, useNavigation } from '@react-navigation/native';
|
|
|
|
|
|
import AddChatIcon from 'assets/icons/messages/chat-plus.svg';
|
|
import AddChatIcon from 'assets/icons/messages/chat-plus.svg';
|
|
import { API_HOST } from 'src/constants';
|
|
import { API_HOST } from 'src/constants';
|
|
import { Colors } from 'src/theme';
|
|
import { Colors } from 'src/theme';
|
|
-import { getFontSize } from 'src/utils';
|
|
|
|
import SwipeableRow from './Components/SwipeableRow';
|
|
import SwipeableRow from './Components/SwipeableRow';
|
|
import { FlashList } from '@shopify/flash-list';
|
|
import { FlashList } from '@shopify/flash-list';
|
|
|
|
|
|
@@ -26,16 +17,14 @@ import { SheetManager } from 'react-native-actions-sheet';
|
|
import MoreModal from './Components/MoreModal';
|
|
import MoreModal from './Components/MoreModal';
|
|
import SearchIcon from 'assets/icons/search.svg';
|
|
import SearchIcon from 'assets/icons/search.svg';
|
|
import { storage, StoreType } from 'src/storage';
|
|
import { storage, StoreType } from 'src/storage';
|
|
-import { usePostGetChatsListQuery } from '@api/chat';
|
|
|
|
-import moment from 'moment';
|
|
|
|
|
|
+import { usePostDeleteChatMutation, usePostGetChatsListQuery } from '@api/chat';
|
|
import { Chat } from './types';
|
|
import { Chat } from './types';
|
|
|
|
|
|
import PinIcon from 'assets/icons/messages/pin.svg';
|
|
import PinIcon from 'assets/icons/messages/pin.svg';
|
|
-
|
|
|
|
-type Routes = {
|
|
|
|
- key: 'all' | 'unread' | 'archived';
|
|
|
|
- title: string;
|
|
|
|
-};
|
|
|
|
|
|
+import { formatDate } from './utils';
|
|
|
|
+import { routes } from './constants';
|
|
|
|
+import { styles } from './styles';
|
|
|
|
+import { useChatStore } from 'src/stores/chatStore';
|
|
|
|
|
|
const MessagesScreen = () => {
|
|
const MessagesScreen = () => {
|
|
const navigation = useNavigation();
|
|
const navigation = useNavigation();
|
|
@@ -43,15 +32,13 @@ const MessagesScreen = () => {
|
|
const [chats, setChats] = useState<Chat[]>([]);
|
|
const [chats, setChats] = useState<Chat[]>([]);
|
|
const [index, setIndex] = useState(0);
|
|
const [index, setIndex] = useState(0);
|
|
const { data: chatsData, refetch } = usePostGetChatsListQuery(token, index === 2 ? 1 : 0, true);
|
|
const { data: chatsData, refetch } = usePostGetChatsListQuery(token, index === 2 ? 1 : 0, true);
|
|
|
|
+
|
|
const [filteredChats, setFilteredChats] = useState<Chat[]>([]);
|
|
const [filteredChats, setFilteredChats] = useState<Chat[]>([]);
|
|
const [search, setSearch] = useState('');
|
|
const [search, setSearch] = useState('');
|
|
|
|
+ const { mutateAsync: deleteChat } = usePostDeleteChatMutation();
|
|
const openRowRef = useRef<any>(null);
|
|
const openRowRef = useRef<any>(null);
|
|
-
|
|
|
|
- const routes: Routes[] = [
|
|
|
|
- { key: 'all', title: 'All' },
|
|
|
|
- { key: 'unread', title: 'Unread' },
|
|
|
|
- { key: 'archived', title: 'Archived' }
|
|
|
|
- ];
|
|
|
|
|
|
+ const { isWarningModalVisible, setIsWarningModalVisible, selectedChat, setSelectedChat } =
|
|
|
|
+ useChatStore();
|
|
|
|
|
|
const handleRowOpen = (ref: any) => {
|
|
const handleRowOpen = (ref: any) => {
|
|
if (openRowRef.current && openRowRef.current !== ref) {
|
|
if (openRowRef.current && openRowRef.current !== ref) {
|
|
@@ -86,30 +73,30 @@ const MessagesScreen = () => {
|
|
);
|
|
);
|
|
|
|
|
|
const filterChatsByTab = () => {
|
|
const filterChatsByTab = () => {
|
|
|
|
+ let filteredList = chats;
|
|
|
|
+
|
|
if (index === 1) {
|
|
if (index === 1) {
|
|
- setFilteredChats(
|
|
|
|
- chats
|
|
|
|
- .filter((chat: Chat) => chat.unread_count > 0)
|
|
|
|
- .sort((a: Chat, b: Chat) => b.pin - a.pin || b.pin_order - a.pin_order)
|
|
|
|
- );
|
|
|
|
- } else if (index === 2) {
|
|
|
|
- setFilteredChats(
|
|
|
|
- chats.sort((a: Chat, b: Chat) => b.pin - a.pin || b.pin_order - a.pin_order)
|
|
|
|
- );
|
|
|
|
- } else {
|
|
|
|
- setFilteredChats(
|
|
|
|
- chats.sort((a: Chat, b: Chat) => b.pin - a.pin || b.pin_order - a.pin_order)
|
|
|
|
- );
|
|
|
|
|
|
+ filteredList = chats.filter((chat) => chat.unread_count > 0);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ filteredList.sort((a, b) => b.pin - a.pin || b.pin_order - a.pin_order);
|
|
|
|
+ setFilteredChats(filteredList);
|
|
};
|
|
};
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
filterChatsByTab();
|
|
filterChatsByTab();
|
|
- }, [index, chats]);
|
|
|
|
|
|
+ }, [chats, index]);
|
|
|
|
+
|
|
|
|
+ const handleDeleteChat = async () => {
|
|
|
|
+ if (!selectedChat) return;
|
|
|
|
|
|
- const handleDeleteChat = (id: number) => {
|
|
|
|
- const updatedChats = chats.filter((chat: Chat) => chat.uid !== id);
|
|
|
|
- setChats(updatedChats);
|
|
|
|
|
|
+ await deleteChat({
|
|
|
|
+ token: token,
|
|
|
|
+ conversation_with_user: selectedChat.uid
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ refetch();
|
|
|
|
+ setSelectedChat(null);
|
|
};
|
|
};
|
|
|
|
|
|
const searchFilter = (text: string) => {
|
|
const searchFilter = (text: string) => {
|
|
@@ -128,32 +115,11 @@ const MessagesScreen = () => {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
- const formatDate = (dateString: Date) => {
|
|
|
|
- const inputDate = moment.utc(dateString).local();
|
|
|
|
- const today = moment().local();
|
|
|
|
-
|
|
|
|
- const yesterday = moment().local().subtract(1, 'days');
|
|
|
|
-
|
|
|
|
- if (inputDate.isSame(today, 'day')) {
|
|
|
|
- return inputDate.format('HH:mm');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (inputDate.isSame(yesterday, 'day')) {
|
|
|
|
- return 'yesterday';
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (!inputDate.isSame(today, 'year')) {
|
|
|
|
- return inputDate.format('DD.MM.YYYY');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return inputDate.format('DD.MM');
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
const renderChatItem = ({ item }: { item: Chat }) => {
|
|
const renderChatItem = ({ item }: { item: Chat }) => {
|
|
return (
|
|
return (
|
|
<SwipeableRow
|
|
<SwipeableRow
|
|
chat={{
|
|
chat={{
|
|
- id: item.uid,
|
|
|
|
|
|
+ uid: item.uid,
|
|
name: item.name,
|
|
name: item.name,
|
|
avatar: item.avatar,
|
|
avatar: item.avatar,
|
|
pin: item.pin,
|
|
pin: item.pin,
|
|
@@ -265,84 +231,16 @@ const MessagesScreen = () => {
|
|
|
|
|
|
<SearchModal />
|
|
<SearchModal />
|
|
<MoreModal />
|
|
<MoreModal />
|
|
|
|
+ <WarningModal
|
|
|
|
+ type={'delete'}
|
|
|
|
+ isVisible={isWarningModalVisible}
|
|
|
|
+ onClose={() => setIsWarningModalVisible(false)}
|
|
|
|
+ title="Delete conversation"
|
|
|
|
+ message={`Are you sure you want to delete conversation with ${selectedChat?.name}?\nThis conversation will be deleted for both sides.`}
|
|
|
|
+ action={handleDeleteChat}
|
|
|
|
+ />
|
|
</PageWrapper>
|
|
</PageWrapper>
|
|
);
|
|
);
|
|
};
|
|
};
|
|
|
|
|
|
-const styles = StyleSheet.create({
|
|
|
|
- title: { color: Colors.DARK_BLUE, fontFamily: 'redhat-700', fontSize: getFontSize(14) },
|
|
|
|
- header: {
|
|
|
|
- alignItems: 'center',
|
|
|
|
- paddingVertical: 16,
|
|
|
|
- flexDirection: 'row',
|
|
|
|
- justifyContent: 'space-between',
|
|
|
|
- marginLeft: '5%',
|
|
|
|
- marginRight: '5%'
|
|
|
|
- },
|
|
|
|
- chatItem: {
|
|
|
|
- flexDirection: 'row',
|
|
|
|
- paddingVertical: 12,
|
|
|
|
- backgroundColor: Colors.WHITE,
|
|
|
|
- borderBottomWidth: 1,
|
|
|
|
- borderBottomColor: Colors.FILL_LIGHT,
|
|
|
|
- gap: 8,
|
|
|
|
- paddingHorizontal: '4%'
|
|
|
|
- },
|
|
|
|
- avatar: {
|
|
|
|
- width: 54,
|
|
|
|
- height: 54,
|
|
|
|
- borderRadius: 27,
|
|
|
|
- borderWidth: 1,
|
|
|
|
- borderColor: Colors.FILL_LIGHT
|
|
|
|
- },
|
|
|
|
- rowContainer: {
|
|
|
|
- flexDirection: 'row',
|
|
|
|
- alignItems: 'center',
|
|
|
|
- justifyContent: 'space-between'
|
|
|
|
- },
|
|
|
|
- chatName: {
|
|
|
|
- flex: 1,
|
|
|
|
- fontFamily: 'montserrat-700',
|
|
|
|
- fontSize: getFontSize(14),
|
|
|
|
- color: Colors.DARK_BLUE
|
|
|
|
- },
|
|
|
|
- chatMessage: {
|
|
|
|
- flex: 1,
|
|
|
|
- fontSize: getFontSize(12),
|
|
|
|
- fontWeight: '600',
|
|
|
|
- color: Colors.DARK_BLUE,
|
|
|
|
- height: '100%'
|
|
|
|
- },
|
|
|
|
- chatTimeContainer: {
|
|
|
|
- justifyContent: 'center',
|
|
|
|
- alignItems: 'flex-end'
|
|
|
|
- },
|
|
|
|
- chatTime: {
|
|
|
|
- fontSize: getFontSize(12),
|
|
|
|
- fontWeight: '600',
|
|
|
|
- color: Colors.LIGHT_GRAY
|
|
|
|
- },
|
|
|
|
- unreadBadge: {
|
|
|
|
- backgroundColor: Colors.ORANGE,
|
|
|
|
- borderRadius: 9,
|
|
|
|
- paddingHorizontal: 6,
|
|
|
|
- alignItems: 'center',
|
|
|
|
- justifyContent: 'center',
|
|
|
|
- height: 18,
|
|
|
|
- minWidth: 18
|
|
|
|
- },
|
|
|
|
- unreadText: {
|
|
|
|
- color: Colors.WHITE,
|
|
|
|
- fontSize: getFontSize(11),
|
|
|
|
- fontFamily: 'montserrat-700'
|
|
|
|
- },
|
|
|
|
- swipeActions: {
|
|
|
|
- flexDirection: 'row',
|
|
|
|
- alignItems: 'center'
|
|
|
|
- },
|
|
|
|
- swipeButton: {
|
|
|
|
- paddingHorizontal: 20
|
|
|
|
- }
|
|
|
|
-});
|
|
|
|
-
|
|
|
|
export default MessagesScreen;
|
|
export default MessagesScreen;
|