-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.vue
87 lines (74 loc) · 1.86 KB
/
App.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<template>
<div class="wrapper">
<h1>Страница с постами</h1>
<my-btn style="margin-top: 15px" @click="showDialog">Создать пост</my-btn>
<post-list :posts="posts" v-if="!arePostsLoading" @remove="removePost" />
<my-preloader v-else>Идет загрузка...</my-preloader>
<my-dialog v-model:isShown="isDialogVisible"
><post-form @create="createPost"
/></my-dialog>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import axios, { AxiosResponse } from 'axios';
import PostForm from '@/components/PostForm.vue';
import PostList from '@/components/PostList.vue';
import { IPost } from './types';
export default defineComponent({
components: { PostForm, PostList },
data() {
return {
posts: [] as IPost[],
isDialogVisible: false,
arePostsLoading: false,
};
},
methods: {
showDialog() {
this.isDialogVisible = true;
},
createPost(post: IPost) {
this.posts.push(post);
this.isDialogVisible = false;
},
removePost(post: IPost) {
this.posts = this.posts.filter((p) => p.id !== post.id);
},
async fetchPosts() {
try {
this.arePostsLoading = true;
const response: AxiosResponse<IPost[], unknown> = await axios.get(
'https://jsonplaceholder.typicode.com/posts?_limit=10'
);
if (response.status !== 200) throw new Error();
this.posts = response.data;
} catch (e) {
console.error(e);
} finally {
this.arePostsLoading = false;
}
},
},
mounted() {
this.fetchPosts();
},
});
</script>
<style lang="scss">
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
button {
border: none;
cursor: pointer;
}
.wrapper {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
</style>