Skip to content

Commit

Permalink
[DDW-619] Async selector
Browse files Browse the repository at this point in the history
  • Loading branch information
thedanheller committed Apr 22, 2019
1 parent c762978 commit b96c051
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
4 changes: 2 additions & 2 deletions source/renderer/app/components/wallet/WalletCreateDialog.js
Expand Up @@ -191,8 +191,8 @@ export default class WalletCreateDialog extends Component<Props, State> {
};
this.props.onSubmit(walletData);
},
onError: form => {
handleFormErrors(form);
onError: () => {
handleFormErrors('.SimpleFormField_error');
this.setState({ isSubmitting: false });
},
});
Expand Down
8 changes: 7 additions & 1 deletion source/renderer/app/components/wallet/WalletRestoreDialog.js
Expand Up @@ -176,6 +176,12 @@ export default class WalletRestoreDialog extends Component<Props, State> {

recoveryPhraseAutocomplete: Autocomplete;

componentWillReceiveProps(newProps: Props) {
if (!this.props.error && newProps.error) {
handleFormErrors('.WalletRestoreDialog_error');
}
}

form = new ReactToolboxMobxForm(
{
fields: {
Expand Down Expand Up @@ -296,7 +302,7 @@ export default class WalletRestoreDialog extends Component<Props, State> {

onSubmit(walletData);
},
onError: handleFormErrors,
onError: handleFormErrors.bind(this, '.SimpleFormField_error'),
});
};

Expand Down
14 changes: 11 additions & 3 deletions source/renderer/app/utils/ReactToolboxMobxForm.js
@@ -1,5 +1,6 @@
// @flow
import MobxReactForm from 'mobx-react-form';
import { waitForExist } from './waitForExist';

export default class ReactToolboxMobxForm extends MobxReactForm {
bindings() {
Expand All @@ -21,7 +22,14 @@ export default class ReactToolboxMobxForm extends MobxReactForm {
}
}

export const handleFormErrors = () => {
const firstErrorLabel = document.querySelector('.SimpleFormField_error');
if (firstErrorLabel) firstErrorLabel.scrollIntoView({ behavior: 'smooth' });
export const handleFormErrors = async (querySelector: string) => {
try {
const firstErrorLabel = await waitForExist(querySelector);
firstErrorLabel.scrollIntoView({ behavior: 'smooth' });
if (firstErrorLabel.nextSibling && firstErrorLabel.nextSibling.click) {
setTimeout(() => firstErrorLabel.nextSibling.click(), 500);
}
} catch (err) {
throw err;
}
};
50 changes: 50 additions & 0 deletions source/renderer/app/utils/waitForExist.js
@@ -0,0 +1,50 @@
// @flow

type Options = {
rejectTimeoutTime?: number,
checkIntervalTime?: number,
context?: HTMLElement,
selectAll?: boolean,
};

const REJECT_TIMEOUT = 10000;
const CHECK_INTERVAL = 500;

export const waitForExist = (
selector: string,
options?: Options = {}
): Promise<any> => {
const {
rejectTimeoutTime = REJECT_TIMEOUT,
checkIntervalTime = CHECK_INTERVAL,
context = document,
selectAll,
} = options;
return new Promise((resolve, reject) => {
const rejectTimeout = setTimeout(() => {
clearInterval(checkInterval);
reject(new Error('Element(s) not found'));
}, rejectTimeoutTime);

const doResolve = selection => {
resolve(selection);
clearInterval(checkInterval);
clearTimeout(rejectTimeout);
};

const checkAll = () => {
const selection = context.querySelectorAll(selector);
if (selection.length) doResolve(selection);
};

const checkSingle = () => {
const selection = context.querySelector(selector);
if (selection) doResolve(selection);
};

const check = selectAll ? checkAll : checkSingle;

const checkInterval = setInterval(check, checkIntervalTime);
check();
});
};

0 comments on commit b96c051

Please sign in to comment.