Refactored Celestials vue to use the composition API (just a test)

This commit is contained in:
Alexis
2021-03-09 17:43:39 +01:00
parent 69f15c787f
commit 665bb18cb5
2 changed files with 22 additions and 23 deletions

View File

@@ -21,6 +21,11 @@ export const addCelestialType = (celestial: any) => {
return celestial; return celestial;
}; };
export default { export const addCelestialsType = (celestials: any) => {
addCelestialType return celestials.map((e: any) => addCelestialType(e));
};
export default {
addCelestialType,
addCelestialsType
}; };

View File

@@ -16,12 +16,12 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent } from "vue"; import { defineComponent, onMounted, ref } from "vue";
// API // API
import axios from "axios"; import axios from "axios";
// Global methods // Global methods
import { addCelestialType } from "@/plugins/methods"; import { addCelestialsType } from "@/plugins/methods";
import CelestialsList from "@/components/celestials/CelestialsList.vue"; import CelestialsList from "@/components/celestials/CelestialsList.vue";
import NestLoader from "@/components/NestLoader.vue"; import NestLoader from "@/components/NestLoader.vue";
@@ -34,38 +34,32 @@ export default defineComponent({
}, },
data() { data() {
return { return {
celestials: Array<any>(),
error: "" error: ""
}; };
}, },
mounted() { setup() {
this.fetchCelestials() const celestials = ref(Array<any>());
.then(res => {
this.celestials = this.addType(res);
})
.catch(() => {
this.error = "Impossible de récupérer les astres.";
});
},
methods: {
/** /**
* Fetches celestial bodies from API * Fetches celestial bodies from API
*/ */
fetchCelestials(): Promise<any> { const fetchCelestials = (): Promise<any> => {
return axios return axios
.get("https://api.le-systeme-solaire.net/rest/bodies/") .get("https://api.le-systeme-solaire.net/rest/bodies/")
.then(res => { .then(res => {
return res.data.bodies; return res.data.bodies;
}); });
}, };
/**
* Assign a type from the celestial object provided const getCelestials = async () => {
*/ celestials.value = await fetchCelestials();
addType(bodies: Array<any>): Array<any> { celestials.value = addCelestialsType(celestials.value);
return bodies.map((e: any) => addCelestialType(e)); };
}
onMounted(getCelestials);
return { celestials };
} }
}); });
</script> </script>