SearchScreen.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* eslint-disable react/style-prop-object */
  2. import * as WebBrowser from 'expo-web-browser';
  3. import * as React from 'react';
  4. import { StatusBar } from 'expo-status-bar';
  5. import Constants from 'expo-constants';
  6. import {
  7. Div,
  8. Button,
  9. Image,
  10. Text,
  11. Avatar,
  12. Input,
  13. Icon,
  14. Tag,
  15. } from 'react-native-magnus';
  16. import { FlatList } from 'react-native-gesture-handler';
  17. import { useTranslation } from 'react-i18next';
  18. import useModel from 'flooks';
  19. import HomeModel from './Home/model';
  20. import MerchantCom from './Home/MerchantCom';
  21. import Header from '../../components/Header';
  22. import { filterMap } from '../../Utils/MerchantUtils';
  23. export default function SearchScreen({ navigation }) {
  24. const { t } = useTranslation();
  25. const { searchHome } = useModel(HomeModel, ['searchHome']);
  26. const [isSearch, setIsSearch] = React.useState(false);
  27. const [searchType, setsearchType] = React.useState('');
  28. const [empty, setEmpty] = React.useState(false);
  29. const [last, setLast] = React.useState(false);
  30. const [searchVal, setsearchVal] = React.useState('');
  31. const [searchKey, setsearchKey] = React.useState('');
  32. const [page, setPage] = React.useState(0);
  33. const [list, setlist] = React.useState([]);
  34. function search() {
  35. if (searchHome.loading) {
  36. return;
  37. }
  38. setIsSearch(true);
  39. setEmpty(false);
  40. setLast(false);
  41. searchHome(searchVal, searchType, page)
  42. .then((res) => {
  43. if (!res.last) {
  44. setPage(page + 1);
  45. } else {
  46. setLast(true);
  47. }
  48. if (res.empty) {
  49. setEmpty(true);
  50. }
  51. if (page === 0) {
  52. setlist(res.content);
  53. } else {
  54. setlist(list.concat(res.content));
  55. }
  56. })
  57. .catch(() => {
  58. setlist([]);
  59. setLast(true);
  60. setEmpty(true);
  61. });
  62. }
  63. function chooseTag(key) {
  64. setsearchKey(filterMap.get(key).name);
  65. setPage(0);
  66. setsearchType('popularTag');
  67. setsearchVal(key);
  68. search();
  69. }
  70. return (
  71. <>
  72. <StatusBar backgroundColor="transparent" style="dark" translucent />
  73. <Div h={Constants.statusBarHeight} bg="white" />
  74. <Div row bg="white" py={10} alignItems="center">
  75. <Input
  76. bg="gray200"
  77. placeholder={t('xiang-chi-shi-mo-sou-yi-sou')}
  78. p={10}
  79. fontSize="xs"
  80. flex={1}
  81. ml={15}
  82. autoFocus
  83. blurOnSubmit
  84. value={searchKey}
  85. focusBorderColor="brand500"
  86. prefix={<Icon name="search" color="gray300" fontFamily="Feather" />}
  87. onChangeText={(text) => {
  88. setsearchKey(text);
  89. }}
  90. onSubmitEditing={() => {
  91. setPage(0);
  92. setsearchType('search');
  93. setsearchVal(searchKey);
  94. search();
  95. }}
  96. />
  97. <Div>
  98. {isSearch ? (
  99. <Button
  100. w={60}
  101. bg="hide"
  102. color="brand500"
  103. fontSize="xs"
  104. onPress={() => {
  105. setsearchKey('');
  106. setIsSearch(false);
  107. }}
  108. >
  109. {t('qu-xiao')}
  110. </Button>
  111. ) : (
  112. <Button
  113. w={60}
  114. bg="hide"
  115. color="brand500"
  116. fontSize="xs"
  117. onPress={() => navigation.goBack()}
  118. >
  119. {t('fan-hui')}
  120. </Button>
  121. )}
  122. </Div>
  123. </Div>
  124. {!isSearch && (
  125. <Div bg="white" flex={1}>
  126. <Div px={10} mt={10}>
  127. <Text>{t('re-men-sou-suo')}</Text>
  128. <Div row flexWrap="wrap">
  129. {[...filterMap.keys()].map((item, index) => {
  130. return (
  131. <Div key={index} w="33.33%" px={3} mt={10}>
  132. <Button
  133. block
  134. bg="gray200"
  135. color="gray300"
  136. fontSize="xs"
  137. onPress={() => chooseTag(item)}
  138. >
  139. {filterMap.get(item).name}
  140. </Button>
  141. </Div>
  142. );
  143. })}
  144. </Div>
  145. </Div>
  146. </Div>
  147. )}
  148. {isSearch && (
  149. <FlatList
  150. data={list}
  151. renderItem={({ item }) => <MerchantCom info={item} />}
  152. onEndReached={search}
  153. ListEmptyComponent={() => empty && <Text>{t('zan-wu-shu-ju')}</Text>}
  154. />
  155. )}
  156. </>
  157. );
  158. }