Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Convert AccountPreferencesPage to ts #26096

Merged
merged 5 commits into from
Jul 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ButtonGroup, Button, Box, Accordion } from '@rocket.chat/fuselage';
import { useToastMessageDispatch, useSetting, useMethod, useTranslation } from '@rocket.chat/ui-contexts';
import React, { useState, useCallback, useRef } from 'react';
import React, { useState, useCallback, useRef, ReactElement } from 'react';

import Page from '../../../components/Page';
import PreferencesGlobalSection from './PreferencesGlobalSection';
Expand All @@ -12,24 +12,63 @@ import PreferencesNotificationsSection from './PreferencesNotificationsSection';
import PreferencesSoundSection from './PreferencesSoundSection';
import PreferencesUserPresenceSection from './PreferencesUserPresenceSection';

const AccountPreferencesPage = () => {
type CurrentData = {
enableNewMessageTemplate: boolean;
language: string;
newRoomNotification: string;
newMessageNotification: string;
clockMode: number;
useEmojis: boolean;
convertAsciiEmoji: boolean;
saveMobileBandwidth: boolean;
collapseMediaByDefault: boolean;
autoImageLoad: boolean;
emailNotificationMode: string;
unreadAlert: boolean;
notificationsSoundVolume: number;
desktopNotifications: string;
pushNotifications: string;
enableAutoAway: boolean;
highlights: string;
messageViewMode: number;
hideUsernames: boolean;
hideRoles: boolean;
displayAvatars: boolean;
hideFlexTab: boolean;
sendOnEnter: string;
idleTimeLimit: number;
sidebarShowFavorites: boolean;
sidebarShowUnread: boolean;
sidebarSortby: string;
sidebarViewMode: string;
sidebarDisplayAvatar: boolean;
sidebarGroupByType: boolean;
muteFocusedConversations: boolean;
dontAskAgainList: [action: string, label: string][];
};

type FormatedData = Omit<Partial<CurrentData>, 'dontAskAgainList' | 'highlights'>;

const AccountPreferencesPage = (): ReactElement => {
const t = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();

const [hasAnyChange, setHasAnyChange] = useState(false);

const saveData = useRef({});
const saveData = useRef<Partial<CurrentData>>({});
const commitRef = useRef({});

const dataDownloadEnabled = useSetting('UserData_EnableDownload');

const onChange = useCallback(
({ initialValue, value, key }) => {
const { current } = saveData;
if (JSON.stringify(initialValue) !== JSON.stringify(value)) {
current[key] = value;
} else {
delete current[key];
if (current) {
if (JSON.stringify(initialValue) !== JSON.stringify(value)) {
current[key as keyof CurrentData] = value;
} else {
delete current[key as keyof CurrentData];
}
}

const anyChange = !!Object.values(current).length;
Expand All @@ -45,7 +84,7 @@ const AccountPreferencesPage = () => {
const handleSave = useCallback(async () => {
try {
const { current: data } = saveData;
if (data.highlights || data.highlights === '') {
if (data?.highlights || data?.highlights === '') {
Object.assign(data, {
highlights: data.highlights
.split(/,|\n/)
Expand All @@ -54,22 +93,22 @@ const AccountPreferencesPage = () => {
});
}

if (data.dontAskAgainList) {
if (data?.dontAskAgainList) {
const list =
Array.isArray(data.dontAskAgainList) && data.dontAskAgainList.length > 0
? data.dontAskAgainList.map(([action, label]) => ({ action, label }))
: [];
Object.assign(data, { dontAskAgainList: list });
}

await saveFn(data);
await saveFn(data as FormatedData);
saveData.current = {};
setHasAnyChange(false);
Object.values(commitRef.current).forEach((fn) => fn());
Object.values(commitRef.current).forEach((fn) => (fn as () => void)());

dispatchToastMessage({ type: 'success', message: t('Preferences_saved') });
} catch (e) {
dispatchToastMessage({ type: 'error', message: e });
dispatchToastMessage({ type: 'error', message: String(e) });
}
}, [dispatchToastMessage, saveFn, t]);

Expand Down