import React, { useState } from 'react'; import { TouchableOpacity, View, StyleSheet } from 'react-native'; import CountryPicker, { Flag } from 'react-native-country-picker-modal'; import { useTheme } from 'styled-components/native'; import { LanguageSelector as LanguageSelectorController, useLanguage, } from 'ordering-components/native'; import { Container, LanguageItem } from './styles'; import langCountries from './lang_country.json'; import { OText, OIcon } from '../shared'; import { LanguageSelectorParams } from '../../types'; const LanguageSelectorUI = (props: LanguageSelectorParams) => { const { languagesState, currentLanguage, handleChangeLanguage } = props; const [, t] = useLanguage(); const theme = useTheme(); const [isCountryModalVisible, setCountryModalVisible] = useState(false); const _languages = languagesState?.languages?.map((language: any) => { return { key: language?.code, value: language?.code, label: language?.name, inputLabel: language?.code.toUpperCase(), countryCode: langCountries.find(item => item.value == language?.code)?.countryCode, }; }); _languages && _languages?.sort((a: any, b: any) => a.content > b.content ? 1 : b.content > a.content ? -1 : 0, ); const countryCodes = _languages?.map((item: any) => item.countryCode); const currentLanguageData = _languages?.find( (item: any) => item.value == currentLanguage, ); const styles = StyleSheet.create({ text: { fontFamily: 'Poppins', fontStyle: 'normal', fontWeight: 'normal', fontSize: 16, }, }); return !languagesState?.loading && languagesState?.languages?.length > 1 && ( {languagesState?.languages && ( setCountryModalVisible(false)} withCountryNameButton countryCodes={countryCodes} //@ts-ignore renderFlagButton={() => ( setCountryModalVisible(true)}> {currentLanguageData?.label} )} flatListProps={{ keyExtractor: (item: any) => item.value, data: _languages || [], renderItem: ({ item }: any) => ( { handleChangeLanguage(item.value); setCountryModalVisible(false); }} activeOpacity={1}> {t(item.code, item.label)} ), }} /> )} ); }; export const LanguageSelector = (props: LanguageSelectorParams) => { const LanguageProps = { ...props, UIComponent: LanguageSelectorUI, }; return ; };