Skip to content

Commit

Permalink
Add badge to new champion (#4)
Browse files Browse the repository at this point in the history
Add badge to new champion
  • Loading branch information
kristiandrex authored Jul 23, 2021
2 parents c4b5691 + 48b6d9e commit 6640fba
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "league-of-react",
"version": "3.1.2",
"version": "3.2.0",
"private": true,
"dependencies": {
"@actions/core": "^1.4.0",
Expand Down
2 changes: 1 addition & 1 deletion public/data/latest.json

Large diffs are not rendered by default.

88 changes: 60 additions & 28 deletions scripts/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,86 @@ const fs = require("fs");
const fetch = require("node-fetch");
const core = require("@actions/core");

async function getLatestVersion() {
async function getVersions() {
const url = "https://ddragon.leagueoflegends.com/api/versions.json";
const response = await fetch(url);
const versions = await response.json();
return versions[0];

return {
latest: versions[0],
previous: versions[1]
};
}

getLatestVersion()
.then(async (latestVersion) => {
if (!fs.existsSync("public/data/version.txt")) {
return await download(latestVersion);
getVersions()
.then(async (versions) => {
const latestVersion = versions.latest;
const versionFile = "public/data/version.txt";

if (!fs.existsSync(versionFile)) {
return await download(versions);
}

const currentVersion = fs
.readFileSync("public/data/version.txt")
.toString();
const currentVersion = fs.readFileSync(versionFile).toString();

if (latestVersion === currentVersion) {
core.setOutput("should-update", false);
console.log(`Version ${latestVersion} it's already downloaded.`);
return;
if (latestVersion !== currentVersion) {
return await download(versions);
}

await download(latestVersion);
core.setOutput("should-update", false);
console.log(`Version ${latestVersion} it's already downloaded.`);
})
.catch((error) => {
console.error("There was an error");
console.error(error);
});

async function download(version) {
console.log(`Downloading version ${version}...`);
async function download(versions) {
const latestVersion = versions.latest;

const url = `http://ddragon.leagueoflegends.com/cdn/${version}/data/es_MX/champion.json`;
const response = await fetch(url);
const json = await response.json();
console.log(`Downloading version ${latestVersion}...`);

const data = JSON.stringify({
version,
champions: Object.values(json.data)
});
const latestData = await fetchVersion(latestVersion);
const previousData = await fetchVersion(versions.previous);

const newChampion = latestData.champions.find(
(champion, index) => previousData.champions[index]?.id !== champion.id
);

const modifiedChampions = latestData.champions.map((champion) => ({
new: champion.id === newChampion?.id,
...champion
}));

latestData.champions = modifiedChampions;

await fs.promises.writeFile("public/data/latest.json", data);
await fs.promises.writeFile("public/data/version.txt", version);
await fs.promises.writeFile(
"public/data/latest.json",
JSON.stringify(latestData)
);

// Adding latest version to Github Actions output.
core.setOutput("latest-version", version);
await fs.promises.writeFile("public/data/version.txt", latestVersion);

// Add latest version to Github Actions output.
core.setOutput("latest-version", latestVersion);
core.setOutput("should-update", true);

console.log(`Version ${version} successfully downloaded.`);
console.log(`Version ${latestVersion} successfully downloaded.`);
}

function getVersionUrl(version) {
return `http://ddragon.leagueoflegends.com/cdn/${version}/data/es_MX/champion.json`;
}

function formatData(payload) {
return {
version: payload.version,
champions: Object.values(payload.data)
};
}

async function fetchVersion(version) {
const response = await fetch(getVersionUrl(version));
const json = await response.json();
return formatData(json);
}
1 change: 1 addition & 0 deletions src/components/Thumbnail.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function Thumbnail({ champion }) {
<div className="thumbnail">
<Link href={`/champions/${champion.id}`}>
<a>
{champion.new && <span className="badge">NUEVO</span>}
<Image
src={`https://ddragon.leagueoflegends.com/cdn/${champion.version}/img/champion/${champion.image.full}`}
alt={champion.name}
Expand Down
1 change: 1 addition & 0 deletions src/pages/champions/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function Champion({ champion }) {
<span className="title">{champion.title}</span>
</div>
<div className="images">
{champion.new && <span className="badge">NUEVO</span>}
<img
src={`http://ddragon.leagueoflegends.com/cdn/img/champion/loading/${champion.id}_0.jpg`}
alt={champion.name}
Expand Down
11 changes: 11 additions & 0 deletions src/styles/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ svg {
width: 24px;
}

.badge {
background-color: var(--YELLOW);
font-weight: bold;
padding: 0.25rem 0.5rem;
font-size: 0.85rem;
border-radius: 0.5rem 0 0 0;
position: absolute;
z-index: 1;
}

nav {
align-items: center;
background-color: var(--YELLOW);
Expand Down Expand Up @@ -116,6 +126,7 @@ main .grid {
cursor: pointer;
overflow: hidden;
text-align: center;
position: relative;
}

.thumbnail img {
Expand Down

1 comment on commit 6640fba

@vercel
Copy link

@vercel vercel bot commented on 6640fba Jul 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.