2022-09-22 21:31:23 +00:00
|
|
|
import React, { useEffect, useLayoutEffect, useState } from 'react';
|
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { useNavigation } from '@react-navigation/native';
|
2020-08-21 13:30:11 +00:00
|
|
|
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../../containers/List';
|
2020-08-21 13:30:11 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { ProfileStackParamList } from '../../stacks/types';
|
2022-09-22 21:31:23 +00:00
|
|
|
import { INotificationPreferences } from '../../definitions';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../../lib/services';
|
2022-09-22 21:31:23 +00:00
|
|
|
import { useAppSelector } from '../../lib/hooks';
|
|
|
|
import ListPicker from './ListPicker';
|
|
|
|
import log from '../../lib/methods/helpers/log';
|
2020-10-30 13:59:44 +00:00
|
|
|
|
2022-09-22 21:31:23 +00:00
|
|
|
const UserNotificationPreferencesView = () => {
|
|
|
|
const [preferences, setPreferences] = useState({} as INotificationPreferences);
|
2022-12-21 16:15:30 +00:00
|
|
|
const [loading, setLoading] = useState(true);
|
2020-08-21 13:30:11 +00:00
|
|
|
|
2022-09-22 21:31:23 +00:00
|
|
|
const navigation = useNavigation<StackNavigationProp<ProfileStackParamList, 'UserNotificationPrefView'>>();
|
|
|
|
const userId = useAppSelector(state => getUserSelector(state).id);
|
2021-11-16 16:16:29 +00:00
|
|
|
|
2022-09-22 21:31:23 +00:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Notification_Preferences')
|
2020-08-21 13:30:11 +00:00
|
|
|
});
|
2022-09-22 21:31:23 +00:00
|
|
|
}, [navigation]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
async function getPreferences() {
|
|
|
|
try {
|
|
|
|
const result = await Services.getUserPreferences(userId);
|
|
|
|
if (result.success) {
|
2022-12-21 16:15:30 +00:00
|
|
|
setLoading(false);
|
2022-09-22 21:31:23 +00:00
|
|
|
setPreferences(result.preferences);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-12-21 16:15:30 +00:00
|
|
|
setLoading(false);
|
2022-09-22 21:31:23 +00:00
|
|
|
log(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getPreferences();
|
|
|
|
}, [userId]);
|
|
|
|
|
2023-07-19 19:47:01 +00:00
|
|
|
const onValueChangePicker = async (param: { [key: string]: string }) => {
|
|
|
|
const previousPreferences = preferences;
|
2022-09-22 21:31:23 +00:00
|
|
|
try {
|
2023-07-19 19:47:01 +00:00
|
|
|
setPreferences({ ...previousPreferences, ...param });
|
2022-09-22 21:31:23 +00:00
|
|
|
const result = await Services.setUserPreferences(userId, param);
|
2023-07-19 19:47:01 +00:00
|
|
|
if (!result.success) {
|
|
|
|
setPreferences(previousPreferences);
|
2022-09-22 21:31:23 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-07-19 19:47:01 +00:00
|
|
|
setPreferences(previousPreferences);
|
2022-09-22 21:31:23 +00:00
|
|
|
log(error);
|
2022-03-03 01:53:44 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-08-21 13:30:11 +00:00
|
|
|
|
2022-09-22 21:31:23 +00:00
|
|
|
return (
|
|
|
|
<SafeAreaView testID='user-notification-preference-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<List.Container>
|
|
|
|
{loading ? (
|
2022-12-21 16:15:30 +00:00
|
|
|
<ActivityIndicator />
|
|
|
|
) : (
|
2022-09-22 21:31:23 +00:00
|
|
|
<>
|
|
|
|
<List.Section title='Desktop_Notifications'>
|
|
|
|
<List.Separator />
|
|
|
|
<ListPicker
|
|
|
|
onChangeValue={onValueChangePicker}
|
|
|
|
preference={'desktopNotifications'}
|
|
|
|
title='Alert'
|
|
|
|
testID='user-notification-preference-view-alert'
|
|
|
|
value={preferences.desktopNotifications}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='Desktop_Alert_info' />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section title='Push_Notifications'>
|
|
|
|
<List.Separator />
|
|
|
|
<ListPicker
|
|
|
|
onChangeValue={onValueChangePicker}
|
|
|
|
preference={'pushNotifications'}
|
|
|
|
title='Alert'
|
|
|
|
testID='user-notification-preference-view-push-notification'
|
|
|
|
value={preferences.pushNotifications}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='Push_Notifications_Alert_Info' />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section title='Email'>
|
|
|
|
<List.Separator />
|
|
|
|
<ListPicker
|
|
|
|
onChangeValue={onValueChangePicker}
|
|
|
|
preference={'emailNotificationMode'}
|
|
|
|
title='Alert'
|
|
|
|
testID='user-notification-preference-view-email-alert'
|
|
|
|
value={preferences.emailNotificationMode}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='You_need_to_verifiy_your_email_address_to_get_notications' />
|
|
|
|
</List.Section>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</List.Container>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default UserNotificationPreferencesView;
|