-
Notifications
You must be signed in to change notification settings - Fork 429
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
[components] Proof of new slug concept #585
Conversation
May also be connected to #430 |
@@ -129,39 +134,19 @@ export default class SlugInput extends React.Component { | |||
} | |||
const {type, slugifyFn} = this.props | |||
|
|||
const slugify = get(type, 'options.slugifyFn') || slugifyFn | |||
const slugify = get(type, 'options.slugify') || slugifyFn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a breaking change. Should be kept as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. need to update the documentation then.
I think we also should fix the issue with invalid slug matching published documents. The default validate function is here: |
The validation branch is soon merged. I think we should remove all validatin-stuff from slug, and use the new validation functionality. Will also prevent people from publishing used slugs. |
09538df
to
80404f0
Compare
@rexxars This is now rebased with next (after validation branch merge). |
I've simplified this greatly by removing the factory pattern, as I don't think this is currently in use anywhere. I've also implemented the uniqueness check using the validation pattern and fixed the default uniqueness check to exclude itself in both draft and published form. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition the the comments above, I also found a few issues when testing:
- Clicking "Generate Slug" on a new document causes an exception to be thrown (this is related to https://github.com/sanity-io/sanity/pull/585/files#diff-1978e92fdc897387c12581ed8404a418R57 I think)
- Doesn't seem like the slug gets updated when clicking "Generate Slug"-button
- The slug value gets unset if it has a value, and you click "Generate Slug"-button, also causing a validation error saying Slug is aleady in use
- Doesn't seem like we can unset the value? This is maybe not a regression, but should probably be supported?
return Promise.resolve() | ||
slugify(sourceValue) { | ||
if (!sourceValue) { | ||
return sourceValue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be wrapped in a Promise.resolve
so that this method always returns a promise?
label: type.title, | ||
description: type.description, | ||
level: level | ||
const newFromSource = typeof source === 'function' ? source(document) : get(document, source) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do we handle the case where newFromSource may be undefined/empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't matter, as this.slugify
checks for truthyness
@@ -32,37 +32,8 @@ | |||
line-height: 1em; | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes in this file to be unrelated to the slug input. Is it intentional that they are included in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. @kristofferj ?
@@ -13,6 +13,11 @@ export default class Switch extends React.Component { | |||
readOnly: PropTypes.bool | |||
} | |||
|
|||
static defaultProps = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also seems unrelated to the current PR
@@ -9,6 +9,7 @@ export default class ValidationListItem extends React.PureComponent { | |||
onClick: PropTypes.func, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unclear to me why changes in this file should be a part of this PR.
// eslint-disable-next-line no-console | ||
console.error(`An error occured while slugifying "${newFromSource}":\n${err.stack}`) | ||
}) | ||
.then(() => this.setState({loading: false})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we guard against the component being unmounted here?
@@ -1,19 +0,0 @@ | |||
import React from 'react' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good riddance 💃
this.props.onChange(PatchEvent.from(value ? set(value) : unset())) | ||
} | ||
updateValue(value) { | ||
this.props.onChange(PatchEvent.from(value ? set(value) : unset())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably make sure we add a type annotation here, and always add _type: this.props.type.name
to the value emitted.
Fixed mentioned issues - not sure if the validation list / styling changes were intentional, leaving that up to @kristofferj to decide on |
Nice! Works a lot better now. The only minor issue left is that the behavior when the source field is not set is a bit strange in that:
Would it make sense to disable the "Generate slug" button when the source field is (or the function returns) empty? |
Good call. Can't really disable it if the function returns false, surely? But if it's not set, sure. |
af59a73
to
cc92dfb
Compare
Still doesn't include a fix for generating a slug until the uniqueness check returns true - if we want to include that in this release, we'll need @bjoerge to expose some way to find the full path of the input from within the input component. In my opinion this is something we could easily add in the future, though. |
Agreed. I created #617 for providing the path to input components. |
@skogsmaskin, @rexxars & @kristofferj Just to be on the safe side: are you all confident that these changes are backward compatible? And that there are no docs that needs to be updated? |
The documentation was/is wrong in regards to the The documentation also needs to be updated to describe the current uniqueness check constraints and how to provide your own. I will update the documentation and make it ready to be published. |
Not ready to merge
slugify
on options for custom slugify function (code saysslugifyFn
) Changed to match the docs.There are som TODOs in the code now. Biggest issue is that when trying to find an unique slug, it shows validator error between each try.