Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

How to create a new document in draft mode using Sanity.js and Next.js.

4 replies
Last updated: Mar 26, 2024
Hey, Im trying learn something new. Let a user submit a form in the front-end, this should create a new document in draft mode, does anyone have an example of this being done?
Mar 22, 2024, 10:44 AM
I was actually building out an example of this a few weeks ago and then got side tracked and couldn’t finish. There’s a good intro to forms in general in Next here . The main thing is you’d replace the database you’re sending your form to with Sanity using our JS client. In order to make it a draft, you’d need to prepend
drafts.
to the
_id
of the document.
Mar 22, 2024, 3:28 PM
Excellent rd! Thanks:) The good ol sidetrack 😂
Mar 22, 2024, 3:52 PM
I implemented something similar to what you are looking for. A blog application that had an /editor path where your could edit and create blogs outside of sanity. though if i remember correctly my sanity would automatically publish new blogs through the API.
If you are interested to see the repo just shoot me a message.
Mar 25, 2024, 12:34 PM
Thanks Tuomas! Actually got it working! But since we are already here, I got another question that one of you might have the answer to. Im displaying this data on a tv, and would like to update the data when I publish any changes, im doing it now with an effect, is there any way i can get the effect to fire when we update the data? The useQuery with webhook and tags perhaps? This is the current setup
  useEffect(() => {
    const fetchData = async () => {
      const data = await client.fetch(query, { weekNumber: weekNumber })
      setData(data)
      //Endre denne for å teste loading
      // setTimeout(() => {
      //   setIsLoading(false)
      // }, 10000)
    }

    fetchData()
    //Endre denne for å oppdatere på intervall
    // const refreshInterval = 30 * 1000
    // const timeoutId = setTimeout(() => {
    //   window.location.reload()
    // }, refreshInterval)

    // return () => clearTimeout(timeoutId)
  }, [weekNumber])
Mar 26, 2024, 2:11 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?