/
TheSearchForm.vue
87 lines (76 loc) · 1.73 KB
/
TheSearchForm.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="search-form">
<h3>Photo Search 📷</h3>
<BaseInput @submit="doSearch" />
</div>
<PhotoList :photos="result" ref="photoList" />
</template>
<script>
import { createApi } from 'unsplash-js';
import BaseInput from '@/components/ui/BaseInput';
import PhotoList from '@/components/Result/PhotoList.vue';
const unsplash = new createApi({
accessKey: 'your unsplash access key',
});
export default {
name: 'TheSearchForm',
components: { BaseInput, PhotoList },
data() {
return {
result: [],
currentPage: 1,
query: '',
};
},
methods: {
async doSearch(searchText) {
this.result = [];
const result = await unsplash.search.getPhotos({
query: searchText,
perPage: 20,
});
if (result.status === 200) {
this.result = result.response.results;
this.currentPage++;
this.query = searchText;
}
},
async loadMore() {
const result = await unsplash.search.getPhotos({
query: this.query,
perPage: 10,
page: this.currentPage,
});
if (result.status === 200) {
result.response.results.forEach((item) => {
this.result.push(item);
});
this.currentPage++;
}
},
},
mounted() {
const listElm = this.$refs.photoList.$el;
listElm.addEventListener('scroll', () => {
if (listElm.scrollTop + listElm.clientHeight >= listElm.scrollHeight) {
this.loadMore();
}
});
},
};
</script>
<style scoped>
.search-form {
width: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
padding: 1rem;
flex-direction: column;
}
h3 {
color: #333;
margin-bottom: 1rem;
text-align: center;
}
</style>