javascript3 Min Read

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Edit/Update Saved Article

Gorav Singal

May 07, 2021

TL;DR

Edit saved articles in a Next.js and Strapi setup by loading Draft.js content state, restricting edit access to the author, and redirecting to the article page after update.

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Edit/Update Saved Article

Introduction

This post is in contuation of our previous post: How to use Draft.js with Next.js and Create Article

In this post, we will try to edit a saved article. We will do following:

  • Show Edit link only to author
  • Edit link for an article
  • Open the form with saved content
  • Update it and redirect to article page

Since I can have many articles, and many authors. I want to Show Edit Article link to only author of the article. Note: A backend check must be there. You can not rely on the frontend checks. This is just to show the link to author only.

In the Article page, where we GET the article, it has the author information as well. And, if the user is logged in, we can compare their username or email.

Lets see code

Function to process data

We will process the json data returned by GET API, and will check for author. If its for author, we will put a flag.

async processAuthor(articles, loggedinUser) {
  if (articles && articles.length > 0) {
    let currentUserEmail = null;
    if (loggedinUser && loggedinUser.email) {
      currentUserEmail = loggedinUser.email;
    }

    if (currentUserEmail) {
      for (let i=0; i<articles.length; i++) {
        if (articles[i].author.email == currentUserEmail) {
          articles[i].myArticle = true;
        }
      }
    }
  }

  return articles;
}

So, we have put a flag: myArticle

Article Page

Lets see code:

export default function Article(props) {
  if (props.error) {
    return <SimpleLayout>
      <ErrorPage statusCode={props.error.statusCode} />
    </SimpleLayout>
  }
  const [ session, loading ] = useSession();

  const body = draftToHtml(JSON.parse(props.body));
  const showEditOption = props.myArticle;
  return (
    <SimpleLayout>
      <h1>{props.title}</h1>
      {showEditOption && 
        <Link href={`/nodes/${props.id}/edit`}>
          <a>
            Edit
          </a>
        </Link>
      }
      <h4>Author: {props.author.username}</h4>
      <div dangerouslySetInnerHTML={{__html: body}}></div>
    </SimpleLayout>
  )
}

export async function getServerSideProps(context) {
  const session = await getSession(context);

  try {
    let data = await apiClient.getArticleBySlug(context.query.slug[0]);
    if (!data || data.length == 0) {
      return {props: {error: {statusCode: 404}}}
    }

    //process author
    // console.log(data);
    data = await contentProcessor.processAuthor(data, session.user);
    return {props: data[0]}
  } catch(error) {
    return {props: {error: {statusCode: 404}}}
  }
}

EditArticle Component

Since, I will be having same code for creating new article and editing article. I want to use code. I don’t want another duplicate code.

I’m developing a common component called: EditArticle

import React, { Component } from 'react'
import apiClient from '../api/api_client'
import { Form, Button, Card } from 'react-bootstrap';
import { withRouter } from 'next/router'
import ArticleEditor from '../editor/editor'

class EditArticle extends Component {
  constructor(props) {
    super(props);
    this.state = {
      id: props.article.id || null,
      title: props.article.title || "",
      body: props.article.body || null,
      isNew: props.article.body ? false : true,

      // this is to make sure we dont do stringify again
      articleUpdated: false
    };

    this.handleInputs = this.handleInputs.bind(this);
    this.submitForm = this.submitForm.bind(this);
  }

  handleInputs = (event) => {
    let {name, value} = event.target
    this.setState({
      [name]: value
    });
  }
  handleEditorContent = (content) => {
    this.setState({
      body: content,
      articleUpdated: true
    });
  }

  submitForm = async (event) => {
    event.preventDefault()
    let article;
    if (this.state.isNew) {
      article = await apiClient.saveArticle({
        title: this.state.title,
        body: JSON.stringify(this.state.body)
      })
    }
    else {
      let articleContent = this.state.body;
      if (this.state.articleUpdated) {
        articleContent = JSON.stringify(articleContent);
      }
      article = await apiClient.updateArticle({
        id: this.state.id,
        title: this.state.title,
        body: articleContent
      })
    }
    this.props.router.push(`/articles/${article.slug}`);
  }

  render() {
    return (
        <Form onSubmit={this.submitForm}>
          <Form.Group controlId="formBasicEmail">
            <Form.Label>Headline</Form.Label>
            <Form.Control type="text" 
              name="title"
              value={this.state.title}
              onChange={this.handleInputs} />
            <Form.Text className="text-muted">
              Give a nice title to your article
            </Form.Text>
          </Form.Group>

          <Form.Group controlId="exampleForm.ControlTextarea2">
            <Form.Label>Body</Form.Label>
            <Card className="p-2">
              <ArticleEditor 
                handleContent={this.handleEditorContent}
                initialContent={this.state.body}
                />
            </Card>
          </Form.Group>
          <Button variant="primary" type="submit">
            Submit
          </Button>
        </Form>
    )
  }
}

export default withRouter(EditArticle);

The code is pretty simple to understand. I’m maintaining a flag whether its for creating new article or editing existing article.

Edit Page

Note: The URL for edit I made is:

/nodes/[id]/edit

Lets create another page: nodes/[id]/edit.jsx

Note: [id] is a folder above.

import SimpleLayout from '../../../components/layout/simple'
import apiClient from '../../../components/api/api_client'
import EditArticle from '../../../components/editor/editArticle';

import React, { Component } from 'react'

export default class EditNode extends Component {
  constructor(props) {
    super(props);
    this.state = {
      id: props.article.id,
      title: props.article.title,
      body: props.article.body,
    };
  }

  render() {
    return (
      <SimpleLayout>
      <div className="row">
        <div className="col-8">
          <EditArticle 
            article={this.state}
          />
        </div>
        <div className="col-4">
          Hey
        </div>
      </div>
    </SimpleLayout>
    )
  }
}

export async function getServerSideProps(context) {
  try {
    let data = await apiClient.getArticleById(context.query.id);
    return {props: {
      article: data
    }}
  } catch(error) {
    return {props: {}}
  }
}

Screenshots

Show Edit link to author

Edit page

Complete Code at Github

You can find the complete code at our Official Github Repo

Share

Related Posts

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Create and View Article with Image Upload

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Create and View Article with Image Upload

Introduction In this post, we will use in Next.js with strapi. And, we will…

How to Integrate Next.js with Strapi Backend and Create a common utility class for REST APIs

How to Integrate Next.js with Strapi Backend and Create a common utility class for REST APIs

Introduction In this post, we will integrate Next.js with Strapi fully. And we…

How to Use Signin Signout Buttons in Next.js bootstrap project with Next-auth

How to Use Signin Signout Buttons in Next.js bootstrap project with Next-auth

Introduction In our last post, we have seen a full example of Next.js with…

Next.js Bootstrap Starter - Nice Template Navbar Header and Few Pages

Next.js Bootstrap Starter - Nice Template Navbar Header and Few Pages

Introduction In this post, we will do following: create a Next.js project…

Nextjs - How to Build Next.js Application on Docker and Pass API Url

Nextjs - How to Build Next.js Application on Docker and Pass API Url

Introduction In this post we will see: How to prepare a docker image for your…

Nextjs - How to Redirect to Another Page and Pass State

Nextjs - How to Redirect to Another Page and Pass State

Introduction We have a page, for example a . And, upon submission we would want…

Latest Posts

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…