getContribution.js 1.16 KB
const userContributionsBox = document.querySelector(".user-status__contributions");
const totalContributionIndicator = document.getElementById("jsTotalContributions");
const URL = "https://api.github.com/graphql"


// const handleImage = () =>{
//     const total = totalContributionIndicator.innerText;
//     console.log(total);
// };

const handleContributons = (total) =>{
    totalContributionIndicator.appendChild(total);
};

const getGithubRepo = () =>{
    const token = process.env.GH_SECRET_SH;
    const username = user.username
    const headers = {
        'Authorization': `bearer ${token}`,
    };
    const body = {
      "query": `query {user(login: "${username}") {contributionsCollection {contributionCalendar {totalContributions}}}}`
    };
    const response = fetch(URL, { method: "POST", body: JSON.stringify(body), headers: headers }).then(function(response){
        return response.json();
    }).then(function(res){
        const total = res.data.user.contributionsCollection.contributionCalendar.totalContributions;
        handleContributons(total);
    });
};

const init=()=>{
    handleImage();
    getGithubRepo();
};

if(userContributionsBox){
    init();
};