diff --git a/src/utils/dbUtils.js b/src/utils/dbUtils.js index 030d14c..2b68313 100644 --- a/src/utils/dbUtils.js +++ b/src/utils/dbUtils.js @@ -1,3 +1,5 @@ +import PouchDB from 'pouchdb'; + export function getLegalDBName(input) { return encodeURIComponent(input.toLowerCase()) .replace(/\./g, '%2E') @@ -11,3 +13,9 @@ export function getLegalDBName(input) { .toLowerCase() .replace(/(%..)/g, esc => `(${esc.substr(1)})`); } + +const dbs = {}; +export function getDb(dbUrl) { + if (!dbs[dbUrl]) dbs[dbUrl] = new PouchDB(dbUrl); + return dbs[dbUrl]; +} diff --git a/src/utils/reactUtils.js b/src/utils/reactUtils.js index a6e22cb..fbd4203 100644 --- a/src/utils/reactUtils.js +++ b/src/utils/reactUtils.js @@ -1,4 +1,4 @@ -import { SubmissionError } from 'redux-form'; +import { SubmissionError, startAsyncValidation, stopAsyncValidation } from 'redux-form'; // redux form submit promise wrapper // - turns all errors into SubmissionError @@ -38,3 +38,12 @@ export function classNames(...names) { .filter(name => typeof name === 'string') .join(' '); } + +export function asyncValidateForm(dispatch, form, asyncValidate, formData) { + dispatch(startAsyncValidation(form)); + asyncValidate(formData).then(() => { + dispatch(stopAsyncValidation(form)); + }).catch(error => { + dispatch(stopAsyncValidation(form, error)); + }) +}