about summary refs log tree commit diff
path: root/src/view/com/composer/PhotoCarouselPicker.tsx
blob: 208e8070b7ffeeee4e8da8551e3f5110ffa9188b (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, {useCallback} from 'react'
import {Image, StyleSheet, TouchableOpacity, ScrollView} from 'react-native'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {colors} from '../../lib/styles'
import {
  openPicker,
  openCamera,
  openCropper,
} from 'react-native-image-crop-picker'

export const PhotoCarouselPicker = ({
  selectedPhotos,
  setSelectedPhotos,
  localPhotos,
}: {
  selectedPhotos: string[]
  setSelectedPhotos: React.Dispatch<React.SetStateAction<string[]>>
  localPhotos: any
}) => {
  const handleOpenCamera = useCallback(() => {
    openCamera({
      mediaType: 'photo',
      cropping: true,
      width: 1000,
      height: 1000,
    }).then(item => {
      setSelectedPhotos([item.path, ...selectedPhotos])
    })
  }, [selectedPhotos, setSelectedPhotos])

  const handleSelectPhoto = useCallback(
    async (uri: string) => {
      const img = await openCropper({
        mediaType: 'photo',
        path: uri,
        width: 1000,
        height: 1000,
      })
      setSelectedPhotos([img.path, ...selectedPhotos])
    },
    [selectedPhotos, setSelectedPhotos],
  )

  const handleOpenGallery = useCallback(() => {
    openPicker({
      multiple: true,
      maxFiles: 4,
      mediaType: 'photo',
    }).then(async items => {
      const result = []

      for await (const image of items) {
        const img = await openCropper({
          mediaType: 'photo',
          path: image.path,
          width: 1000,
          height: 1000,
        })
        result.push(img.path)
      }
      setSelectedPhotos([
        // ...items.reduce(
        //   (accum, cur) => accum.concat(cur.sourceURL!),
        //   [] as string[],
        // ),
        ...result,
        ...selectedPhotos,
      ])
    })
  }, [selectedPhotos, setSelectedPhotos])

  return (
    <ScrollView
      horizontal
      style={styles.photosContainer}
      showsHorizontalScrollIndicator={false}>
      <TouchableOpacity
        style={[styles.galleryButton, styles.photo]}
        onPress={handleOpenCamera}>
        <FontAwesomeIcon
          icon="camera"
          size={24}
          style={{color: colors.blue3}}
        />
      </TouchableOpacity>
      {localPhotos.photos.map((item: any, index: number) => (
        <TouchableOpacity
          key={`local-image-${index}`}
          style={styles.photoButton}
          onPress={() => handleSelectPhoto(item.node.image.uri)}>
          <Image style={styles.photo} source={{uri: item.node.image.uri}} />
        </TouchableOpacity>
      ))}
      <TouchableOpacity
        style={[styles.galleryButton, styles.photo]}
        onPress={handleOpenGallery}>
        <FontAwesomeIcon icon="image" style={{color: colors.blue3}} size={24} />
      </TouchableOpacity>
    </ScrollView>
  )
}

const styles = StyleSheet.create({
  photosContainer: {
    width: '100%',
    maxHeight: 96,
    padding: 8,
    overflow: 'hidden',
  },
  galleryButton: {
    borderWidth: 1,
    borderColor: colors.gray3,
    alignItems: 'center',
    justifyContent: 'center',
  },
  photoButton: {
    width: 75,
    height: 75,
    marginRight: 8,
    borderWidth: 1,
    borderRadius: 16,
    borderColor: colors.gray3,
  },
  photo: {
    width: 75,
    height: 75,
    marginRight: 8,
    borderRadius: 16,
  },
})