From b6cb9157ed65ad0e5df0fb89a85adc0eac9db568 Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Fri, 5 Mar 2021 14:59:11 +0100 Subject: [PATCH] Added type for celestial bodies w/ icons --- src/components/celestials/CelestialCard.vue | 102 ++++++++++++++++++- src/components/celestials/CelestialsList.vue | 13 +++ src/views/celestials/Celestials.vue | 48 ++++++++- 3 files changed, 158 insertions(+), 5 deletions(-) diff --git a/src/components/celestials/CelestialCard.vue b/src/components/celestials/CelestialCard.vue index 4380923..11dbeb4 100644 --- a/src/components/celestials/CelestialCard.vue +++ b/src/components/celestials/CelestialCard.vue @@ -1,6 +1,60 @@ - {{ celestial.name }} + + + + + + + + + + + {{ celestial.name }} + + Type : + {{ celestial.type }} + + + + + + Facteur Gravité : {{ celestial.gravity }} + + + Facteur Densité : {{ celestial.density }} + + + + + + + Détails + + @@ -9,8 +63,54 @@ import { defineComponent } from "vue"; export default defineComponent({ name: "celestial-card", + data() { + return { + type: "unknown" + }; + }, props: { celestial: Object + }, + mounted() { + console.log(this.celestial); } }); + + diff --git a/src/components/celestials/CelestialsList.vue b/src/components/celestials/CelestialsList.vue index 856a649..a84c21d 100644 --- a/src/components/celestials/CelestialsList.vue +++ b/src/components/celestials/CelestialsList.vue @@ -29,3 +29,16 @@ export default defineComponent({ } }); + + diff --git a/src/views/celestials/Celestials.vue b/src/views/celestials/Celestials.vue index 63bdc2d..95b72ff 100644 --- a/src/views/celestials/Celestials.vue +++ b/src/views/celestials/Celestials.vue @@ -20,12 +20,26 @@ export default defineComponent({ components: { CelestialsList }, + + // Initial state data() { return { celestials: [] }; }, + + async mounted() { + // Fetches from API... + this.fetchBodies().then(res => { + // ...and add type + this.celestials = this.addType(res); + }); + }, + methods: { + /** + * Fetches celestial bodies from API + */ fetchBodies() { return axios .get("https://api.le-systeme-solaire.net/rest/bodies/") @@ -36,10 +50,36 @@ export default defineComponent({ console.log(err); return []; }); + }, + + /** + * Assign a type from the celestial object provided + */ + addType(bodies: []) { + return bodies.filter((e: any) => { + // Check if element is planet + if (e.isPlanet) { + if (e.moons) { + e.type = "planète à lunes"; + } else { + e.type = "planète"; + } + + // Check if element is moon + } else if (e.aroundPlanet != null) { + e.type = "lune"; + + // Check if element is star + } else if (e.id === "soleil") { + e.type = "étoile"; + + // ...else, body is "other" + } else { + e.type = "autre"; + } + return e; + }); } - }, - async mounted() { - this.celestials = await this.fetchBodies(); } }); @@ -47,7 +87,7 @@ export default defineComponent({
+ Type : + {{ celestial.type }} +
Facteur Gravité : {{ celestial.gravity }}
Facteur Densité : {{ celestial.density }}