mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-14 01:00:36 -04:00
fix(client): privacy save button state (#67230)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import React, { useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { connect } from 'react-redux';
|
||||
@@ -32,12 +33,15 @@ function ProfilePrivacyComponent({
|
||||
}: ProfilePrivacyProps): JSX.Element {
|
||||
const { t } = useTranslation();
|
||||
const [isExpanded, setIsExpanded] = useState(user.profileUI.isLocked);
|
||||
// Snapshot of the original values to detect unsaved changes
|
||||
const [initialPrivacyValues, setInitialPrivacyValues] = useState({
|
||||
...user.profileUI
|
||||
});
|
||||
const [privacyValues, setPrivacyValues] = useState({ ...user.profileUI });
|
||||
const [madeChanges, setMadeChanges] = useState(false);
|
||||
const madeChanges = !isEqual(privacyValues, initialPrivacyValues);
|
||||
|
||||
function toggleFlag(flag: keyof ProfileUI): () => void {
|
||||
return () => {
|
||||
setMadeChanges(true);
|
||||
setPrivacyValues({ ...privacyValues, [flag]: !privacyValues[flag] });
|
||||
};
|
||||
}
|
||||
@@ -46,7 +50,7 @@ function ProfilePrivacyComponent({
|
||||
e.preventDefault();
|
||||
if (!madeChanges) return;
|
||||
submitProfileUI(privacyValues);
|
||||
setMadeChanges(false);
|
||||
setInitialPrivacyValues({ ...privacyValues }); // reset baseline after saving
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user