From b6b18aaaaada53f3503f0779a9e05adc1f5ccd0e Mon Sep 17 00:00:00 2001 From: balzack Date: Sat, 1 Oct 2022 00:32:25 -0700 Subject: [PATCH] preparing assets for message --- .../conversation/addTopic/AddTopic.jsx | 52 ++++++++++++++++++- .../conversation/addTopic/AddTopic.styled.js | 20 +++++-- .../conversation/addTopic/useAddTopic.hook.js | 15 +++++- 3 files changed, 81 insertions(+), 6 deletions(-) diff --git a/app/mobile/src/session/conversation/addTopic/AddTopic.jsx b/app/mobile/src/session/conversation/addTopic/AddTopic.jsx index 5c0216e3..7cc24200 100644 --- a/app/mobile/src/session/conversation/addTopic/AddTopic.jsx +++ b/app/mobile/src/session/conversation/addTopic/AddTopic.jsx @@ -1,4 +1,4 @@ -import { TextInput, View, TouchableOpacity, Text, } from 'react-native'; +import { Image, FlatList, TextInput, Alert, View, TouchableOpacity, Text, } from 'react-native'; import { useState, useRef } from 'react'; import { useAddTopic } from './useAddTopic.hook'; import { styles } from './AddTopic.styled'; @@ -6,17 +6,65 @@ import AntIcons from '@expo/vector-icons/AntDesign'; import MaterialIcons from '@expo/vector-icons/MaterialCommunityIcons'; import Colors from 'constants/Colors'; import { SafeAreaView } from 'react-native-safe-area-context'; +import ImagePicker from 'react-native-image-crop-picker' export function AddTopic() { const { state, actions } = useAddTopic(); + const addImage = async () => { + try { + const full = await ImagePicker.openPicker({ mediaType: 'photo', includeBase64: true }); + actions.addImage(full.path); + } + catch (err) { + console.log(err); + } + } + + const remove = (item) => { + Alert.alert( + `Removing ${item.type} from message.`, + "Confirm?", + [ + { text: "Cancel", + onPress: () => {}, + }, + { text: "Remove", onPress: () => { + actions.removeAsset(item.key); + }} + ] + ); + } + + const renderAsset = ({ item }) => { + if (item.type === 'image') { + return ( + remove(item)}> + + + ); + } + else { + return ( + + ); + } + } + return ( + { state.assets.length > 0 && ( + + )} - + diff --git a/app/mobile/src/session/conversation/addTopic/AddTopic.styled.js b/app/mobile/src/session/conversation/addTopic/AddTopic.styled.js index d4c3d161..5ce2051c 100644 --- a/app/mobile/src/session/conversation/addTopic/AddTopic.styled.js +++ b/app/mobile/src/session/conversation/addTopic/AddTopic.styled.js @@ -11,8 +11,8 @@ export const styles = StyleSheet.create({ addButtons: { display: 'flex', flexDirection: 'row', - marginLeft: 4, - marginRight: 4, + marginLeft: 12, + marginRight: 12, marginBottom: 16, }, addButton: { @@ -29,7 +29,10 @@ export const styles = StyleSheet.create({ marginRight: 4, }, input: { - margin: 8, + marginLeft: 16, + marginRight: 16, + marginTop: 8, + marginBottom: 8, padding: 8, borderRadius: 4, borderWidth: 1, @@ -49,5 +52,16 @@ export const styles = StyleSheet.create({ marginLeft: 8, marginRight: 8, }, + asset: { + width: 92, + height: 92, + marginRight: 8, + backgroundColor: 'yellow', + }, + carousel: { + paddingTop: 8, + paddingLeft: 16, + paddingRight: 16, + }, }) diff --git a/app/mobile/src/session/conversation/addTopic/useAddTopic.hook.js b/app/mobile/src/session/conversation/addTopic/useAddTopic.hook.js index df7b47c7..459559bf 100644 --- a/app/mobile/src/session/conversation/addTopic/useAddTopic.hook.js +++ b/app/mobile/src/session/conversation/addTopic/useAddTopic.hook.js @@ -1,12 +1,15 @@ -import { useState, useEffect, useContext } from 'react'; +import { useState, useRef, useEffect, useContext } from 'react'; import { ConversationContext } from 'context/ConversationContext'; +import { Image } from 'react-native'; export function useAddTopic(cardId, channelId) { const [state, setState] = useState({ message: null, + assets: [], }); + const assetId = useRef(0); const conversation = useContext(ConversationContext); const updateState = (value) => { @@ -17,6 +20,16 @@ export function useAddTopic(cardId, channelId) { setMessage: (message) => { updateState({ message }); }, + addImage: (data) => { + assetId.current++; + Image.getSize(data, (width, height) => { + const asset = { key: assetId.current, type: 'image', data: data, ratio: width/height }; + updateState({ assets: [ ...state.assets, asset ] }); + }); + }, + removeAsset: (key) => { + updateState({ assets: state.assets.filter(item => (item.key !== key))}); + }, }; return { state, actions };