Ninja Space Content Tech Blog

Week Nine of Learning Angular

August 4, 2021
Oh my goodness, I am so tired this morning. I only had 2 hours of sleep!

My contracting work is slow this week so I am getting back to Learning Angular with Mosh (self-paced tutorial) again. I am 34% of the way. I know you're probably wondering why it is taking me so long to finish! As mentioned in my other weekly blog recaps with learning notes for Angular, it's because it takes me a long time to debug version differences as I am running a new version of Angular, whereas Mosh's tutorial is teaching with a much older version. I'm coding along while I'm listening to his videos.

Optimistic vs Pessimistic Updates
Modern updates utilizes Optimistic Updates as it'll render changes faster, which is where you make the changes like using a splice method before you make a call to an API server.

Observables vs Promises
For Angular, you use can Observables async operations. The async calls will only happen when you call subscribe with an observable. Observable are lazy and promises are eager and nothing happens until you subscribe with observable. You can always change observable into a promise but Mosh doesn't recommend it. You also need .then for observables whereas, you don't need a .then with promises.

The cool thing about observables is you can use something like the .retry(3) operator. You mainly use .map and .catch for observables. You can also chain events before subscribing when using observables.
 

create-react-app and Redux for React Apps Plus Safari Blank Issue

July 21, 2021
I've been creating react apps since October 2020 so it's only 9 months and I've only worked with redux for one of my projects so far and I want to work on another project with redux. For this blog post, I'll be highlighting on creating my new react app with create-react-app and redux implementation.

Through the coding bootcamp, I would just "npm install react react-dom react-scripts" in my command line, and "mkdir" my src and public folders and corresponding files to start my react projects. It's the manual way to do it without the extra stuff. I am glad they taught me this way first. This year, I've created two new post-bootcamp projects this way as well.

Today, I am finally creating a "create-react-app" but first in my command line I "sudo npm install -g create-react-app" on my computer this morning to install this globally onto my machine. Then, I made a new folder and in it using the terminal, I typed the following in the terminal: "create-react-app lipstickstain" and as it was installing a lot of files, my MacBook started to get hot right away. I call it "the take-off" as it sounded like being in a plane that's about to take off when it starts running this hot. I had to close my MacBook for awhile to let it cool down.

I use Node so you'll see my terminal commands have "npm" in it throughout my blog post below.

After Cool Down
Alright, I'm back at it again after it cooled down. I've dabbled in redux earlier this year as I was self-teaching myself and have just been learning it through youtube tutorials. However, it's been a few months since so I am going to relearn it again. I'm following along the "Redux Crash Course With React" from Traversy Media's Youtube Channel. As recommended, I'm also installing ES7 React/Redux/GraphQL/React-Native Snippet extension for my VSC.

Coding Steps
  1. After I've created a new react app with the "create-react-app" command. I created a 'component' folder under the 'src' folder. Then inside the 'component' folder, I created two files: 'Posts.js' and 'Postform.js'.
  2. Shortcuts >>> You can type rcc and then the 'tab' button to quickly get a class component set up or rfc and then the 'tab' button to get a function set-up quickly.
  3. React Legacy Update >>> The video said to use "componentWillMount" but that is now legacy.
  4. When you are ready to maintain state, implement redux and install their libraries so "npm i redux react-redux redux-thunk"
  5. To use redux, I created 2 new folders: actions and reducers. Then, I created the following 5 new files: postActions.js and types.js for 'actions' folder, index.js and postReducer.js for 'reducers' folder and a store.js file
  6. Extra Important Notes:
    • You will need to use Provider in this, which is a react component. It allows us to use redux and react together. You'll wrap <Provider> around the entire app.
    • To see the state in the redux extension tools, remember to add the following: + window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() to the createStore function. You will need to import "compose" as well. Here is how my entire store.js file looks like with this implementation.
    It really seems like I had to create a lot of additional files to just to use redux instead of just using react hooks, but I know the developing world demands it, especially for state management.

    I also want to note that other functions had to be imported because you have to create the store and make sure you connect react to redux.

Big issue:
I had the biggest issue when I was deploying my app onto AWS Amplify! My implementation with redux was not working on Safari so it would just show a blank page. I was exclusively testing on Google Chrome so I didn't pay attention to it very much but after an hour of looking it up, it had to do with my middleware. I had to install the 'redux-devtools-extension' library and implement it to my store. Here is what I had to update to in my store.js file: 
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';
import { composeWithDevTools } from 'redux-devtools-extension';
const composedEnhancer = composeWithDevTools(applyMiddleware(thunk))
const initialState = {};

const store = createStore(
  rootReducer, 
  initialState, 
  composedEnhancer
);

export default store;

Another Big Issue:
I ran into another deployed issue. When you are fetching data while under development on localhost, the browser doesn't throw an error when the route is http versus https. However, upon deployment, this is very crucial. Data will not pull or fetch if you accidentally forgot the 's' in https in your fetch URL from Heroku (which is where I deployed my API).


Another, Another Big Issue:
After fixing the previous two issues, I still wasn't able to pull data on deployment. On localhost, it was working fine. It ended up being an issue with cors. I use Express and Node and this was my back-end issue. Here is how my updated access control code looks like in my back end to fix the issue: 
server.all('*', (req, res, next) => {
 res.header("Access-Control-Allow-Origin", "*");
 next();
});

Finally
My app is now running as expected on my deployed link from AWS Amplify and no longer has a blank page on Safari, data is fetched successfully and no more scary cors red errors in the console! Phew! I hope these notes will help you debug if you run into the same issues as me. Now, it's time to beef up my content and grow my database by using the post form from my front-end UI. Then, I will think of a strategy to promote it somehow. Maybe though social media ads? 
 

Week Eight of Learning Angular

July 11, 2021
This is my Week 8 of learning Angular. Sorry, I went off track for awhile. It's been 2 months since I wrote up my notes for Week 7. (See all of my learning notes for Angular.) I picked up a contract job towards the end of May and that had been my focus. This week I am focusing on error handling in Angular.

  1. Use onInit method to call http services and constructor should not call http services. Oninit is a lifestyle hook. When we use the ng onInit method, we tell the compiler about it.
  2. We want to implement Separate of Concerns. For example, don't include delete responsibility in the same class or service of a new post because it is hard to maintain and test.
  3. To create a new service: ng g s post
  4. Handling Errors Types:
    1. Server is offline
    2. Network is down
    3. Unhandled exceptions
    4. "Not found" errors (404) -- expected error
    5. "Bad request" errors (400) -- expected error
So much has changed with handling errors so my code is noticeably different from what Mosh has in his tutorial due to certain things be deprecated. So, here is my code for my file name "post.service.ts". You can use it as a reference if you're also using Angular version 11+ like me.
import {HttpClient} from '@angular/common/http';
import { Injectable } from '@angular/core';

import {Observable, throwError} from 'rxjs';
import {catchError} from 'rxjs/operators';
import { AppError } from '../common/app-error';
import { NotFoundError } from '../common/not-found-error';

@Injectable({
 providedIn: 'root'
})
export class PostService {
 private url = 'http://jsonplaceholder.typicode.com/posts'

 constructor(private http: HttpClient) { }
 getPost() {
  return this.http.get(this.url);
 }

 createPost(post: any) {
  return this.http.post(this.url, JSON.stringify(post))
 }

 updatePost(post: any) {
  return this.http.patch(this.url + '/' + post.id, JSON.stringify({isRead: true}))
 }

 deletePost(id: number) {
  return this.http.delete(this.url + '/' + id)
  .pipe(catchError(error => {
   if (error.status === 404) 
    return throwError(new NotFoundError());
    
   return throwError(new AppError(error));
  })) 
 }
}

 

Connecting Facebook to Yola CMS

May 29, 2021
Today, I'm focusing on adding another way for my visitors to be able to visit my websites without leaving my Facebook pages if they're already on there. I'm a Yola CMS client and there's a tool under my Yola log-in that allows me to add my website that I published through them as a tab on my Facebook page. I'm adding this Facebook publishing integration to a few of my Yola sites. 

As part of my marketing strategy, I always create an associated Facebook page for each website so that there could be some cross-marketing. (Read more about how I leverage free social media marketing to grow my traffic.) I've seen in my Google Analytics reports that every time I post something on social media and include a link in the post to my website, web traffic definitely shows a bit of a spike and occasionally, a big spike. So, I'm going to try this Facebook publishing feature where it adds a new tab on my Facebook page and allow visitors to go on my "Website" without leaving Facebook.

I can only add the Facebook publishing feature to Facebook pages that have lesser likes and followers because it currently only works in an older version of Facebook pages.

I learned that I couldn't implement it on my Facebook page that has over 1,800 likers/followers because Facebook has just updated my higher trafficked Facebook page to their latest version while keeping my less popular Facebook pages on the older version; and Yola's Facebook publishing feature doesn't work on the latest version.

STEPS for Yola Facebook Publishing:

Yola has the Facebook publishing feature under their "Settings". It is turnkey.

  1. Go to "Settings" of your working site. Then, go to "Publishing" on the left side bar. At the bottom of the page, click on "Connect with Facebook". 
  2. Select the Facebook page you'd like to use for this particular Yola site. If you've configured a facebook publishing feature for a different Yola site for a Facebook page before, hit "Edit Settings" instead of "Continue as..." of your account (this will open a new tab or window). Select the exact Facebook page you'd like to use for your particular account (this is for those who have many Facebook pages under one Facebook account), then toggle "Yes" to allow Yola Website Builder to manage your Facebook page. 
  3. Then go back to the original tab of your "Publishing" settings. You'll see a radio button to select that exact Facebook page to allow the "Website" tab to show up on your Facebook page. You'll notice that before you did this, a "Website" tab was not seen from your list of tabs on your Facebook page. After a few minutes, you should see it as Yola has done this for you automatically since you've allowed them to publish it on your behalf. It is pretty cool. 

I would move the "Website" tab on my Facebook page up once it's propagated so that it's on the second tab. That way, visitors can see it when they go on the Facebook page. This will increase awareness and make it more apparent to visitors. See my screenshot below of one of my Facebook pages with this "Website" tab below. 

Importantly, you can only view this "Website" tab on the desktop version of Facebook and not the mobile versions of the Facebook pages.

You can also view Yola's detailed instructions and Q & As on enabling the Facebook Publishing feature for your Yola account.
 

Adding a Sitemap to Godaddy Shared Hosting

May 22, 2021
On May 9th, 2021, I added a sitemap.txt file to one of my React apps so that it can get properly indexed in the search engine and I can start seeing stats on Google's search console. I am current hosting this app on Godaddy's shared hosting. 

Today, a couple of weeks later, I noticed that I am not showing hits on certain pages; just on the homepage. So, I went on my app and utilized real time tracking and I realized Google Analytics doesn't pick it up even in real-time. I figured this may have something to do with my sitemap submission.

To submit a sitemap to Godaddy shared hosting, I just went into the cPanel, went to the public_html and added my sitemap.txt file with a list of the pages I want to index, line by line.

Now, since not all of the pages are being indexed properly, I went into my search console and noticed that it excluded these subpages for some unknown reason. Amazingly, Google Search Console has a URL inspection tool where I can go into each of the links they excluded and request that Google indexes them by clicking on "REQUEST INDEXING" link. Within a couple of minutes, I'm able to see the other pages show up when I am on them via Google Analytics so it was a very easy process! Just watch out for your indexing quota.
 

About Ninja Space Content


Ninja Space Content I have been building simple websites on my own since 2008 and currently run several websites at the moment, including this one. I used to be an account manager for an affiliate/e-commerce company, helping affiliates grow their sales so I have some knowledge on the business side and client side of affiliate marketing. During the Covid-19 pandemic, I completed a JavaScript coding bootcamp and graduated in Dec 2020. I've been working as a contractor for a few software companies ever since.
Note: links to resources and promoting special deals may allow me to earn a small commission from each sale.