2022-05-17 12:42:31 +00:00
|
|
|
import { useNavigation } from '@react-navigation/native';
|
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { FlatList, StyleSheet } from 'react-native';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
|
|
|
|
import { setUser } from '../../actions/login';
|
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
|
|
|
import * as List from '../../containers/List';
|
2022-08-19 21:14:37 +00:00
|
|
|
import { sendLoadingEvent } from '../../containers/Loading';
|
2022-05-17 12:42:31 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
|
|
|
import StatusIcon from '../../containers/Status/Status';
|
2022-06-27 18:46:59 +00:00
|
|
|
import { FormTextInput } from '../../containers/TextInput';
|
2022-05-17 12:42:31 +00:00
|
|
|
import { IApplicationState, TUserStatus } from '../../definitions';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { showToast } from '../../lib/methods/helpers/showToast';
|
|
|
|
import { Services } from '../../lib/services';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { showErrorAlert } from '../../lib/methods/helpers';
|
|
|
|
import log, { events, logEvent } from '../../lib/methods/helpers/log';
|
2022-05-17 12:42:31 +00:00
|
|
|
|
|
|
|
interface IStatus {
|
|
|
|
id: TUserStatus;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const STATUS: IStatus[] = [
|
|
|
|
{
|
|
|
|
id: 'online',
|
|
|
|
name: 'Online'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'busy',
|
|
|
|
name: 'Busy'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'away',
|
|
|
|
name: 'Away'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'offline',
|
|
|
|
name: 'Invisible'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
inputContainer: {
|
|
|
|
marginTop: 32,
|
|
|
|
marginBottom: 32
|
|
|
|
},
|
|
|
|
inputLeft: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 12,
|
|
|
|
left: 12
|
|
|
|
},
|
|
|
|
inputStyle: {
|
|
|
|
paddingLeft: 48
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-07-15 20:18:36 +00:00
|
|
|
const Status = ({
|
|
|
|
statusType,
|
|
|
|
status,
|
|
|
|
setStatus
|
|
|
|
}: {
|
|
|
|
statusType: IStatus;
|
|
|
|
status: TUserStatus;
|
|
|
|
setStatus: (status: TUserStatus) => void;
|
|
|
|
}) => {
|
|
|
|
const { id, name } = statusType;
|
2022-05-17 12:42:31 +00:00
|
|
|
return (
|
|
|
|
<List.Item
|
|
|
|
title={name}
|
2022-07-15 20:18:36 +00:00
|
|
|
onPress={() => {
|
|
|
|
const key = `STATUS_${statusType.id.toUpperCase()}` as keyof typeof events;
|
2022-05-17 12:42:31 +00:00
|
|
|
logEvent(events[key]);
|
2022-07-15 20:18:36 +00:00
|
|
|
if (status !== statusType.id) {
|
|
|
|
setStatus(statusType.id);
|
2022-05-17 12:42:31 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
testID={`status-view-${id}`}
|
2022-07-15 20:18:36 +00:00
|
|
|
left={() => <StatusIcon size={24} status={statusType.id} />}
|
2022-05-17 12:42:31 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const StatusView = (): React.ReactElement => {
|
|
|
|
const user = useSelector((state: IApplicationState) => getUserSelector(state));
|
|
|
|
const isMasterDetail = useSelector((state: IApplicationState) => state.app.isMasterDetail);
|
|
|
|
const Accounts_AllowInvisibleStatusOption = useSelector(
|
|
|
|
(state: IApplicationState) => state.settings.Accounts_AllowInvisibleStatusOption
|
|
|
|
);
|
|
|
|
|
|
|
|
const [statusText, setStatusText] = useState(user.statusText || '');
|
2022-07-15 20:18:36 +00:00
|
|
|
const [status, setStatus] = useState(user.status);
|
2022-05-17 12:42:31 +00:00
|
|
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { setOptions, goBack } = useNavigation();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const submit = async () => {
|
|
|
|
logEvent(events.STATUS_DONE);
|
2022-07-15 20:18:36 +00:00
|
|
|
if (statusText !== user.statusText || status !== user.status) {
|
|
|
|
await setCustomStatus(status, statusText);
|
2022-05-17 12:42:31 +00:00
|
|
|
}
|
|
|
|
goBack();
|
|
|
|
};
|
|
|
|
const setHeader = () => {
|
|
|
|
setOptions({
|
|
|
|
title: I18n.t('Edit_Status'),
|
|
|
|
headerLeft: isMasterDetail ? undefined : () => <HeaderButton.CancelModal onPress={goBack} />,
|
|
|
|
headerRight: () => (
|
|
|
|
<HeaderButton.Container>
|
2022-07-15 20:44:24 +00:00
|
|
|
<HeaderButton.Item title={I18n.t('Save')} onPress={submit} testID='status-view-submit' />
|
2022-05-17 12:42:31 +00:00
|
|
|
</HeaderButton.Container>
|
|
|
|
)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
setHeader();
|
2022-07-15 20:18:36 +00:00
|
|
|
}, [statusText, status]);
|
2022-05-17 12:42:31 +00:00
|
|
|
|
2022-07-15 20:18:36 +00:00
|
|
|
const setCustomStatus = async (status: TUserStatus, statusText: string) => {
|
2022-08-19 21:14:37 +00:00
|
|
|
sendLoadingEvent({ visible: true });
|
2022-05-17 12:42:31 +00:00
|
|
|
try {
|
2022-07-06 20:05:13 +00:00
|
|
|
await Services.setUserStatus(status, statusText);
|
2022-07-15 20:18:36 +00:00
|
|
|
dispatch(setUser({ statusText, status }));
|
2022-07-06 20:05:13 +00:00
|
|
|
logEvent(events.STATUS_CUSTOM);
|
|
|
|
showToast(I18n.t('Status_saved_successfully'));
|
2022-05-17 12:42:31 +00:00
|
|
|
} catch (e: any) {
|
|
|
|
const messageError =
|
2022-07-06 20:05:13 +00:00
|
|
|
e.error && e.error.includes('[error-too-many-requests]')
|
|
|
|
? I18n.t('error-too-many-requests', { seconds: e.reason.replace(/\D/g, '') })
|
|
|
|
: e.reason;
|
2022-05-17 12:42:31 +00:00
|
|
|
logEvent(events.STATUS_CUSTOM_F);
|
|
|
|
showErrorAlert(messageError);
|
2022-07-15 20:18:36 +00:00
|
|
|
log(e);
|
2022-05-17 12:42:31 +00:00
|
|
|
}
|
2022-08-19 21:14:37 +00:00
|
|
|
sendLoadingEvent({ visible: false });
|
2022-05-17 12:42:31 +00:00
|
|
|
};
|
|
|
|
|
2022-07-15 20:18:36 +00:00
|
|
|
const statusType = Accounts_AllowInvisibleStatusOption ? STATUS : STATUS.filter(s => s.id !== 'offline');
|
2022-05-17 12:42:31 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SafeAreaView testID='status-view'>
|
|
|
|
<FlatList
|
2022-07-15 20:18:36 +00:00
|
|
|
data={statusType}
|
2022-05-17 12:42:31 +00:00
|
|
|
keyExtractor={item => item.id}
|
2022-07-15 20:18:36 +00:00
|
|
|
renderItem={({ item }) => <Status statusType={item} status={status} setStatus={setStatus} />}
|
2022-05-17 12:42:31 +00:00
|
|
|
ListHeaderComponent={
|
|
|
|
<>
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2022-05-17 12:42:31 +00:00
|
|
|
value={statusText}
|
|
|
|
containerStyle={styles.inputContainer}
|
|
|
|
onChangeText={text => setStatusText(text)}
|
2022-07-15 20:18:36 +00:00
|
|
|
left={<StatusIcon testID={`status-view-current-${status}`} style={styles.inputLeft} status={status} size={24} />}
|
2022-05-17 12:42:31 +00:00
|
|
|
inputStyle={styles.inputStyle}
|
|
|
|
placeholder={I18n.t('What_are_you_doing_right_now')}
|
|
|
|
testID='status-view-input'
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
ItemSeparatorComponent={List.Separator}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default StatusView;
|