{"size": 5796, "ext": "js", "max_stars_repo_path": "src/views/blog/EditBlogCategory.js", "max_stars_repo_name": "Sumeshwar-pandey/admissify", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/views/blog/EditBlogCategory.js", "max_issues_repo_name": "Sumeshwar-pandey/admissify", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/views/blog/EditBlogCategory.js", "max_forks_repo_name": "Sumeshwar-pandey/admissify", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.6721311475, "max_line_length": 80, "alphanum_fraction": 0.4791235335}
import React, { useState } from "react";
//Bootstrap and jQuery libraries
import "bootstrap/dist/css/bootstrap.min.css";
import { Link } from "react-router-dom";
import "jquery/dist/jquery.min.js";
import "./Style.css";
//Datatable Modules
import "datatables.net-dt/js/dataTables.dataTables";
import "datatables.net-dt/css/jquery.dataTables.min.css";
import "font-awesome/css/font-awesome.min.css";
import $ from "jquery";
import baseurl from "../../services/apiconfig";
import Multiselect from "multiselect-react-dropdown";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import { getToken } from "../../utils/Auth/token";
import jwt_decode from "jwt-decode";
const isAuthenticated = getToken();
var user = jwt_decode(isAuthenticated);
const axios = require("axios");
class EditBlogCategory extends React.Component {
  componentDidMount() {
    $(document).ready(function () {
      $("#example").DataTable();
    });
    $(document).on("input", ".number", function () {
      this.value = this.value.replace(/\D/g, "");
    });

    //Getting ID From URL
    var url = window.location.pathname;
    var id = url.substring(url.lastIndexOf("/") + 1);
    //For State By ID
    axios
      .get(baseurl.baseurl + `/blogcategory/getbyid?id=${id}`)
      .then((res) => {
        
        this.setState({
          id: res.data.data.id,
          category: res.data.data.category,
        });
      })
      .catch((err) => {
        //console.log("err",err)
        toast.error("Error occurred at API end (" + err.message + ")");
      });
  }

  constructor() {
    super();
    this.state = { formErrors: {} };
  }
  handleChange(evt) {
    this.setState({
      [evt.target.name]: evt.target.value,
    });
  }

  categoryValidation() {
    const { category } = this.state;
    let formErrors = {};
    let formIsValid = true;
    if (!category) {
      formIsValid = false;
      formErrors["categoryNameErr"] = "This field is required.";
    }
    this.setState({ formErrors: formErrors });
    return formIsValid;
  }
  //Update Blog Category
  updateCategory(evt, id) {
    evt.preventDefault();
    if (this.categoryValidation()) {
      const postData = {
        category: this.state.category,
        updated_by: user?.id,
      };
      if (this.state.category) {
        axios
          .post(baseurl.baseurl + `/blogcategory/checkblogcategory`, postData)
          .then((res) => {
            if (this.state.category.trim() == "") {
              toast.error("Category will not submit with blank space.");
            } else {
              axios
                .put(baseurl.baseurl + `/blogcategory/update/${id}`, postData)
                .then((res) => {
                  
                  toast.success(res.data.message);
                  window.location.href = "/blog";
                })
                .catch((err) => {
                  toast.error(
                    "Error occurred at API end (" + err.message + ")"
                  );
                });
            }
          })
          .catch((err) => {
            console.log("err", err);
            toast.error("Category already exist!");
          });
      }
      this.setState(this.initialState);
    }
  }
  render() {
    const { categoryNameErr } = this.state.formErrors;
    return (
      <div>
        <section className="breadcrumbs">
          <span>
            <Link className="crumbs" to={"/dashboard"}>
              Home
            </Link>
          </span>{" "}
          <span>
            <span className="fa fa-circle dot"></span>
          </span>{" "}
          <Link className="crumbs" to={"/blog"}>
            Blogs
          </Link>{" "}
          <span>
            <span className="fa fa-circle dot"></span>
          </span>{" "}
          <span>Edit Blog Category</span>
        </section>
        <section className="content">
          <ToastContainer />
          <div className="col-xs-12">
            <div className="box">
              <div className="box-body row">
                <div className="col-lg-12 col-sm-12 col-xs-12">
                  <div className="form-group">
                    <label> Category</label>
                    <input
                      type="text"
                      name="category"
                      onChange={(evt) => this.handleChange(evt)}
                      defaultValue={this.state.category}
                      className={
                        categoryNameErr
                          ? "form-control showError"
                          : "form-control"
                      }
                      placeholder="Enter category name"
                    />
                    {categoryNameErr && (
                      <div style={{ color: "red", paddingBottom: 2 }}>
                        {categoryNameErr}
                      </div>
                    )}
                  </div>
                </div>
              </div>
              <div className="col-lg-12 col-sm-12 col-xs-12">
                <div className="form-group">
                  <center>
                    <input
                      className="btn btn-warning"
                      type="submit"
                      value="Update"
                      onClick={(evt) => this.updateCategory(evt, this.state.id)}
                    />
                    <Link
                      className="btn btn-success"
                      to={"/blog"}
                      style={{ marginLeft: "10px" }}
                    >
                      Close
                    </Link>
                  </center>
                </div>
              </div>
            </div>
          </div>
        </section>
      </div>
    );
  }
}
export default EditBlogCategory;
