Ocelot-Social/webapp/components/utils/UniqueSlugForm.js
roschaefer be6c4a6f7c Refactor to use FormSchema
That way we can re-use the code for slug validation in the
CreateUserAccount component
2019-09-20 20:05:27 +02:00

31 lines
967 B
JavaScript

import { debounce } from 'lodash'
import { checkSlugAvailableQuery } from '~/graphql/User.js'
export default function UniqueSlugForm({ translate, apollo, currentUser }) {
return {
formSchema: {
slug: [
{
asyncValidator(rule, value, callback) {
debounce(() => {
const variables = { slug: value }
apollo.query({ query: checkSlugAvailableQuery, variables }).then(response => {
const {
data: { User },
} = response
const existingSlug = User && User[0] && User[0].slug
const available = !existingSlug || existingSlug === currentUser.slug
if (!available) {
callback(new Error(translate('settings.validation.slug.alreadyTaken')))
} else {
callback()
}
})
}, 500)()
},
},
],
},
}
}