added posts

This commit is contained in:
2025-12-10 17:02:12 +00:00
parent ab6c2baa70
commit d7978a9303

View File

@@ -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() {
if (idx < len - 1) {
idx++;
rightCap.value = idx === len - 1;
leftCap.value = idx === 0;
post.value = posts[idx]; post.value = posts[idx];
idx = (idx + 1) % len; }
} }
function prevPost() { function prevPost() {
if (idx > 0) {
idx--;
rightCap.value = idx === len - 1;
leftCap.value = idx === 0;
post.value = posts[idx]; post.value = posts[idx];
idx = (idx - 1) % len; }
} }
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>