1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
import React, {useEffect, useState} from 'react';
import {
Keyboard,
SectionListData,
StatusBar,
StyleSheet,
Text,
View,
} from 'react-native';
import {BottomDrawer} from '../../components';
import {
SEARCH_ENDPOINT_MESSAGES,
SEARCH_ENDPOINT_SUGGESTED,
} from '../../constants';
import {loadSearchResults} from '../../services';
import {ScreenType} from '../../types';
import {normalize} from '../../utils';
import {ChatResultsList, ChatSearchBar} from './index';
interface NewChatModalProps {
modalVisible: boolean;
setChatModalVisible: (open: boolean) => void;
}
const NewChatModal: React.FC<NewChatModalProps> = ({
modalVisible,
setChatModalVisible,
}) => {
const [searching, setSearching] = useState(false);
const [results, setResults] = useState<SectionListData<any>[]>([]);
const [query, setQuery] = useState<string>('');
const handleFocus = () => {
setSearching(true);
};
const handleBlur = () => {
Keyboard.dismiss();
};
const handleCancel = () => {
setQuery('');
setChatModalVisible(false);
};
const getDefaultSuggested = async () => {
const searchResults = await loadSearchResults(
`${SEARCH_ENDPOINT_SUGGESTED}`,
);
const sanitizedResult = [
{
title: 'users',
data: searchResults?.users,
},
];
setResults(sanitizedResult);
};
const getQuerySuggested = async () => {
const searchResults = await loadSearchResults(
`${SEARCH_ENDPOINT_MESSAGES}?query=${query}`,
);
if (query.length > 2) {
const sanitizedResult = [
{
title: 'users',
data: searchResults?.users,
},
];
setResults(sanitizedResult);
} else {
setResults([]);
}
};
useEffect(() => {
if (query.length === 0) {
getDefaultSuggested();
}
if (!searching) {
return;
}
if (query.length < 3) {
return;
}
getQuerySuggested();
}, [query]);
const _modalContent = () => {
return (
<View style={styles.modalShadowContainer}>
<View style={styles.titleContainerStyles}>
<Text style={styles.titleTextStyles}>New Message</Text>
</View>
<ChatSearchBar
onCancel={handleCancel}
onChangeText={setQuery}
onBlur={handleBlur}
onFocus={handleFocus}
value={query}
searching={searching}
placeholder={''}
label={'To:'}
/>
{results.length > 0 && (
<View style={styles.headerContainerStyles}>
<Text style={styles.headerTextStyles}>Suggested</Text>
</View>
)}
<ChatResultsList
{...{results, setChatModalVisible}}
previewType={'Search'}
screenType={ScreenType.Search}
/>
</View>
);
};
return (
<View>
<StatusBar barStyle="dark-content" />
<BottomDrawer
initialSnapPosition={'90%'}
isOpen={modalVisible}
setIsOpen={(open) => {
if (!open) {
setQuery('');
}
setChatModalVisible(open);
}}
showHeader={false}>
{_modalContent()}
</BottomDrawer>
</View>
);
};
const styles = StyleSheet.create({
modalShadowContainer: {
height: '100%',
borderRadius: 9,
backgroundColor: 'white',
},
titleContainerStyles: {marginVertical: 24},
titleTextStyles: {
fontWeight: 'bold',
fontSize: normalize(18),
lineHeight: normalize(21),
textAlign: 'center',
},
headerContainerStyles: {
marginTop: 26,
marginBottom: 10,
marginHorizontal: 28,
},
headerTextStyles: {
fontWeight: 'bold',
fontSize: normalize(17),
lineHeight: normalize(20),
},
});
export default NewChatModal;
|