strava-frontend/src/pages/workouts/WorkoutList.vue

80 lines
2.0 KiB
Vue

<template>
<h1 class="page-title">Тренировки</h1>
<section class="news-feed">
<template v-if="workoutItems.length == 0">
<div>Тренировки не найдены</div>
</template>
<template v-for="item in workoutItems" :key="item.id">
<WorkoutListItem
:item="item"
:openWorkout="openWorkout"
:deleteItem="deleteItem"
></WorkoutListItem>
</template>
</section>
</template>
<script setup lang="ts">
import { AxiosResponse, AxiosInstance } from "axios";
import { ref, inject } from "vue";
import { useToast } from "vuestic-ui/web-components";
import { useRouter } from "vue-router";
import { WorkoutItem } from "./Definitions.vue";
import WorkoutListItem from "./components/WorkoutListItem.vue";
const { push } = useRouter();
const axiosAuth = inject("axiosAuth") as AxiosInstance;
let workoutItems = ref<Array<WorkoutItem>>([]);
const { init } = useToast();
const deleteItem = (id: string, event: Event) => {
event.stopPropagation();
axiosAuth
.delete(`/api/v0/workouts/${id}`)
.then((_: AxiosResponse) => {
init({
message: "Тренировка успешно удалена.",
color: "success",
});
initWorkouts();
})
.catch((error: unknown) => {
console.log(error);
init({
message: "Что-то пошло не так.",
color: "error",
});
});
};
const openWorkout = (id: string) => {
push({ name: "workout_item", params: { id: id } }).catch(() => {});
};
const initWorkouts = () => {
axiosAuth
.get(`/api/v0/workouts`)
.then((response: AxiosResponse) => {
workoutItems.value = response.data.results;
})
.catch((error: unknown) => {
console.log(error);
init({
message: "Что-то пошло не так.",
color: "error",
});
});
};
initWorkouts();
</script>
<style>
.news-feed {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
</style>