add some utils

This commit is contained in:
casperlamboo 2017-11-02 16:59:56 +01:00
parent 831160394c
commit 81b144b86d
2 changed files with 18 additions and 1 deletions

View File

@ -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];
}

View File

@ -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));
})
}