How to automate your portfolio website [Part 1]

Ahmed Altaai
6 min readOct 14, 2020

To have less steps redacting the portfolio every time you publish a blog or push a new project to GitHub, automating it is the right choice.

In this article, I’d like to share how you can automate your portfolio website with the help of some APIs, let me show you how I did it.

  • Automatically display and link my projects from GitHub
  • Automatically display and link my blogs from Hashnode

The steps I took

First of all, I was messing around with the github API and have created a user search application with vuejs. Then the idea struck me! If I can search for users and display them this way, there must be a way to automatically display all the repositories on my website.

So I started looking through the API links until I found what was needed. Afterwards I looked into the Hashnode API which will be explained in the next article so stay tuned.

The GitHub API

Preparing the fetch request

This link will be prepared so we can fetch all the public repositories of the chosen profile.

"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}"

All the work is happening inside a component

Requesting the repositories from the GitHub API with the JavaScript fetch API when the the instance is created.

created() { fetch('https://api.github.com/users/ahmedaltaai/repos') .then( res.json()) .then( console.log(data) }) }

The response

Afterwards

Analise the returned data by clicking on the triangle next to the indexed links to display the information within the object which is needed to know which data is being returned and which to use for displaying on the website.

I placed a red dot next to the information which I want to display:

  • Name
  • Language
  • Description
  • Repository URL
  • URL of the project for live preview

Now lets store all the information.

Storing the response into an array

To be able to store the data we first need to create an empty array.

Then assign the data to the array.

Populating the empty array which was created prior

created() { fetch('https://api.github.com/users/ahmedaltaai/repos') .then( res.json()) .then( this.repos = data }) }

Looping over the repos

We can use the v-for directive to render a list of items based on an array. The v-for directive requires a special syntax in the form of item in items, where items is the source data array and item is an alias for the array element being iterated on.

v-for="(repo, i) in repos" :key="i"

To give Vue a hint so that it can track each node’s identity, and thus reuse and reorder existing elements, you need to provide a unique key attribute for each item.

This is how my component looks like. I will spare the styling which you can find in my GitHub repo

This is how it looks like in the browser

As you can see it is returning every public repository which I don’t want it to; so I will use an if-statement and disclose that it should show only the repos which have a homepage link them.

This is a common scenario as you can imagine, wanting to conditionally loop through objects. Only actually running the loop on objects that match a given criteria. The question is how do you do it?

This is how to do it

Place this if statement after the v-for loop so it has access to the individual item and can figure out if repo.homepage is true or false

v-if=”repo.homepage”

We want to loop through this repos array using the v-for directive. With each loop iteration we display another repo. But we only want to loop through repos which have a link to a live demo. If a repo doesn’t have anything inside the repo.homepage we basically want to skip over it.

This means if homepage in the individual repo is true then show me the requested details as of name, description and so on.

The result of the if-statement

As you can see this would technically work. But we now have empty div elements around each object which can likely interfere with DOM traversal and phantom CSS styling. In addition, it clutters our app and adds unnecessary structure to our HTML markup.

I should also mention that you are still looping through unwanted repos and running a conditional before breaking and going to the next loop iteration. This might solve the problem visually, but computationally we are still looping through every single repo in the system instead of only the needed ones. Depending on the size of the repos payload, this could lead to performance issues.

The solution: computed properties

Instead of looping through all of the repo objects and then checking mid-loop if they meet our criteria, we could just create a subset of our repos, which only represents the wanted repos, with homepage link, and loop only that subset!

We have the repos as an array so we can use the .filter() method to only return the wanted repositories.

v-for="(repo, i) in wantedRepos" :key="i"

The new outcome with computed properties

90% of the work is done. There is one more thing I want to add to the cards I want to add colored language tags the same as on GitHub.

example

You may be asking: well how can I add a description and a homepage link to my repositories.

Say no more

  • Go to the desired repository
  • Click the cog icon

First of all put a class or id on the languages element. I used a class and named it ‘lang’

I believe everyone is going to have multiple repositories to portray so when you grab the element of the language e.g. class of ‘lang’ there will be multiple classes with the same value because every repo card has its own language element. So we need to grab them all.

The colored dot is styled in the CSS section and we are only adding the background-color to it.

I grabbed the colors from GitHub with the help of dev-tools inspector.

Congratulations on automating your projects section on your portfolio homepage.

Next: Hashnode API implementation so stay tuned and follow me to not miss it out!

Originally published at https://ahmeds.tech.

--

--