RouteAddGroup.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import React, { useEffect, useState } from 'react';
  2. import {
  3. View,
  4. Text,
  5. TouchableOpacity,
  6. StyleSheet,
  7. Image,
  8. ActivityIndicator,
  9. ScrollView
  10. } from 'react-native';
  11. import { useNavigation } from '@react-navigation/native';
  12. import { Colors } from 'src/theme';
  13. import { Input } from 'src/components';
  14. import { storage, StoreType } from 'src/storage';
  15. import { PostCreateGroup, usePostCreateGroupMutation } from '@api/chat';
  16. import { API_HOST } from 'src/constants';
  17. import { AvatarWithInitials } from 'src/components';
  18. import * as ImagePicker from 'expo-image-picker';
  19. import * as yup from 'yup';
  20. import { FlashList } from '@shopify/flash-list';
  21. import { useSheetRouter } from 'react-native-actions-sheet';
  22. import { getFontSize } from 'src/utils';
  23. import CloseIcon from 'assets/icons/close.svg';
  24. import CameraIcon from 'assets/icons/messages/camera.svg';
  25. import { Formik } from 'formik';
  26. import { useGroupChatStore } from 'src/stores/groupChatStore';
  27. import { NAVIGATION_PAGES } from 'src/types';
  28. const ProfileSchema = yup.object({
  29. name: yup
  30. .string()
  31. .required('name is required')
  32. .min(3, 'group name should be at least 3 characters'),
  33. description: yup.string().optional().max(8000, 'description should not exceed 8000 characters'),
  34. users: yup.array().min(2, 'select at least 2 members').required('members are required')
  35. });
  36. const RouteAddGroup = () => {
  37. const router = useSheetRouter('search-modal');
  38. const token = storage.get('token', StoreType.STRING) as string;
  39. const navigation = useNavigation();
  40. const {
  41. selectedUsers,
  42. removeUser,
  43. image,
  44. setImage,
  45. groupName,
  46. setGroupName,
  47. description,
  48. setDescription,
  49. clearStore
  50. } = useGroupChatStore();
  51. const [isSubmitting, setIsSubmitting] = useState(false);
  52. const { mutate: createGroup } = usePostCreateGroupMutation();
  53. const pickImage = async () => {
  54. let result = await ImagePicker.launchImageLibraryAsync({
  55. mediaTypes: ImagePicker.MediaTypeOptions.Images,
  56. allowsEditing: true,
  57. aspect: [4, 3],
  58. quality: 1
  59. });
  60. if (!result.canceled) {
  61. setImage(result.assets[0]);
  62. }
  63. };
  64. const renderUserItem = ({ item }: { item: any }) => {
  65. return (
  66. <View style={styles.selectedUserContainer}>
  67. <View style={styles.userContainer}>
  68. <TouchableOpacity onPress={() => removeUser(item.user_id)} style={styles.removeIcon}>
  69. <CloseIcon width={10} height={10} fill={Colors.WHITE} />
  70. </TouchableOpacity>
  71. {item.avatar ? (
  72. <Image source={{ uri: API_HOST + item.avatar }} style={styles.selectedAvatar} />
  73. ) : (
  74. <AvatarWithInitials
  75. text={`${item.first_name[0] ?? ''}${item.last_name[0] ?? ''}`}
  76. flag={API_HOST + item.flag1}
  77. size={60}
  78. fontSize={21}
  79. borderColor={Colors.LIGHT_GRAY}
  80. borderWidth={1}
  81. />
  82. )}
  83. </View>
  84. </View>
  85. );
  86. };
  87. return (
  88. <Formik
  89. validationSchema={ProfileSchema}
  90. initialValues={{
  91. name: groupName,
  92. description,
  93. users: selectedUsers
  94. }}
  95. onSubmit={async (values) => {
  96. setIsSubmitting(true);
  97. const groupData: PostCreateGroup = {
  98. token,
  99. name: values.name,
  100. description: values.description,
  101. users: selectedUsers.map((user) => +user.user_id),
  102. admins: []
  103. };
  104. if (image && image.uri) {
  105. groupData.group_avatar = {
  106. type: image.type || 'image',
  107. uri: image.uri,
  108. name: image.uri.split('/').pop()!
  109. };
  110. }
  111. await createGroup(groupData, {
  112. onSuccess: (res) => {
  113. setIsSubmitting(false);
  114. navigation.navigate(
  115. ...([
  116. NAVIGATION_PAGES.GROUP_CHAT,
  117. {
  118. group_token: res.groupToken,
  119. name: values.name,
  120. avatar: res.groupAvatar,
  121. userType: 'normal'
  122. }
  123. ] as never)
  124. );
  125. router?.close();
  126. },
  127. onError: (err) => {
  128. console.log('err', err);
  129. setIsSubmitting(false);
  130. }
  131. });
  132. }}
  133. >
  134. {(props) => {
  135. useEffect(() => {
  136. props.setFieldValue('users', selectedUsers);
  137. }, [selectedUsers]);
  138. return (
  139. <View style={styles.container}>
  140. <View style={styles.header}>
  141. <TouchableOpacity
  142. onPress={() => {
  143. router?.goBack();
  144. }}
  145. style={{
  146. paddingTop: 16,
  147. paddingBottom: 6,
  148. paddingHorizontal: 6
  149. }}
  150. >
  151. <Text style={styles.headerText}>Back</Text>
  152. </TouchableOpacity>
  153. {isSubmitting ? (
  154. <View
  155. style={{
  156. paddingTop: 10,
  157. paddingHorizontal: 10
  158. }}
  159. >
  160. <ActivityIndicator size="small" color={Colors.DARK_BLUE} />
  161. </View>
  162. ) : (
  163. <TouchableOpacity
  164. style={{
  165. paddingTop: 16,
  166. paddingBottom: 6,
  167. paddingHorizontal: 6
  168. }}
  169. onPress={() => props.handleSubmit()}
  170. >
  171. <Text style={styles.headerText}>Save</Text>
  172. </TouchableOpacity>
  173. )}
  174. </View>
  175. <ScrollView
  176. showsVerticalScrollIndicator={false}
  177. style={{ flex: 1 }}
  178. contentContainerStyle={{ gap: 16 }}
  179. >
  180. <View style={styles.photoContainer}>
  181. <TouchableOpacity style={styles.photoContainer} onPress={pickImage}>
  182. {!image && (
  183. <>
  184. <View style={[styles.groupPhoto, { backgroundColor: Colors.FILL_LIGHT }]}>
  185. <CameraIcon width={36} height={36} fill={Colors.LIGHT_GRAY} />
  186. </View>
  187. <Text style={styles.photoText}>Add photo</Text>
  188. </>
  189. )}
  190. {image && (
  191. <>
  192. <Image
  193. source={{ uri: image.uri }}
  194. style={{
  195. width: 80,
  196. height: 80,
  197. borderRadius: 40,
  198. borderWidth: 1,
  199. borderColor: Colors.FILL_LIGHT
  200. }}
  201. />
  202. <Text style={styles.photoText}>Change photo</Text>
  203. </>
  204. )}
  205. </TouchableOpacity>
  206. </View>
  207. <Input
  208. placeholder="Add group name"
  209. value={props.values.name}
  210. inputMode={'text'}
  211. onChange={(text) => {
  212. props.handleChange('name')(text);
  213. setGroupName(text);
  214. }}
  215. onBlur={props.handleBlur('name')}
  216. header="Group name"
  217. formikError={props.touched.name && props.errors.name}
  218. />
  219. <Input
  220. placeholder="Add group description"
  221. value={props.values.description}
  222. onChange={(text) => {
  223. props.handleChange('description')(text);
  224. setDescription(text);
  225. }}
  226. onBlur={props.handleBlur('description')}
  227. header="Description"
  228. multiline
  229. height={58}
  230. formikError={props.touched.description && props.errors.description}
  231. />
  232. {selectedUsers.length > 0 ? (
  233. <View
  234. style={[
  235. styles.usersRow,
  236. props.errors.users ? { borderColor: Colors.RED, borderWidth: 1 } : {}
  237. ]}
  238. >
  239. <FlashList
  240. viewabilityConfig={{
  241. waitForInteraction: true,
  242. itemVisiblePercentThreshold: 50,
  243. minimumViewTime: 1000
  244. }}
  245. scrollEnabled={false}
  246. data={selectedUsers}
  247. renderItem={renderUserItem}
  248. keyExtractor={(item) => item.user_id.toString()}
  249. estimatedItemSize={100}
  250. extraData={selectedUsers}
  251. showsVerticalScrollIndicator={false}
  252. contentContainerStyle={styles.selectedUsersList}
  253. numColumns={4}
  254. />
  255. </View>
  256. ) : null}
  257. {props.errors.users && (
  258. <Text
  259. style={[styles.textError, selectedUsers.length > 0 ? { marginTop: -32 } : {}]}
  260. >
  261. select at least 2 members
  262. </Text>
  263. )}
  264. </ScrollView>
  265. </View>
  266. );
  267. }}
  268. </Formik>
  269. );
  270. };
  271. const styles = StyleSheet.create({
  272. container: {
  273. gap: 16,
  274. height: '100%',
  275. backgroundColor: Colors.WHITE
  276. },
  277. header: {
  278. flexDirection: 'row',
  279. justifyContent: 'space-between',
  280. alignItems: 'center'
  281. },
  282. headerText: {
  283. color: Colors.DARK_BLUE,
  284. fontSize: getFontSize(14),
  285. fontWeight: '700'
  286. },
  287. photoContainer: {
  288. alignItems: 'center',
  289. gap: 8
  290. },
  291. groupPhoto: {
  292. width: 80,
  293. height: 80,
  294. borderRadius: 40,
  295. alignItems: 'center',
  296. justifyContent: 'center'
  297. },
  298. photoText: {
  299. color: Colors.DARK_BLUE,
  300. fontSize: 12,
  301. fontWeight: '700'
  302. },
  303. input: {
  304. marginBottom: 12
  305. },
  306. userItem: {
  307. flexDirection: 'row',
  308. alignItems: 'center',
  309. paddingVertical: 8,
  310. paddingHorizontal: 12,
  311. backgroundColor: Colors.FILL_LIGHT,
  312. gap: 8,
  313. borderRadius: 8,
  314. marginBottom: 6
  315. },
  316. avatar: {
  317. width: 36,
  318. height: 36,
  319. borderRadius: 18,
  320. borderWidth: 1,
  321. borderColor: Colors.LIGHT_GRAY
  322. },
  323. userName: {
  324. color: Colors.DARK_BLUE,
  325. fontSize: getFontSize(14),
  326. fontFamily: 'montserrat-700'
  327. },
  328. userSubtitle: {
  329. color: Colors.DARK_BLUE,
  330. fontSize: 14,
  331. fontFamily: 'montserrat-500'
  332. },
  333. userNM: {
  334. color: Colors.DARK_BLUE,
  335. fontSize: 14,
  336. fontFamily: 'montserrat-700',
  337. marginRight: 12
  338. },
  339. unselectedCircle: {
  340. width: 20,
  341. height: 20,
  342. borderRadius: 10,
  343. borderWidth: 1,
  344. borderColor: Colors.LIGHT_GRAY,
  345. justifyContent: 'center',
  346. alignItems: 'center'
  347. },
  348. selectedUsersList: {
  349. paddingTop: 12
  350. },
  351. selectedUserContainer: {
  352. position: 'relative',
  353. width: '100%',
  354. alignItems: 'center',
  355. paddingBottom: 12
  356. },
  357. userContainer: {},
  358. selectedAvatar: {
  359. width: 60,
  360. height: 60,
  361. borderRadius: 30,
  362. borderWidth: 1,
  363. borderColor: Colors.LIGHT_GRAY
  364. },
  365. removeIcon: {
  366. position: 'absolute',
  367. top: -4,
  368. right: -4,
  369. width: 22,
  370. height: 22,
  371. borderRadius: 11,
  372. borderWidth: 1,
  373. borderColor: Colors.WHITE,
  374. backgroundColor: Colors.RED,
  375. justifyContent: 'center',
  376. alignItems: 'center',
  377. zIndex: 1
  378. },
  379. usersRow: {
  380. flex: 1,
  381. backgroundColor: Colors.FILL_LIGHT,
  382. borderRadius: 8,
  383. marginBottom: 24
  384. },
  385. textError: {
  386. color: Colors.RED,
  387. fontSize: getFontSize(12),
  388. fontFamily: 'redhat-600',
  389. marginTop: 5
  390. }
  391. });
  392. export default RouteAddGroup;