This APP uses a Rails API back-end and a JavaScript front-end using the fetch()
method to fetch the back-end, gets the JSON uses the json()
method to instantiate javaScript Objects and saves them in Recipe.all
and Comment.all
so it won't hit the API for the next request. This APP follows the concerns principal such as request related to Json API
requests are happenning in the Api class
, rendering markup texts
, logic statements
not related to DOM
manupulations, instatiating new Object
instances, saving them in a class variable happen in the Recipe
and Comment
classes. Everything related to DOM
manupulation happen in the events liteners
.
This APP has users authentications
via devise gem
and to fully use the APP you would have to create and account follow below
. This APP allows users to create FOOD RECIPES to be shared among others users, leave COMMENTS...
- ruby 2.6.1
gem "devise", "~> 4.7"
gem "fast_jsonapi", "~> 1.5"
gem "pry", "~> 0.12.2"
gem "typhoeus", "~> 1.3"
gem 'rails', '~> 6.0.2', '>= 6.0.2.1'
- Fork and clone this repository and then run
- run
yarn install --check-files
to update yarn - run
rails db:migrate
to create the migrations - run
rails s
to start the rails server - head to
http://localhost:3000/
on your browser and click onsignup
to create a new account - head back to your terminal and run
rails db:seed
to seed the database - Go back to your browser and you should see new records of Recipes.
let BASE_URL = "http://localhost:3000"
- Checks if
Recipe.all === 0
before calling onApi.fetchRecipes()
to instantiatenew instances of recipes
saves them inRecipe.all
giving the program some flexibility.
- Calls on
Api.fetchToCreateRecipes(recipeAttributes)
chaining with a.then
and uses thejson
from the backend API to createnew Recipes
and saves them inRecipe.all
through thesave()
instance method
- Saves
this
as a new instance of recipe inRecipe.all
and returns it
- Checks to see if a
user
issigned in
or not, then decides of themarkup
to be rendered.
- Calls on
Api.fetchToUpdateRecipes(recipe)
uses thejson
from the backend to update thenew Recipe
, mapping over theRecipe.all
and setting anif (recipe.id === json.id)
to avoid duplications, the return value of this function will be the updated recipepromise
.
-- Checks if this.comments.length === 0
before calling on Api.fetchRecipeShow(this.id)
to hit the backend Rails API, recicpe has_many comments
allows me to pass the recipe id in this case this.id
finds the recipe comments or create them returning a resolved promise
- Uses the
filter
method and returns allcomments
related to the matchingrecipe id
passed in the block
- Checks to see if a
user
issigned in
or not, finds therecipe comments
iterate over them and then decides of themarkup
to be rendered.
- Render the
show page markup
- Render the
form markup
to create new recipes
- Render the
form markup
to update recipes
- Finds a recipe by a given
id
and return it
- In javaScript a class variable is defined outside of the class
- Calls on
Api.fetchToCreateComments(commentAttributes)
chaining with a.then
and uses thejson
from the backend API to createnew Comment
and saves them inComment.all
through thesave()
instance method
- Finds a comment by a given
attributes id
to return or create anew Comment
with thoseattributes
and saves them inComment.all
- Render the
comment show page markup
- Saves
this
as a new instance inComment.all
and always returnsthis
- In javaScript a class variable is defined outside of the class
Fetches
the${BASE_URL}/recipes
hits therecipes index controller action
if resolved it will return a destruturedpromise
Fetches
the${BASE_URL}/recipes/${id}
from the id parameter hits therecipes controller show action
if resolved it will return a destruturedpromise
of the passed in id
Fetches
the${BASE_URL}/recipes
giving thefetch
method a second argument with aPOST
method aX-CSRF-Token
in theheaders
to validate the authenticity of the request, hits therecipes controller create action
if valid, will persist the data and then returns a destructuredpromise
for the frontend use
Fetches
the${BASE_URL}/recipes/${recipe.id}
by addind the recipe object id to the url, giving thefetch
method a second argument with aPATCH
method including aX-CSRF-Token
in theheaders
to validate the authenticity of the request, hits therecipes controller update action
updates the record and returns a destructuredpromise
.
Fetches
the${BASE_URL}/recipes/${recipeId}
by adding the recipe id to the Url, with a second argument to thefetch
method ofDELETE
including aX-CSRF-Token
in theheaders
to validate the authenticity of the request, hits thedestroy action
and then returns ajson response
and usesjson()
to extract the actual json and return thepromise
Fetches
the${BASE_URL}/comments
with a second argument of aPOST
method andX-CSRF-Token
in theheaders
to validate the authenticity of the request, hits thecomments controller create action
if valid, will persist the data and then returns a destructuredpromise
for the frontend use
- Takes in 2 arguments the
recipeId
that we want todelete
thecomment
from and thecommentId
that is being deleted fetches the${BASE_URL}/recipes/${recipeId}/comments/${commentId}
with a second argument ofDELETE
method including aX-CSRF-Token
in theheaders
to validate the authenticity of the request, hits thedestroy action
and then returns ajson response
and usesjson()
to extract the actual json and return thepromise
- Is making sure the initial
HTML
is fully loaded and parsed
- Adding
click
events liteners responding to a button click deciding what action is being required and how to manupulate theDOM
e.preventDefault()
to prevent a normal request cycle when submitting to create a new record.- adding submit events listeners to create new instances of objects via users interactions and manipulate the
DOM