import React, { useCallback, useState } from 'react'; import { StyleSheet, ViewStyle, TextStyle, TouchableHighlight, View, } from 'react-native'; import { useStyles, useTheme } from '../../hooks'; import { ComponentStyleProp, LLComponentStyleFn } from '../../types'; import { LLText } from '../LLText'; export type LLLoadActionButtonStyles = { buttonContainer: ViewStyle; button: ViewStyle; disabledbutton: ViewStyle; buttonText: TextStyle; disabledButtonText: TextStyle; }; export type LLLoadActionButtonProps = ComponentStyleProp & { widgetId: string; }; export type LLLoadActionButtonComponentProps = ComponentStyleProp & { label?: string; actionInProgressLabel?: string; onPress: () => Promise; }; export function LLLoadActionButtonComponent({ onPress, label, actionInProgressLabel, styles: stylesProp, }: LLLoadActionButtonComponentProps) { const loadMoreStyles = useStyles({ componentStylesFn: getLoadMoreButtonStyles, stylesProp, }); const { theme } = useTheme(); const [isLoading, setIsLoading] = useState(false); const onLoad = useCallback(() => { setIsLoading(true); onPress().finally(() => { setIsLoading(false); }); }, [setIsLoading, onPress]); return ( {isLoading ? actionInProgressLabel : label} ); } const getLoadMoreButtonStyles: LLComponentStyleFn = ({ theme, }) => StyleSheet.create({ buttonContainer: { display: 'flex', justifyContent: 'center', width: '100%', alignItems: 'center', }, button: { display: 'flex', justifyContent: 'center', alignItems: 'center', width: 190, height: 40, borderRadius: 8, marginBottom: 16, backgroundColor: theme.primaryButtonBackground, }, disabledbutton: { backgroundColor: theme.disabledButtonBackground, width: 210, }, buttonText: { fontSize: 14, color: theme.text, }, disabledButtonText: { color: theme.disabledButtonText, }, });