Skip to content

Commit

Permalink
update: add changes tracking and disable button logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nirbhayel committed Dec 2, 2024
1 parent df444a8 commit ac4f5b3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
11 changes: 9 additions & 2 deletions modules/settings/assets/js/components/bottom-bar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { useSettings, useStorage, useToastNotification } from '@ea11y/hooks';
import { __ } from '@wordpress/i18n';

const BottomBar = () => {
const { selectedMenu, iconDesign, iconPosition } = useSettings();
const { selectedMenu, iconDesign, iconPosition, hasChanges, setHasChanges } =
useSettings();
const { save } = useStorage();
const { success, error } = useToastNotification();

Expand All @@ -21,6 +22,7 @@ const BottomBar = () => {
},
}).then(() => {
success('Settings saved!');
setHasChanges(false);
});
} catch (e) {
error('Failed to save settings!');
Expand All @@ -36,7 +38,12 @@ const BottomBar = () => {
width="100%"
borderTop="1px solid rgba(0, 0, 0, 0.12)"
>
<Button variant="contained" color="info" onClick={saveSettings}>
<Button
variant="contained"
color="info"
onClick={saveSettings}
disabled={!hasChanges}
>
{__('Save Changes', 'pojo-accessibility')}
</Button>
</Box>
Expand Down
4 changes: 3 additions & 1 deletion modules/settings/assets/js/hooks/use-icon-design.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { useSettings } from '@ea11y/hooks';

export const useIconDesign = () => {
const { iconDesign, setIconDesign } = useSettings();
const { iconDesign, setIconDesign, setHasChanges } = useSettings();

const updateIconDesign = (newValues) => {
setIconDesign((prev) => ({
...prev,
...newValues,
}));

setHasChanges(true);
};

return { iconDesign, updateIconDesign };
Expand Down
4 changes: 3 additions & 1 deletion modules/settings/assets/js/hooks/use-icon-positioon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useSettings } from '@ea11y/hooks';
import { useCallback } from '@wordpress/element';

export const useIconPosition = () => {
const { iconPosition, setIconPosition } = useSettings();
const { iconPosition, setIconPosition, setHasChanges } = useSettings();

const updateIconPosition = useCallback(
(device, key, value) => {
Expand All @@ -13,6 +13,7 @@ export const useIconPosition = () => {
[key]: value,
},
}));
setHasChanges(true);
},
[setIconPosition],
);
Expand All @@ -32,6 +33,7 @@ export const useIconPosition = () => {
},
},
}));
setHasChanges(true);
};

return { iconPosition, updateIconPosition, updateExactPosition };
Expand Down
4 changes: 4 additions & 0 deletions modules/settings/assets/js/hooks/use-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export const SettingsProvider = ({ children }) => {
position: 'top-left',
},
});

const [hasChanges, setHasChanges] = useState(false);
return (
<SettingsContext.Provider
value={{
Expand All @@ -80,6 +82,8 @@ export const SettingsProvider = ({ children }) => {
setIconPosition,
iconDesign,
setIconDesign,
hasChanges,
setHasChanges,
}}
>
{children}
Expand Down

0 comments on commit ac4f5b3

Please sign in to comment.