PhotoEditModal.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import React, { useEffect, useState } from 'react';
  2. import {
  3. View,
  4. Text,
  5. TouchableOpacity,
  6. Platform,
  7. KeyboardAvoidingView,
  8. ScrollView,
  9. Image
  10. } from 'react-native';
  11. import { Button, Input, Modal, FlatList as List } from 'src/components';
  12. import RangeCalendar from 'src/components/Calendars/RangeCalendar';
  13. import { ButtonVariants } from 'src/types/components';
  14. import { AllRegions, PhotosData } from '../utils/types';
  15. import { getImageUri } from '../utils';
  16. import { styles } from './styles';
  17. import ChevronIcon from '../../../../../assets/icons/travels-screens/chevron-bottom.svg';
  18. import CalendarSvg from '../../../../../assets/icons/calendar.svg';
  19. export const PhotoEditModal = ({
  20. isVisible,
  21. onClose,
  22. photo,
  23. allRegions,
  24. handleUpdate,
  25. description,
  26. setDescription,
  27. selectedDate,
  28. setSelectedDate,
  29. selectedRegion,
  30. setSelectedRegion
  31. }: {
  32. isVisible: boolean;
  33. onClose: () => void;
  34. photo: PhotosData;
  35. allRegions: AllRegions[];
  36. handleUpdate: () => void;
  37. description: string | null;
  38. setDescription: (text: string) => void;
  39. selectedDate: string | null;
  40. setSelectedDate: (date: string) => void;
  41. selectedRegion: AllRegions | null;
  42. setSelectedRegion: (region: any) => void;
  43. }) => {
  44. const [imgAspectRatio, setImgAspectRatio] = useState(0);
  45. const [isRegionsModalVisible, setIsRegionsModalVisible] = useState(false);
  46. const [calendarVisible, setCalendarVisible] = useState(false);
  47. useEffect(() => {
  48. if (photo) {
  49. const uri = getImageUri(photo.url_mid);
  50. Image.getSize(
  51. uri,
  52. (width, height) => {
  53. const aspectRatio = width / height;
  54. setImgAspectRatio(aspectRatio);
  55. },
  56. (error) => {
  57. console.error(`Couldn't get the image size: ${error}`);
  58. }
  59. );
  60. }
  61. }, [photo]);
  62. return (
  63. <Modal visible={isVisible} onRequestClose={onClose} headerTitle="Edit photo">
  64. <KeyboardAvoidingView
  65. behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
  66. style={{ flex: 1 }}
  67. keyboardVerticalOffset={Platform.select({ ios: 44, android: 27 })}
  68. >
  69. <ScrollView
  70. keyboardShouldPersistTaps="handled"
  71. contentContainerStyle={{ flexGrow: 1 }}
  72. showsVerticalScrollIndicator={false}
  73. >
  74. <Image
  75. source={{ uri: getImageUri(photo?.url_mid) }}
  76. style={[
  77. styles.imageEdit,
  78. {
  79. aspectRatio: imgAspectRatio
  80. }
  81. ]}
  82. />
  83. <View style={{ gap: 10, marginTop: 8 }}>
  84. <Input
  85. placeholder="Photo description"
  86. inputMode={'text'}
  87. onChange={(text) => setDescription(text)}
  88. value={description ?? photo?.title}
  89. />
  90. <TouchableOpacity
  91. style={[styles.regionSelector, , { justifyContent: 'space-between' }]}
  92. onPress={() => setIsRegionsModalVisible(true)}
  93. >
  94. <Text style={styles.regionText}>
  95. {selectedRegion?.country ??
  96. allRegions.find((region: AllRegions) => region.id === photo?.region)?.country}
  97. </Text>
  98. <ChevronIcon />
  99. </TouchableOpacity>
  100. <TouchableOpacity
  101. style={styles.regionSelector}
  102. onPress={() => setCalendarVisible(true)}
  103. >
  104. <CalendarSvg />
  105. <Text style={styles.regionText}>{selectedDate ?? photo?.date}</Text>
  106. </TouchableOpacity>
  107. <RangeCalendar
  108. isModalVisible={calendarVisible}
  109. closeModal={(date: Date | null) => {
  110. date && setSelectedDate(date.toISOString().split('T')[0]);
  111. setCalendarVisible(false);
  112. }}
  113. allowRangeSelection={false}
  114. />
  115. </View>
  116. <View style={styles.saveBtn}>
  117. <Button variant={ButtonVariants.FILL} onPress={handleUpdate} children={'Save'} />
  118. </View>
  119. <Modal
  120. onRequestClose={() => setIsRegionsModalVisible(false)}
  121. headerTitle={'Select Region'}
  122. visible={isRegionsModalVisible}
  123. >
  124. <List
  125. itemObject={(object) => {
  126. setSelectedRegion(object);
  127. setIsRegionsModalVisible(false);
  128. }}
  129. initialData={allRegions}
  130. />
  131. </Modal>
  132. </ScrollView>
  133. </KeyboardAvoidingView>
  134. </Modal>
  135. );
  136. };