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

[components] Proof of new slug concept #585

Merged
merged 15 commits into from
Feb 28, 2018
Merged

[components] Proof of new slug concept #585

merged 15 commits into from
Feb 28, 2018

Conversation

kristofferjs
Copy link
Contributor

Not ready to merge

  • Docs says slugify on options for custom slugify function (code says slugifyFn) Changed to match the docs.
  • No auto
  • One button to generate slug based on slugify and source

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.

@kristofferjs
Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

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.

@skogsmaskin
Copy link
Member

skogsmaskin commented Feb 15, 2018

I think we also should fix the issue with invalid slug matching published documents.

The default validate function is here: /packages/@sanity/form-builder/src/sanity/inputs/Slug.js. It should ignore results from a published document that is the same as the draft currently in edit.

@kristofferjs
Copy link
Contributor Author

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.

@kristofferjs
Copy link
Contributor Author

@rexxars This is now rebased with next (after validation branch merge).
Just need to do the slug-validation and I think we are good to go. (see slug.js). The query is in Slug.js. I think it should be defined one place. We dont have access to the document-variable now, but I think you had a plan for this.

@rexxars
Copy link
Member

rexxars commented Feb 20, 2018

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.

@rexxars rexxars dismissed skogsmaskin’s stale review February 20, 2018 09:24

Out of date, please re-review

Copy link
Member

@bjoerge bjoerge left a 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
Copy link
Member

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)
Copy link
Member

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?

Copy link
Member

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

Copy link
Member

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?

Copy link
Member

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 = {
Copy link
Member

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,
Copy link
Member

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}))
Copy link
Member

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'
Copy link
Member

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()))
Copy link
Member

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.

@rexxars
Copy link
Member

rexxars commented Feb 22, 2018

Fixed mentioned issues - not sure if the validation list / styling changes were intentional, leaving that up to @kristofferj to decide on

@rexxars rexxars requested a review from bjoerge February 22, 2018 13:25
@bjoerge
Copy link
Member

bjoerge commented Feb 22, 2018

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:

  • The generate slug button is still clickable.
  • When you click it, you actually unset the current value of the slug field.

Would it make sense to disable the "Generate slug" button when the source field is (or the function returns) empty?

@rexxars
Copy link
Member

rexxars commented Feb 22, 2018

Good call. Can't really disable it if the function returns false, surely? But if it's not set, sure.

@kristofferjs
Copy link
Contributor Author

@rexxars @bjoerge This is maybe ready to be merged now?

@rexxars
Copy link
Member

rexxars commented Feb 28, 2018

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.

@bjoerge
Copy link
Member

bjoerge commented Feb 28, 2018

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.

@kristofferjs kristofferjs merged commit 03b7d93 into next Feb 28, 2018
@kristofferjs kristofferjs deleted the slug-bugs branch February 28, 2018 09:16
@bjoerge
Copy link
Member

bjoerge commented Feb 28, 2018

@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?

@rexxars
Copy link
Member

rexxars commented Feb 28, 2018

The documentation was/is wrong in regards to the slugify/slugifyFn option. I will send a PR that ensures that either one will work and warn when using the slugifyFn-form.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants