import React, { useState, useEffect } from 'react' import { OrderReview as ReviewOrderController, useLanguage, ToastType, useToast } from 'ordering-components/native' import { useForm, Controller } from 'react-hook-form' import { ReviewOrderContainer, FormReviews, Category, Stars } from './styles' import { OButton, OIcon, OInput, OText } from '../shared' import { TouchableOpacity, StyleSheet, View, TextStyle } from 'react-native'; import { useTheme } from 'styled-components/native'; import NavBar from '../NavBar' import Spinner from 'react-native-loading-spinner-overlay' import { ReviewOrderParams } from '../../types' import moment from 'moment' export const ReviewOrderUI = (props: ReviewOrderParams) => { const { order, stars, handleChangeInput, handleChangeRating, handleSendReview, formState, navigation } = props const theme = useTheme(); const styles = StyleSheet.create({ inputTextArea: { borderColor: theme.colors.clear, borderRadius: 7.6, marginVertical: 20, height: 100, alignItems: 'flex-start' } }) const [, t] = useLanguage() const [, { showToast }] = useToast() const { handleSubmit, control, errors } = useForm() const [alertState, setAlertState] = useState<{ open: boolean, content: Array, success?: boolean }>({ open: false, content: [], success: false }) const categories = { quality: { name: 'quality', show: t('QUALITY', 'Quality of Product') }, punctuality: { name: 'punctiality', show: t('PUNCTUALITY', 'Punctuality') }, service: { name: 'service', show: t('SERVICE', 'Service') }, packaging: { name: 'packaging', show: t('PRODUCT_PACKAGING', 'Product Packaging') } } const onSubmit = () => { if (Object.values(stars).some(value => value === 0)) { setAlertState({ open: true, content: Object.values(categories).map((category, i) => stars[category.name] === 0 ? `- ${t('CATEGORY_ATLEAST_ONE', `${category.show} must be at least one point`).replace('CATEGORY', category.name.toUpperCase())} ${i !== 3 && '\n'}` : ' ') }) return } handleSendReview() setAlertState({ ...alertState, success: true }) } const getStarArr = (rating: number) => { switch (rating) { case 0: return [0, 0, 0, 0, 0]; case 1: return [1, 0, 0, 0, 0]; case 2: return [1, 1, 0, 0, 0]; case 3: return [1, 1, 1, 0, 0]; case 4: return [1, 1, 1, 1, 0]; case 5: return [1, 1, 1, 1, 1]; default: return [0, 0, 0, 0, 0]; } } useEffect(() => { if (formState.error && !formState?.loading) { showToast(ToastType.Error, formState.result) } if (!formState.loading && !formState.error && alertState.success) { showToast(ToastType.Success, t('REVIEW_SUCCESS_CONTENT', 'Thank you, Review successfully submitted!')) navigation?.canGoBack() && navigation.goBack() } }, [formState.result]) useEffect(() => { if (Object.keys(errors).length > 0) { // Convert all errors in one string to show in toast provider const list = Object.values(errors); let stringError = ''; list.map((item: any, i: number) => { stringError += i + 1 === list.length ? `- ${item.message}` : `- ${item.message}\n`; }); showToast(ToastType.Error, stringError); } }, [errors]); useEffect(() => { if (alertState.open) { alertState.content && showToast( ToastType.Error, alertState.content ) } }, [alertState.content]) const getStar = (star: number, index: number, category: string) => { switch (star) { case 0: return ( handleChangeRating({ target: { name: category, value: index + 1 } })}> ) case 1: return ( handleChangeRating({ target: { name: category, value: index + 1 } })}> ) } } return ( <> {moment().format('MMM D, yyyy')}} titleAlign={'center'} onActionLeft={() => navigation?.canGoBack() && navigation.goBack()} rightImg={null} btnStyle={{ paddingLeft: 0 }} paddingTop={0} /> {/* */} {Object.values(categories).map(category => ( {category.show} {getStarArr(stars[category?.name]).map((star, index) => getStar(star, index, category.name))} ))} ( { onChange(val) handleChangeInput(val) }} style={styles.inputTextArea} multiline bgColor={theme.colors.inputDisabled} /> )} /> ) } export const ReviewOrder = (props: ReviewOrderParams) => { const reviewOrderProps = { ...props, UIComponent: ReviewOrderUI } return }