Updating a Notion page through a node website

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

By now, we have created a website to showcase movies from our Notion database. That already is pretty awesome, but let's take a step further and add the option to update these Notion pages.
What we'll do is add a checkbox to indicate whether we saw a movie or not.
This will send a request to our Node express server and update the Notion document page with the value of the checkbox.
To write down the steps that need to happen:
In total, it should look something like this:

Let's start with the Notion update call first.
Open the modules/notion.js document and create a new function called toggleMovie.
toggleMovie: async (id, value) => {
await notion.pages.update({
page_id: id,
properties: {
Watched: { checkbox: value },
},
});
},
We add two parameters to this call: the id and the value.
The id is the unique page id.
Then we call the Notion page update query and pass the property we want to change, the Watched property checkbox value.
We can now use this function in the following manner.
const {toggleMovie} = require('./modules/notion');
await toggleMovie('UNIQUE-ID', true | false);
Then we introduced some middleware in the form of our Node express app.
Let's import the toggleMovie function:
const {getDatabase, toggleMovie} = require('./modules/notion');
Then we need to enable express to handle post requests:
app.use(express.json());
Then we make a put request:
app.put('/movie/:id', async (req, res) => {
await toggleMovie(req.params.id, req.body.checked);
res.json('done');
});
As you can see, it includes a part :id. This is a dynamic route, and we can retrieve that by using the req.params.id method.
Where id refers to the variable name in :id.
Now it's time to bind everything together in the front end.
Let's create a function that will call our Node API, to begin with.
const updateMovie = async (id, checked) => {
await fetch(`http://localhost:8000/movie/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({checked: checked}),
});
};
This is a fetch call that performs a PUT request to our movie id endpoint.
And it passed a variable checked value.
Now let's add the checkbox to our front end. The first goal is to render this in a way that shows the current state.
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.checked = movie.watched;
checkbox.className = 'absolute w-5 h-5 -top-2 right-4';
textCardDiv.appendChild(checkbox);
The initial value is set by the movie.watched variable, which refers to the boolean value.
Since the checkbox is absolute, we need to add a relative class to the parent div.
textContainerDiv.className = 'relative z-10 px-4 -mt-8';
The last part is to bind a change event to our checkbox. This will fire every time our checkbox changes value. And it's what will execute our calls.
checkbox.addEventListener('change', (event) => {
updateMovie(movie.id, event.currentTarget.checked);
});
We pass the movie id to our function, as well as the current checkbox value. This already reflects the changed value, so we don't need to invert it for this method.
And that's it. We now have the option to update a Notion page through our own Node express website.
You can find the complete code example on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter