mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
import { debounce } from 'lodash'
|
|
import { checkSlugAvailableQuery } from '~/graphql/User.js'
|
|
|
|
export default function UniqueSlugForm({ translate, apollo, currentUser }) {
|
|
return {
|
|
formSchema: {
|
|
slug: [
|
|
{
|
|
type: 'string',
|
|
required: true,
|
|
pattern: /^[a-z0-9_-]+$/,
|
|
message: translate('settings.validation.slug.regex'),
|
|
},
|
|
{
|
|
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)()
|
|
},
|
|
},
|
|
],
|
|
},
|
|
}
|
|
}
|