mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00: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 React, { useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
@@ -32,12 +33,15 @@ function ProfilePrivacyComponent({
|
|||||||
}: ProfilePrivacyProps): JSX.Element {
|
}: ProfilePrivacyProps): JSX.Element {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [isExpanded, setIsExpanded] = useState(user.profileUI.isLocked);
|
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 [privacyValues, setPrivacyValues] = useState({ ...user.profileUI });
|
||||||
const [madeChanges, setMadeChanges] = useState(false);
|
const madeChanges = !isEqual(privacyValues, initialPrivacyValues);
|
||||||
|
|
||||||
function toggleFlag(flag: keyof ProfileUI): () => void {
|
function toggleFlag(flag: keyof ProfileUI): () => void {
|
||||||
return () => {
|
return () => {
|
||||||
setMadeChanges(true);
|
|
||||||
setPrivacyValues({ ...privacyValues, [flag]: !privacyValues[flag] });
|
setPrivacyValues({ ...privacyValues, [flag]: !privacyValues[flag] });
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -46,7 +50,7 @@ function ProfilePrivacyComponent({
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (!madeChanges) return;
|
if (!madeChanges) return;
|
||||||
submitProfileUI(privacyValues);
|
submitProfileUI(privacyValues);
|
||||||
setMadeChanges(false);
|
setInitialPrivacyValues({ ...privacyValues }); // reset baseline after saving
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user