2021-09-13 20:41:05 +00:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
|
|
import { ActivityIndicator, StyleSheet, Text } from 'react-native';
|
2021-05-26 17:24:54 +00:00
|
|
|
|
2022-04-07 14:10:03 +00:00
|
|
|
import { MessageTypeLoad, themes } from '../../../lib/constants';
|
2022-03-03 20:25:03 +00:00
|
|
|
import { MessageType } from '../../../definitions';
|
2021-05-26 17:24:54 +00:00
|
|
|
import { useTheme } from '../../../theme';
|
2022-06-06 14:17:51 +00:00
|
|
|
import Touch from '../../../lib/methods/helpers/touch';
|
2021-05-26 17:24:54 +00:00
|
|
|
import sharedStyles from '../../Styles';
|
|
|
|
import I18n from '../../../i18n';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
button: {
|
|
|
|
paddingVertical: 16,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-03 20:25:03 +00:00
|
|
|
const LoadMore = ({
|
|
|
|
load,
|
|
|
|
type,
|
|
|
|
runOnRender
|
|
|
|
}: {
|
|
|
|
load: Function;
|
|
|
|
type: MessageType;
|
|
|
|
runOnRender: boolean;
|
|
|
|
}): React.ReactElement => {
|
2021-05-26 17:24:54 +00:00
|
|
|
const { theme } = useTheme();
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const handleLoad = useCallback(async () => {
|
2021-05-26 17:24:54 +00:00
|
|
|
try {
|
|
|
|
if (loading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setLoading(true);
|
|
|
|
await load();
|
|
|
|
} finally {
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
}, [loading]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (runOnRender) {
|
|
|
|
handleLoad();
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
let text = 'Load_More';
|
2022-03-03 20:25:03 +00:00
|
|
|
if (type === MessageTypeLoad.NEXT_CHUNK) {
|
2021-05-26 17:24:54 +00:00
|
|
|
text = 'Load_Newer';
|
|
|
|
}
|
2022-03-03 20:25:03 +00:00
|
|
|
if (type === MessageTypeLoad.PREVIOUS_CHUNK) {
|
2021-05-26 17:24:54 +00:00
|
|
|
text = 'Load_Older';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-09-13 20:41:05 +00:00
|
|
|
<Touch onPress={handleLoad} style={styles.button} theme={theme} enabled={!loading}>
|
|
|
|
{loading ? (
|
|
|
|
<ActivityIndicator color={themes[theme].auxiliaryText} />
|
|
|
|
) : (
|
|
|
|
<Text style={[styles.text, { color: themes[theme].titleText }]}>{I18n.t(text)}</Text>
|
|
|
|
)}
|
2021-05-26 17:24:54 +00:00
|
|
|
</Touch>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default LoadMore;
|