Skip to content

Commit

Permalink
Added feature to reset the preferences to default values. #1900
Browse files Browse the repository at this point in the history
  • Loading branch information
pravesh-sharma committed Aug 27, 2024
1 parent f5c1cd9 commit 73d7804
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
14 changes: 14 additions & 0 deletions web/pgadmin/preferences/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,17 @@ def update():
data={'data': 'Success'},
status=200
)


@blueprint.route("/", methods=['DELETE'], endpoint="reset_prefs")
@pga_login_required
def reset():
"""
Reset preferences to default
"""
res, msg = Preferences.reset()

if not res:
return internal_server_error(errormsg=msg)

return success_return()
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import getApiInstance from '../../../../static/js/api_instance';
import CloseSharpIcon from '@mui/icons-material/CloseSharp';
import HelpIcon from '@mui/icons-material/HelpRounded';
import SaveSharpIcon from '@mui/icons-material/SaveSharp';
import SettingsBackupRestoreIcon from'@mui/icons-material/SettingsBackupRestore';
import pgAdmin from 'sources/pgadmin';
import { DefaultButton, PgIconButton, PrimaryButton } from '../../../../static/js/components/Buttons';
import BaseUISchema from 'sources/SchemaView/base_schema.ui';
Expand Down Expand Up @@ -589,6 +590,39 @@ export default function PreferencesComponent({ ...props }) {
window.open(url_for('help.static', { 'filename': 'preferences.html' }), 'pgadmin_help');
};

const reset = () => {
pgAdmin.Browser.notifier.confirm(
gettext('Reset preferences'),
gettext('All the preferences will be reset to default. Object explorer tree will be automatically refreshed to reflect the changes.')
+ gettext('Please note, an app reload is required to change the language of the application. You may decide to reload later.') + '<br>' + '<br>'
+ gettext('Do you want to proceed?'),
function () {},
function () {},
'',
'Cancel',
[
<DefaultButton key="save-reload" className='Alert-margin' onClick={()=>resetPrefsToDefault(true)} startIcon={<SaveSharpIcon />}>{gettext('Save & Reload')}</DefaultButton>,
<PrimaryButton key="save-reload-later" className='Alert-margin' onClick={()=>resetPrefsToDefault(false)} startIcon={<SaveSharpIcon />} autoFocus={true} >{gettext('Save & Reload Later')}</PrimaryButton>
]
);
};

const resetPrefsToDefault = (refresh = false) => {
api({
url: url_for('preferences.index'),
method: 'DELETE'
}).then(()=>{
if (refresh){
location.reload();
return true;
}
preferencesStore.cache();
props.closeModal();
}).catch((err) => {
pgAdmin.Browser.notifier.alert(err.response.data);
});
};

return (
<StyledBox height={'100%'}>
<Box className='PreferencesComponent-root'>
Expand Down Expand Up @@ -618,6 +652,9 @@ export default function PreferencesComponent({ ...props }) {
<DefaultButton className='PreferencesComponent-buttonMargin' onClick={() => { props.closeModal();}} startIcon={<CloseSharpIcon onClick={() => { props.closeModal();}} />}>
{gettext('Cancel')}
</DefaultButton>
<DefaultButton className='PreferencesComponent-buttonMargin' onClick={reset} startIcon={<SettingsBackupRestoreIcon />}>
{gettext('Reset to default')}
</DefaultButton>
<PrimaryButton className='PreferencesComponent-buttonMargin' startIcon={<SaveSharpIcon />} disabled={disableSave} onClick={() => { savePreferences(prefChangedData, initValues); }}>
{gettext('Save')}
</PrimaryButton>
Expand Down
14 changes: 10 additions & 4 deletions web/pgadmin/static/js/helpers/ModalProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ export function useModal() {
return React.useContext(ModalContext);
}

function AlertContent({ text, confirm, okLabel = gettext('OK'), cancelLabel = gettext('Cancel'), onOkClick, onCancelClick }) {
function AlertContent({ text, confirm, okLabel = gettext('OK'), cancelLabel = gettext('Cancel'), onOkClick, onCancelClick, extraButtons }) {
return (
<StyledBox display="flex" flexDirection="column" height="100%">
<Box flexGrow="1" p={2}>{typeof (text) == 'string' ? HTMLReactParser(text) : text}</Box>
<Box className='Alert-footer'>
{confirm &&
<DefaultButton startIcon={<CloseIcon />} onClick={onCancelClick} >{cancelLabel}</DefaultButton>
}
<PrimaryButton className='Alert-margin' startIcon={<CheckRoundedIcon />} onClick={onOkClick} autoFocus={true} >{okLabel}</PrimaryButton>
{
extraButtons.length ?
extraButtons :
<PrimaryButton className='Alert-margin' startIcon={<CheckRoundedIcon />} onClick={onOkClick} autoFocus={true} >{okLabel}</PrimaryButton>
}
</Box>
</StyledBox>
);
Expand All @@ -61,6 +65,7 @@ AlertContent.propTypes = {
onCancelClick: PropTypes.func,
okLabel: PropTypes.string,
cancelLabel: PropTypes.string,
extraButtons: PropTypes.array
};

function alert(title, text, onOkClick, okLabel = gettext('OK')) {
Expand All @@ -76,7 +81,7 @@ function alert(title, text, onOkClick, okLabel = gettext('OK')) {
});
}

function confirm(title, text, onOkClick, onCancelClick, okLabel = gettext('Yes'), cancelLabel = gettext('No')) {
function confirm(title, text, onOkClick, onCancelClick, okLabel = gettext('Yes'), cancelLabel = gettext('No'), extraButtons = []) {
// bind the modal provider before calling
this.showModal(title, (closeModal) => {
const onCancelClickClose = () => {
Expand All @@ -87,8 +92,9 @@ function confirm(title, text, onOkClick, onCancelClick, okLabel = gettext('Yes')
onOkClick?.();
closeModal();
};
extraButtons = extraButtons?.map(button=>React.cloneElement(button, { onClick:()=>{closeModal(); button.props.onClick();}}));
return (
<AlertContent text={text} confirm onOkClick={onOkClickClose} onCancelClick={onCancelClickClose} okLabel={okLabel} cancelLabel={cancelLabel} />
<AlertContent text={text} confirm onOkClick={onOkClickClose} onCancelClick={onCancelClickClose} okLabel={okLabel} cancelLabel={cancelLabel} closeModal={closeModal} extraButtons={extraButtons} />
);
});
}
Expand Down
4 changes: 2 additions & 2 deletions web/pgadmin/static/js/helpers/Notifier.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ class Notifier {
this.modal.alert(title, text, onOkClick, okLabel);
}

confirm(title, text, onOkClick, onCancelClick, okLabel=gettext('Yes'), cancelLabel=gettext('No')) {
confirm(title, text, onOkClick, onCancelClick, okLabel=gettext('Yes'), cancelLabel=gettext('No'), extraButtons=[]) {
/* Use this if you want to use pgAdmin global notifier.
Or else, if you want to use modal inside iframe only then use ModalProvider eg- query tool */
this.modal.confirm(title, text, onOkClick, onCancelClick, okLabel, cancelLabel);
this.modal.confirm(title, text, onOkClick, onCancelClick, okLabel, cancelLabel, extraButtons);
}

showModal(title, content, modalOptions) {
Expand Down
17 changes: 17 additions & 0 deletions web/pgadmin/utils/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,20 @@ def migrate_user_preferences(self, pid, converter_func):
pref.value = converter_func(pref.value)

db.session.commit()

@classmethod
def reset(cls):
"""
reset
Reset the preferences for the current user in the configuration table.
"""
try:
db.session.query(UserPrefTable).filter(
UserPrefTable.uid == current_user.id).delete()
db.session.commit()
except Exception as e:
db.session.rollback()
current_app.logger.exception(e)
return False, str(e)

return True, None

0 comments on commit 73d7804

Please sign in to comment.