import React, { Fragment, useState, useEffect } from 'react'; import { StyleSheet, View, ScrollView, Alert, BackHandler } from 'react-native'; import { TouchableWithoutFeedback } from 'react-native-gesture-handler'; import { Surface, Text, TextInput, IconButton, Colors, Divider, ActivityIndicator } from 'react-native-paper'; import { heightPercentageToDP as hp } from 'react-native-responsive-screen'; const Home = () => { const [ weight, setWeight ] = useState(''); const [ selectedShape, setSelectedShape ] = useState(null); const [ selectedDiscount, setSelectedDiscount ] = useState(null); const [ selectedColor, setSelectedColor ] = useState(''); const [ selectedClarity, setSelectedClarity ] = useState(''); const [ roundPrices, setRoundPrices ] = useState([]); const [ pearPrices, setPearPrices ] = useState([]); const [ price, setPrice ] = useState(0); const [ totalPrice, setTotalPrice ] = useState(0); const shapes = [ { shape: 'round', letter: 'R' }, { shape: 'pear', letter: 'T' }, { shape: 'pear', letter: 'P' }, { shape: 'pear', letter: 'C' }, { shape: 'pear', letter: 'E' }, { shape: 'pear', letter: 'O' }, { shape: 'pear', letter: 'PS' }, { shape: 'pear', letter: 'H' }, ]; const discounts = [-100, -90, -80, -70, -60, -50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]; const colors = [ { color: 'd', letter: 'D' }, { color: 'e', letter: 'E' }, { color: 'f', letter: 'F' }, { color: 'g', letter: 'G' }, { color: 'i', letter: 'I' }, { color: 'j', letter: 'J' }, { color: 'k', letter: 'K' }, { color: 'l', letter: 'L' }, { color: 'm', letter: 'M' }, { color: 'n', letter: 'N' } ]; const clarities = [ { clarity: 'if', letter: 'IF' }, { clarity: 'vvs1', letter: 'VVS1' }, { clarity: 'vvs2', letter: 'VVS2' }, { clarity: 'vs1', letter: 'VS1' }, { clarity: 'vs2', letter: 'VS2' }, { clarity: 'si1', letter: 'SI1' }, { clarity: 'si2', letter: 'SI2' }, { clarity: 'si3', letter: 'SI3' }, { clarity: 'i1', letter: 'I1' }, { clarity: 'i2', letter: 'I2' }, { clarity: 'i3', letter: 'I3' } ]; useEffect(() => { (async() => { try { let roundPrices = await fetch('https://technet.rapaport.com/HTTP/JSON/Prices/GetPriceSheet.aspx', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: '%7B%0A%22request%22%3A%20%7B%0A%22header%22%3A%20%7B%0A%22username%22%3A%20%22a2a1ritnqs9fmldmexwlrbediodvct%22%2C%0A%22password%22%3A%20%22RKRBnVKK%22%0A%0A%7D%2C%0A%22body%22%3A%7B%0A%22shape%22%3A%20%22round%22%0A%7D%0A%7D%0A%7D' }); if (!roundPrices.ok && roundPrices.response.header.error_code !== 0) throw new Error(); roundPrices = await roundPrices.json(); let pearPrices = await fetch('https://technet.rapaport.com/HTTP/JSON/Prices/GetPriceSheet.aspx', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: '%7B%0A%22request%22%3A%20%7B%0A%22header%22%3A%20%7B%0A%22username%22%3A%20%22a2a1ritnqs9fmldmexwlrbediodvct%22%2C%0A%22password%22%3A%20%22RKRBnVKK%22%0A%0A%7D%2C%0A%22body%22%3A%7B%0A%22shape%22%3A%20%22pear%22%0A%7D%0A%7D%0A%7D' }); if (!pearPrices.ok && pearPrices.response.header.error_code !== 0) throw new Error(); pearPrices = await pearPrices.json(); setRoundPrices(roundPrices.response.body.price); setPearPrices(pearPrices.response.body.price); } catch(err) { Alert.alert('Could not get diamond prices', 'Check your internet connection and try again later', [{ text: 'Ok', onPress: () => BackHandler.exitApp() }]); } })(); }, []); useEffect(() => { let diamonds = selectedShape ? selectedShape.shape === 'round' ? roundPrices : pearPrices : []; if (selectedColor) diamonds = diamonds.filter(diamond => diamond.color === selectedColor); if (selectedClarity) diamonds = diamonds.filter(diamond => diamond.clarity === selectedClarity); if (weight) { if (weight > 10.99) { let price = 0; let filteredDiamonds = []; let weights = new Array(Math.floor(weight / 10)).fill(10); weights.push(weight % 10); weights.forEach(partialWeight => { filteredDiamonds = diamonds.filter(diamond => Number(partialWeight) >= diamond.low_size && Number(partialWeight) <= diamond.high_size); price += filteredDiamonds[0] ? filteredDiamonds[0].caratprice : 0; }); setPrice(price); } else { let filteredDiamonds = []; filteredDiamonds = diamonds.filter(diamond => Number(weight) >= diamond.low_size && Number(weight) <= diamond.high_size); setPrice(filteredDiamonds[0] ? filteredDiamonds[0].caratprice : 0); } } else setPrice(diamonds.length > 0 ? diamonds[0].caratprice : 0); }, [selectedShape, selectedColor, selectedClarity, weight]); useEffect(() => { setTotalPrice(price * weight + (price * weight * selectedDiscount / 100)); }, [price, weight, selectedDiscount]); return roundPrices.length > 0 && pearPrices.length > 0 ? ( { shapes.map(({ shape, letter }, index) => ( setSelectedShape({ shape, letter })}> {letter} { index !== shapes.length - 1 && } )) } { discounts.map((discount, index) => ( setSelectedDiscount(discount)}> {discount <= 0 ? discount : `+${discount}`}% { index !== discounts.length - 1 && } )) } { colors.map(({ color, letter }, index) => ( setSelectedColor(color)}> {letter} { index !== colors.length - 1 && } )) } { clarities.map(({ clarity, letter }, index) => ( setSelectedClarity(clarity)}> {letter} { index !== clarities.length - 1 && } )) } { [1, 2, 3].map((number, index) => ( setWeight(weight + number)}> {number} { index !== [1, 2, 3].length - 1 && } )) } { [4, 5, 6].map((number, index) => ( setWeight(weight + number)}> {number} { index !== [4, 5, 6].length - 1 && } )) } { [7, 8, 9].map((number, index) => ( setWeight(weight + number)}> {number} { index !== [7, 8, 9].length - 1 && } )) } { if (weight.indexOf('.') === -1 && weight !== '') setWeight(weight + '.') }}> . setWeight(weight + '0')}> 0 setWeight('')} onPress={() => setWeight(weight.substring(0, weight.length - 1))}> ) : ; }; const styles = StyleSheet.create({ container: { paddingHorizontal: 15, paddingVertical: 10, flex: 1, flexDirection: 'column' }, grid: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }, separator: { width: 1, height: '50%', backgroundColor: 'rgba(0, 0, 0, 0.2)' }, divider: { height: 1, backgroundColor: 'rgba(0, 0, 0, 0.2)' }, surface: { marginVertical: hp(2), paddingHorizontal: 5, height: 50, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', overflow: 'hidden' }, discountSurface: { width: '25%', height: 45, paddingVertical: 5, flexDirection: 'column', justifyContent: 'center', alignContent: 'space-around', overflow: 'hidden' }, innerSurface: { elevation: 1, paddingVertical: 5, paddingHorizontal: 10, marginHorizontal: 5, width: 'auto' }, keyboard: { marginVertical: 5, padding: 5 }, keyboardRow: { margin: 5, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-around' }, key: { width: 35, height: 37, flexDirection: 'row', justifyContent: 'center', alignItems: 'center' } }); export default Home;