added posts
This commit is contained in:
@@ -4,6 +4,8 @@ import axios from "axios";
|
|||||||
|
|
||||||
const post = ref(null);
|
const post = ref(null);
|
||||||
const fetched = ref(false);
|
const fetched = ref(false);
|
||||||
|
const leftCap = ref(false);
|
||||||
|
const rightCap = ref(false);
|
||||||
let posts = [];
|
let posts = [];
|
||||||
let idx = 0;
|
let idx = 0;
|
||||||
let len = 0;
|
let len = 0;
|
||||||
@@ -14,6 +16,7 @@ async function fetchPosts() {
|
|||||||
posts = res.data;
|
posts = res.data;
|
||||||
fetched.value = true;
|
fetched.value = true;
|
||||||
post.value = posts[0];
|
post.value = posts[0];
|
||||||
|
leftCap.value = true;
|
||||||
len = posts.length;
|
len = posts.length;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@@ -21,13 +24,21 @@ async function fetchPosts() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function nextPost() {
|
function nextPost() {
|
||||||
post.value = posts[idx];
|
if (idx < len - 1) {
|
||||||
idx = (idx + 1) % len;
|
idx++;
|
||||||
|
rightCap.value = idx === len - 1;
|
||||||
|
leftCap.value = idx === 0;
|
||||||
|
post.value = posts[idx];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function prevPost() {
|
function prevPost() {
|
||||||
post.value = posts[idx];
|
if (idx > 0) {
|
||||||
idx = (idx - 1) % len;
|
idx--;
|
||||||
|
rightCap.value = idx === len - 1;
|
||||||
|
leftCap.value = idx === 0;
|
||||||
|
post.value = posts[idx];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
@@ -43,8 +54,8 @@ onMounted(() => {
|
|||||||
<small
|
<small
|
||||||
>Created at: {{ new Date(post.CreatedAt).toLocaleString() }}</small
|
>Created at: {{ new Date(post.CreatedAt).toLocaleString() }}</small
|
||||||
>
|
>
|
||||||
<button @click="nextPost">Next</button>
|
<button v-if="!leftCap" @click="prevPost">Prev</button>
|
||||||
<button @click="prevPost">Prev</button>
|
<button v-if="!rightCap" @click="nextPost">Next</button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user