From dc6d3f64b34370074aaaf66e398cae65bf95d8ee Mon Sep 17 00:00:00 2001 From: analuizaff Date: Tue, 9 Mar 2021 13:57:50 -0300 Subject: [PATCH] restricting amount of images --- src/app/components/forms/FormImagePicker.js | 35 +++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/app/components/forms/FormImagePicker.js b/src/app/components/forms/FormImagePicker.js index 483006e..58f66bf 100644 --- a/src/app/components/forms/FormImagePicker.js +++ b/src/app/components/forms/FormImagePicker.js @@ -1,20 +1,41 @@ -import React from "react"; +import React, { useState } from "react"; import { useFormikContext } from "formik"; import ErrorMessage from "./ErrorMessage"; import ImageInputList from "../ImageInputList"; -import { View, Text, StyleSheet } from "react-native"; +import { View, Text, StyleSheet, Modal, Alert } from "react-native"; import colors from "../../config/colors"; import { dimensions } from "../../config/dimensions"; function FormImagePicker({ name }) { const { errors, setFieldValue, touched, values } = useFormikContext(); const imageUris = values[name]; + const [modalVisible, setModalVisible] = useState(false); const handleAdd = (uri) => { - setFieldValue(name, [...imageUris, uri]); + if (imageUris.length === 0) { + setFieldValue(name, [uri]); + } else { + createTwoButtonAlert(uri); + } }; + const createTwoButtonAlert = (uri) => + Alert.alert( + "Substituir imagem?", + "É possível enviar apenas uma imagem", + [ + { + text: "Cancel", + onPress: () => console.log("Cancel Pressed"), + style: "cancel" + }, + { text: "OK", onPress: () => setFieldValue(name, [uri])} + ], + { cancelable: false } + ); + + const handleRemove = (uri) => { setFieldValue( name, @@ -43,6 +64,14 @@ const styles = StyleSheet.create({ marginBottom: 5, marginTop: 15, }, + centeredView: { + flex: 1, + justifyContent: "center", + alignItems: "center", + marginTop: 22, + }, }); + + export default FormImagePicker;