about summary refs log tree commit diff
path: root/src/view/com/composer/SelectedPhoto.tsx
blob: 393c0b573b7f3cc428e63dc0123670c921c83f18 (plain) (blame)
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
import React, {useCallback} from 'react'
import {Image, StyleSheet, TouchableOpacity, View} from 'react-native'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {colors} from '../../lib/styles'

export const SelectedPhoto = ({
  selectedPhotos,
  onSelectPhotos,
}: {
  selectedPhotos: string[]
  onSelectPhotos: (v: string[]) => void
}) => {
  const imageStyle =
    selectedPhotos.length === 1
      ? styles.image250
      : selectedPhotos.length === 2
      ? styles.image175
      : styles.image85

  const handleRemovePhoto = useCallback(
    item => {
      onSelectPhotos(selectedPhotos.filter(filterItem => filterItem !== item))
    },
    [selectedPhotos, onSelectPhotos],
  )

  return selectedPhotos.length !== 0 ? (
    <View testID="selectedPhotosView" style={styles.imageContainer}>
      {selectedPhotos.length !== 0 &&
        selectedPhotos.map((item, index) => (
          <View
            key={`selected-image-${index}`}
            style={[styles.image, imageStyle]}>
            <TouchableOpacity
              testID="removePhotoButton"
              onPress={() => handleRemovePhoto(item)}
              style={styles.removePhotoButton}>
              <FontAwesomeIcon
                icon="xmark"
                size={16}
                style={{color: colors.white}}
              />
            </TouchableOpacity>

            <Image
              testID="selectedPhotoImage"
              style={[styles.image, imageStyle]}
              source={{uri: item}}
            />
          </View>
        ))}
    </View>
  ) : null
}

const styles = StyleSheet.create({
  imageContainer: {
    flex: 1,
    flexDirection: 'row',
    marginTop: 16,
  },
  image: {
    resizeMode: 'contain',
    borderRadius: 8,
    margin: 2,
    backgroundColor: colors.gray1,
  },
  image250: {
    width: 250,
    height: 250,
  },
  image175: {
    width: 175,
    height: 175,
  },
  image85: {
    width: 85,
    height: 85,
  },
  removePhotoButton: {
    position: 'absolute',
    top: 8,
    right: 8,
    width: 24,
    height: 24,
    borderRadius: 12,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: colors.black,
    zIndex: 1,
  },
})