Skip to content

Instantly share code, notes, and snippets.

@dabernathy89
Created December 1, 2022 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabernathy89/ff3f9873c7611ab3d1fb549a1663c98f to your computer and use it in GitHub Desktop.
Save dabernathy89/ff3f9873c7611ab3d1fb549a1663c98f to your computer and use it in GitHub Desktop.
ChatGPT

Prompt: Write a Vue component that displays the most popular repositories from Github

Here is an example of a Vue component that displays the most popular repositories from Github:

<template>
  <div>
    <h1>Popular Github Repositories</h1>
    <ul>
      <li v-for="repo in repositories" :key="repo.id">
        {{ repo.name }}
      </li>
    </ul>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'PopularRepos',
  data() {
    return {
      repositories: [],
    };
  },
  created() {
    axios
      .get('https://api.github.com/repositories')
      .then(response => {
        this.repositories = response.data;
      });
  },
};
</script>

This component uses the axios library to make an HTTP request to the Github API to retrieve a list of the most popular repositories. It then displays this list using a v-for directive in the template.

Note: In the real world, you would need to include authentication information, such as a personal access token, in the request to the Github API in order to access the data. You would also need to handle potential errors that may occur while making the request.

Prompt: Refactor the code to Vue 3 using the composition API syntax and TypeScript

Here is an example of a Vue 3 component that displays the most popular repositories from Github using the composition API syntax and TypeScript:

<template>
  <div>
    <h1>Popular Github Repositories</h1>
    <ul>
      <li v-for="repo in repositories" :key="repo.id">
        {{ repo.name }}
      </li>
    </ul>
  </div>
</template>

<script lang="ts">
import axios from 'axios';
import { defineComponent, reactive } from '@vue/composition-api';

export default defineComponent({
  name: 'PopularRepos',
  setup() {
    const state = reactive({
      repositories: [],
    });

    axios
      .get('https://api.github.com/repositories')
      .then(response => {
        state.repositories = response.data;
      });

    return {
      repositories: state.repositories,
    };
  },
});
</script>

This component uses the axios library to make an HTTP request to the Github API to retrieve a list of the most popular repositories. It then displays this list using a v-for directive in the template.

The component uses the composition API syntax and TypeScript to define its state and behavior. The reactive function is used to create a reactive state object, and the returned object is used in the component's template.

Note: In the real world, you would need to include authentication information, such as a personal access token, in the request to the Github API in order to access the data. You would also need to handle potential errors that may occur while making the request.

Prompt: Add a fontawesome icon to each repository based on its keywords

Here is an example of a Vue 3 component that displays the most popular repositories from Github and includes a fontawesome icon for each repository based on its keywords:

<template>
  <div>
    <h1>Popular Github Repositories</h1>
    <ul>
      <li v-for="repo in repositories" :key="repo.id">
        <i :class="`fas fa-${getIconForRepo(repo)}`"></i>
        {{ repo.name }}
      </li>
    </ul>
  </div>
</template>

<script lang="ts">
import axios from 'axios';
import { defineComponent, reactive } from '@vue/composition-api';

export default defineComponent({
  name: 'PopularRepos',
  setup() {
    const state = reactive({
      repositories: [],
    });

    axios
      .get('https://api.github.com/repositories')
      .then(response => {
        state.repositories = response.data;
      });

    // Returns a fontawesome icon name based on the keywords
    // of the given repository.
    function getIconForRepo(repo: any) {
      if (repo.keywords.includes('vue')) return 'vuejs';
      if (repo.keywords.includes('react')) return 'react';
      if (repo.keywords.includes('angular')) return 'angular';
      return 'code';
    }

    return {
      repositories: state.repositories,
      getIconForRepo,
    };
  },
});
</script>

This component uses the axios library to make an HTTP request to the Github API to retrieve a list of the most popular repositories. It then displays this list using a v-for directive in the template, and includes a fontawesome icon for each repository using the getIconForRepo method.

The component uses the composition API syntax and TypeScript to define its state and behavior. The reactive function is used to create a reactive state object, and the returned object is used in the component's template.

Note: In the real world, you would need to include authentication information, such as a personal access token, in the request to the Github API in order to access the data. You would also need to handle potential errors that may occur while making the request.

Prompt: show more details about each repository when its name is clicked

Here is an example of a Vue 3 component that displays the most popular repositories from Github and includes more details about each repository when its name is clicked:

<template>
  <div>
    <h1>Popular Github Repositories</h1>
    <ul>
      <li v-for="repo in repositories" :key="repo.id">
        <i :class="`fas fa-${getIconForRepo(repo)}`"></i>
        <span @click="showRepoDetails(repo)">{{ repo.name }}</span>
        <div v-if="selectedRepo === repo">
          <p>{{ repo.description }}</p>
          <p>Stars: {{ repo.stars }}</p>
          <a :href="repo.url" target="_blank">View on Github</a>
        </div>
      </li>
    </ul>
  </div>
</template>

<script lang="ts">
import axios from 'axios';
import { defineComponent, reactive } from '@vue/composition-api';

export default defineComponent({
  name: 'PopularRepos',
  setup() {
    const state = reactive({
      repositories: [],
      selectedRepo: null,
    });

    axios
      .get('https://api.github.com/repositories')
      .then(response => {
        state.repositories = response.data;
      });

    // Returns a fontawesome icon name based on the keywords
    // of the given repository.
    function getIconForRepo(repo: any) {
      if (repo.keywords.includes('vue')) return 'vuejs';
      if (repo.keywords.includes('react')) return 'react';
      if (repo.keywords.includes('angular')) return 'angular';
      return 'code';
    }

    function showRepoDetails(repo: any) {
      state.selectedRepo = repo;
    }

    return {
      repositories: state.repositories,
      selectedRepo: state.selectedRepo,
      getIconForRepo,
      showRepoDetails,
    };
  },
});
</script>

This component uses the axios library to make an HTTP request to the Github API to retrieve a list of the most popular repositories. It then displays this list using a v-for directive in the template, and includes a fontawesome icon for each repository using the getIconForRepo method. When the name of a repository is clicked, the showRepoDetails method is called, which sets the selectedRepo state variable to the selected repository. This causes the template to display more details about the selected repository, such as its description, number of stars, and a link to the repository on Github.

The component uses the composition API syntax and TypeScript to define its state and behavior. The reactive function is used to create a reactive state object, and the returned object is used in the component's template.

Note: In the real world, you would need to include authentication information, such as a personal access token, in the request to the Github API in order to access the data. You would also need to handle potential

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment