import { RouteProp, useNavigation, useRoute } from '@react-navigation/core';
import React, { useEffect, useState } from 'react';
import { Switch, Text } from 'react-native';
import { TActionSheetOptionsItem, useActionSheet } from '../../containers/ActionSheet';
import { CustomIcon } from '../../containers/CustomIcon';
import * as List from '../../containers/List';
import SafeAreaView from '../../containers/SafeAreaView';
import StatusBar from '../../containers/StatusBar';
import { IRoomNotifications, TRoomNotificationsModel } from '../../definitions';
import I18n from '../../i18n';
import { SWITCH_TRACK_COLOR } from '../../lib/constants';
import { useAppSelector } from '../../lib/hooks';
import { showErrorAlertWithEMessage } from '../../lib/methods/helpers';
import { compareServerVersion } from '../../lib/methods/helpers/compareServerVersion';
import log, { events, logEvent } from '../../lib/methods/helpers/log';
import { Services } from '../../lib/services';
import { ChatsStackParamList } from '../../stacks/types';
import { useTheme } from '../../theme';
import sharedStyles from '../Styles';
import { OPTIONS } from './options';
type TOptions = keyof typeof OPTIONS;
type TRoomNotifications = keyof IRoomNotifications;
type TUnionOptionsRoomNotifications = TOptions | TRoomNotifications;
interface IBaseParams {
preference: TUnionOptionsRoomNotifications;
room: TRoomNotificationsModel;
onChangeValue: (pref: TUnionOptionsRoomNotifications, param: { [key: string]: string }, onError: () => void) => void;
}
const RenderListPicker = ({
preference,
room,
title,
testID,
onChangeValue
}: {
title: string;
testID: string;
} & IBaseParams) => {
const { showActionSheet, hideActionSheet } = useActionSheet();
const { colors } = useTheme();
const pref = room[preference]
? OPTIONS[preference as TOptions].find(option => option.value === room[preference])
: OPTIONS[preference as TOptions][0];
const [option, setOption] = useState(pref);
const options: TActionSheetOptionsItem[] = OPTIONS[preference as TOptions].map(i => ({
title: I18n.t(i.label, { defaultValue: i.label, second: i.second }),
onPress: () => {
hideActionSheet();
onChangeValue(preference, { [preference]: i.value.toString() }, () => setOption(option));
setOption(i);
},
right: option?.value === i.value ? () => : undefined
}));
return (
showActionSheet({ options })}
right={() => (
{option?.label ? I18n.t(option?.label, { defaultValue: option?.label, second: option?.second }) : option?.label}
)}
/>
);
};
const RenderSwitch = ({ preference, room, onChangeValue }: IBaseParams) => {
const [switchValue, setSwitchValue] = useState(!room[preference]);
return (
{
onChangeValue(preference, { [preference]: switchValue ? '1' : '0' }, () => setSwitchValue(switchValue));
setSwitchValue(value);
}}
/>
);
};
const NotificationPreferencesView = (): React.ReactElement => {
const route = useRoute>();
const { rid, room } = route.params;
const navigation = useNavigation();
const serverVersion = useAppSelector(state => state.server.version);
const [hideUnreadStatus, setHideUnreadStatus] = useState(room.hideUnreadStatus);
useEffect(() => {
navigation.setOptions({
title: I18n.t('Notification_Preferences')
});
}, []);
useEffect(() => {
const observe = room.observe();
observe.subscribe(data => {
setHideUnreadStatus(data.hideUnreadStatus);
});
}, []);
const saveNotificationSettings = async (key: TUnionOptionsRoomNotifications, params: IRoomNotifications, onError: Function) => {
try {
// @ts-ignore
logEvent(events[`NP_${key.toUpperCase()}`]);
await Services.saveNotificationSettings(rid, params);
} catch (e) {
// @ts-ignore
logEvent(events[`NP_${key.toUpperCase()}_F`]);
log(e);
onError();
showErrorAlertWithEMessage(e);
}
};
return (
}
/>
}
/>
}
/>
{hideUnreadStatus && compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '4.8.0') ? (
}
/>
) : null}
);
};
export default NotificationPreferencesView;