Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/unique slug validation #246

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/app/(cloud)/cloud/_components/UniqueSlug/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@ export const UniqueSlug: React.FC<{

const debouncedSlug = useDebounce(state.slug, 100)

const currentSlug = React.useRef(initialValue || '')

useEffect(() => {
let timer: NodeJS.Timeout

if (!isRequesting.current && (validateOnInit || state.userInteracted)) {
isRequesting.current = true

const slug = currentSlug.current

if (!slug) {
setError('Please input a slug')
dispatchState({ type: 'SET_UNIQUE', payload: false })
isRequesting.current = false
return
}

if (debouncedSlug) {
if (debouncedSlug.length < 3) {
setError('The slug must be at least 3 characters long.')
Expand Down Expand Up @@ -134,18 +145,18 @@ export const UniqueSlug: React.FC<{
description = ''
} else if (!state.fetched && (validateOnInit || state.userInteracted)) {
description = 'Checking slug availability...'
} else if (!validatedSlug) {
} else if (!currentSlug.current) {
description = 'Please input a slug'
isError = true
} else if (validatedSlug.length < 3) {
} else if (currentSlug.current.length < 3) {
description = 'The slug must be at least 3 characters long.'
isError = true
} else if (error) {
description = error
} else if (!slugIsValid) {
description = `'${validatedSlug}' is not available. Please choose another.`
description = `'${currentSlug.current}' is not available. Please choose another.`
} else if (slugIsValid) {
description = `'${validatedSlug}' is available`
description = `'${currentSlug.current}' is available`
}

let icon: React.ReactNode = null
Expand All @@ -162,6 +173,7 @@ export const UniqueSlug: React.FC<{
label={label}
initialValue={initialValue}
onChange={newSlug => {
currentSlug.current = newSlug
dispatchState({ type: 'SET_SLUG', payload: newSlug })
dispatchState({ type: 'SET_USER_INTERACTED' })
}}
Expand Down