index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import React, { useEffect, useState } from 'react';
  2. import { View, Text, Image, TouchableOpacity } from 'react-native';
  3. import { qualityOptions } from '../../utils/constants';
  4. import { RegionAddData } from '../../utils/types';
  5. import { Colors } from 'src/theme';
  6. import { API_HOST } from 'src/constants';
  7. import { styles } from './styles';
  8. import TrashSvg from 'assets/icons/travels-screens/trash-solid.svg';
  9. import ChevronIcon from 'assets/icons/travels-screens/down-arrow.svg';
  10. import CheckSvg from 'assets/icons/travels-screens/check.svg';
  11. import SquareSvg from 'assets/icons/travels-screens/square.svg';
  12. const RegionItem = ({
  13. region,
  14. onDelete,
  15. onToggleStatus,
  16. onQualityChange,
  17. onHiddenChange,
  18. startDate,
  19. endDate,
  20. isEditing
  21. }: {
  22. region: RegionAddData;
  23. onDelete: () => void;
  24. onToggleStatus: () => void;
  25. onQualityChange: () => void;
  26. onHiddenChange: () => void;
  27. startDate: string | null;
  28. endDate: string | null;
  29. isEditing: boolean;
  30. }) => {
  31. const name = region.region_name.split(/ – | - /);
  32. const qualityName = region.quality
  33. ? qualityOptions.find((q) => q.id === region.quality)?.name
  34. : 'Good Visit';
  35. const disabled = !startDate || startDate > new Date().toISOString().split('T')[0];
  36. const completed = endDate && endDate <= new Date().toISOString().split('T')[0];
  37. const [isCompleted, setIsCompleted] = useState(false);
  38. useEffect(() => {
  39. if (isEditing) return;
  40. setIsCompleted(completed as boolean);
  41. if (completed && region.status === 0) {
  42. region.status = 1;
  43. } else {
  44. region.status = 0;
  45. }
  46. }, [completed, endDate]);
  47. return (
  48. <View key={region.id} style={styles.regionItem}>
  49. <View style={styles.regionItemTop}>
  50. <View style={styles.regionHeader}>
  51. <Image
  52. source={{ uri: `${API_HOST}/img/flags_new/${region.flag1}` }}
  53. style={styles.flagStyle}
  54. />
  55. {region.flag2 && (
  56. <Image
  57. source={{ uri: `${API_HOST}/img/flags_new/${region.flag2}` }}
  58. style={[styles.flagStyle, { marginLeft: -17 }]}
  59. />
  60. )}
  61. <View style={styles.nameContainer}>
  62. <Text style={styles.regionName}>{name[0]}</Text>
  63. <Text style={styles.regionSubname}>{name[1]}</Text>
  64. </View>
  65. </View>
  66. <TouchableOpacity style={styles.trashBtn} onPress={onDelete}>
  67. <TrashSvg fill={Colors.WHITE} />
  68. </TouchableOpacity>
  69. </View>
  70. <View style={styles.divider}></View>
  71. <Text style={styles.qualityOfVisit}>Quality of visit</Text>
  72. <View style={styles.regionItemBottom}>
  73. <TouchableOpacity style={styles.qualitySelector} onPress={onQualityChange}>
  74. <Text style={styles.qualityButtonText}>{qualityName}</Text>
  75. <ChevronIcon width={18} height={18} />
  76. </TouchableOpacity>
  77. <TouchableOpacity
  78. style={[
  79. styles.markCompletedButton,
  80. (disabled) && {
  81. backgroundColor: Colors.LIGHT_GRAY,
  82. borderColor: Colors.LIGHT_GRAY
  83. }
  84. ]}
  85. onPress={() => {
  86. onToggleStatus();
  87. setIsCompleted(false);
  88. }}
  89. disabled={disabled}
  90. >
  91. <View style={styles.completedContainer}>
  92. <View style={{ position: 'relative' }}>
  93. <SquareSvg />
  94. </View>
  95. {(isCompleted || (region.status === 1 && !disabled)) && (
  96. <View style={{ position: 'absolute', left: 2, top: 1 }}>
  97. <CheckSvg width={14} height={14} />
  98. </View>
  99. )}
  100. <Text style={styles.markCompletedButtonText}>Completed</Text>
  101. </View>
  102. </TouchableOpacity>
  103. </View>
  104. {region.can_be_hidden && (
  105. <View style={{ marginTop: 12, marginHorizontal: '25%' }}>
  106. <TouchableOpacity
  107. style={[
  108. styles.markCompletedButton,
  109. disabled && {
  110. backgroundColor: Colors.LIGHT_GRAY,
  111. borderColor: Colors.LIGHT_GRAY
  112. }
  113. ]}
  114. onPress={onHiddenChange}
  115. disabled={disabled}
  116. >
  117. <View style={styles.completedContainer}>
  118. <View style={{ position: 'relative' }}>
  119. <SquareSvg />
  120. </View>
  121. {region.hidden && (
  122. <View style={{ position: 'absolute', left: 2, top: 1 }}>
  123. <CheckSvg width={14} height={14} />
  124. </View>
  125. )}
  126. <Text style={styles.markCompletedButtonText}>Hidden</Text>
  127. </View>
  128. </TouchableOpacity>
  129. </View>
  130. )}
  131. </View>
  132. );
  133. };
  134. export default RegionItem;