JavaScript app — Book Reviews
Tags: rails, javascript, softwaredevelopment
Published: 2020-05-23 02:38:06

---
title: JavaScript app — Book Reviews
published: true
date: 2020-05-17 00:00:00 UTC
tags: rails,javascript, software-development,ruby-on-rails
canonical_url: 
---

### JavaScript app — Book Reviews

Due to the Covid-19, the stay-at-home orders for millions of people all over the world have changed how we spend our time. One activity that’s been untouched by the pandemic is Reading. What is on your reading list and option to share your favorite titles and add reviews, anything a potential reader should know about such as “Beautiful writing” or “great pricing” were used as a user story and a features on Book reviews application.

Ultimate goal, build Book reviews single page application (SPA) using a JavaScript frontend and a Rails API backend **was set.**

![](https://cdn-images-1.medium.com/max/1024/0*fNHUxanxe3VknhxP)<figcaption>Photo by <a href="https://unsplash.com/@alfonsmc10?utm_source=medium&amp;utm_medium=referral">Alfons Morales</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

During the Covid-19 pandemic in NYC, as many other parents, my new normal became filled with google classrooms and spreadsheets, zoom calls, kids’ online worksheets, checking their writing and helping them log onto google hangouts, turn in assignments and using many different platforms. Some of them like PEARSON REALIZE — newest learning management system, then IReady, MyON, iXL Science and Flipgrid really required my involvement.

<div style="width:100%;height:0;padding-bottom:56%;position:relative;"><iframe src="https://giphy.com/embed/JEhCPFfqi2Hy8" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></div><p><a href="https://giphy.com/gifs/disney-books-beauty-and-the-beast-belle-JEhCPFfqi2Hy8"></a></p>

And with all this “new normalcy”, building Book reviews application was not an easy task but i decided to use whatever time I can find to start working on this project and incorporate many of the new skills I was hoping to learn but this time with limited time due to three kids homeschooling.

> **_This required new approaches and mentorship._**

> **_Thanks to Noah Pryer, CTO at_** [**_TEACHABLE_**](https://teachable.com/) **_all-in-one platform that helps you create and sell courses online, and handles everything from web hosting to payment processing, I was able to complete this project and create Book reviews application. His extensive professional and academic knowledge was support needed during this period._**

### **Project requirements**

- HTML, CSS, and JavaScript frontend with a Rails API backend. All interactions between the client and the server required to be handled asynchronously (AJAX) and use JSON as the communication format.
- It needed to organize data through Object Oriented JavaScript (classes) to encapsulate related data and behavior, and domain model served by the Rails backend must include a resource with at least one has-many relationship.
- The backend and frontend must collaborate to demonstrate Client-Server Communication.
- Application should have at least 3 AJAX calls, covering at least 2 of Create, Read, Update, and Delete (CRUD).
- Client-side JavaScript code must use fetch with the appropriate HTTP verb, and your Rails API should use RESTful conventions.

**Language and skills implemented**

Project was build using a Rails API for the backend and JavaScript for the frontend. Toolset included Visual Studio Code (editor/terminal), GitHub and Postgres for database.

### Rails API Backend

The Rails component of this project is very straightforward with a Book and Review models and associations.

**Setting up:**  **Models & Controllers**

```
class Book < ApplicationRecord
    has\_many :reviews
end

class Review < ApplicationRecord
    belongs\_to :book
end

class ReviewsController < ApplicationController
  def index
    reviews = Review.all
    render json: ReviewSerializer.new(reviews)
  end

  def create
    new\_review = Review.new(review\_params)
      if new\_review.save
        render json: ReviewSerializer.new(new\_review)
      else
        render json: new\_review.errors
      end
  end
  private 
  def review\_params
    params.require(:review).permit(:description)
  end
end
```

### Building out the frontend

![](https://cdn-images-1.medium.com/max/1024/0*V3sGDPxHkE7CGJqE.jpg)

We have index.js file with functional programming and Adapters and respective Classes for our models and reviews set like this:

![](https://cdn-images-1.medium.com/max/1024/0*Sxey_-vW35a3H2pO.jpg)

EventListener and handleMenuClick function is set to determine if target is anything besides the menus and I create callback object that has key value pairs and if we matched the targets with the keys in the object, we can essentially pull out that function from the object and invoke it:

```
function handleMenuClick(event){
  if (event.target.id !== menu){
    main.innerHTML = ``
    callbacks[`${event.target.id}`]()
  }
} 

const callbacks = {
  allBooks: renderAllBooks,
  booksReviews: renderAllBooksReviews,
  newBook: renderNewBookForm,
  newReview: renderNewReviewForm
}
```

To learn more check my [Github](https://github.com/ivanadokic/book-reviews) or connect with me on [LinkedIn](https://www.linkedin.com/in/ivana-dokic-b96460120/) or [Twitter](https://twitter.com/LloydPile).

_Originally published at_ [_https://ivanadokic.github.io_](https://ivanadokic.github.io/java_srcipt_app_-_book_reviews) _on May 17, 2020._