Added like count to posts

main
TZGyn 3 years ago
parent 95919077a1
commit a3fce82bfe
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -16,6 +16,7 @@ return new class extends Migration
$table->uuid('id'); $table->uuid('id');
$table->foreignUuid('user_id')->constrained(table: 'users', column: 'id'); $table->foreignUuid('user_id')->constrained(table: 'users', column: 'id');
$table->longText('description')->nullable(true); $table->longText('description')->nullable(true);
$table->integer('like_count')->default(0);
$table->timestamps(); $table->timestamps();
}); });
} }

@ -1,45 +1,104 @@
<script setup lang="ts"> <script setup lang="ts">
type Post = { type Post = {
user: { user: {
name: String name: string
} }
description: String id: string
description: string
created_at: string created_at: string
like_count: number
} }
const props = defineProps<Post>() type Props = {
post: Post
}
const now = new Date().toLocaleDateString() const dummy = reactive({
comment_count: 100,
retweet_count: 100,
})
const postTime = new Date(props.created_at) const props = defineProps<Props>()
const post = props.post
const now = new Date().toLocaleDateString()
const postTime = new Date(post.created_at)
const displayTime = () => { const displayTime = () => {
if (now == postTime.toLocaleDateString()) { if (now == postTime.toLocaleDateString()) {
return postTime.toLocaleTimeString() return postTime.toLocaleTimeString()
} }
return postTime.toLocaleDateString() return postTime.toLocaleDateString()
}
const showPost = () => {
return
navigateTo(`/post/${post.id}`)
}
const liked = ref<Boolean>(false)
const toggleComment = () => {}
const toggleRetweet = () => {}
const toggleLike = () => {
post.like_count += liked.value ? -1 : 1
liked.value = !liked.value
} }
</script> </script>
<template> <template>
<div> <div>
<div class="flex w-full gap-4 border border-lightgray p-6"> <div
@click="showPost()"
class="flex w-full gap-4 border border-lightgray px-4 py-2 hover:cursor-pointer">
<div> <div>
<Icon <Icon
name="user" name="user"
size="40" /> size="40" />
</div> </div>
<div class="flex w-full flex-col gap-2"> <div class="flex w-full flex-col gap-2">
<div class="flex w-full justify-between"> <div class="mt-2 flex w-full justify-between">
<div class="flex h-full gap-4 font-bold"> <div class="flex h-full gap-4 font-bold">
{{ props.user.name }} {{ post.user.name }}
</div> </div>
<div> <div>
{{ displayTime() }} {{ displayTime() }}
</div> </div>
</div> </div>
<div class="h-full whitespace-pre-line"> <div class="h-full whitespace-pre-line">
{{ props.description }} {{ post.description }}
</div>
<div class="my-2 flex select-none justify-between">
<div
@click="toggleComment()"
class="flex items-center gap-2 hover:text-accent">
<Icon
name="uil:comment"
size="20" />
{{ dummy.comment_count }}
</div>
<div
@click="toggleRetweet()"
class="flex items-center gap-2 hover:text-green-500">
<Icon
name="ant-design:retweet-outlined"
size="20" />
{{ dummy.retweet_count }}
</div>
<div
@click="toggleLike()"
class="flex items-center gap-2 hover:text-red-500"
:class="{ 'text-red-500': liked }">
<Icon
name="material-symbols:favorite-outline"
size="20" />
{{ post.like_count }}
</div>
<div class="flex items-center gap-2 hover:text-accent">
<Icon
name="material-symbols:upload"
size="20" />
</div>
</div> </div>
</div> </div>
</div> </div>

@ -9,6 +9,7 @@ const PostValidator = z.object({
user: z.object({ user: z.object({
name: z.string(), name: z.string(),
}), }),
like_count: z.number(),
}) })
const newPostValidator = z.object({ const newPostValidator = z.object({

Loading…
Cancel
Save