|
1 | 1 | <script lang="ts" setup>
|
2 | 2 | import type { Option } from '../../types.ts'
|
3 |
| -import type { OptionsDatabase } from '@devprotocol/clubs-plugin-posts' |
4 |
| -import { decode } from '@devprotocol/clubs-core' |
| 3 | +import type { OptionsDatabase, Posts } from '@devprotocol/clubs-plugin-posts' |
| 4 | +import {type ClubsProfile, decode} from '@devprotocol/clubs-core' |
5 | 5 | import { onMounted, ref } from 'vue'
|
| 6 | +import Profile from '../Votes/Profile.vue'; |
| 7 | +import IconLock from '../../assets/images/icon-lock.png'; |
| 8 | +import {remark} from 'remark'; |
| 9 | +import strip from 'strip-markdown' |
6 | 10 |
|
7 | 11 | type Props = {
|
8 | 12 | options: Option[]
|
9 | 13 | feeds: OptionsDatabase[]
|
| 14 | + postsPluginId: string |
10 | 15 | }
|
11 | 16 | const props = defineProps<Props>()
|
12 | 17 |
|
13 |
| -const polls = ref([]) |
14 |
| -const error = ref('') |
15 |
| -const isLoading = ref(true) |
| 18 | +const polls = ref<any[]>([]) |
16 | 19 |
|
17 |
| -const fetchPolls = async () => { |
18 |
| - // これでPollのFeedをとってくる |
19 |
| - // http://localhost:4321/api/devprotocol:clubs:plugin:posts/default-2/search/has:option/%23poll |
| 20 | +const { postsPluginId, feeds } = props |
| 21 | +
|
| 22 | +type Polls = { |
| 23 | + title: string |
| 24 | + values: PostsPlus[] |
| 25 | +} |
| 26 | +
|
| 27 | +type PostsPlus = Posts & { |
| 28 | + image: string |
| 29 | + profile: Promise<{ |
| 30 | + readonly profile: ClubsProfile | undefined; |
| 31 | + readonly error: Error | undefined; |
| 32 | + }> |
| 33 | + stripedMarkdown: string |
| 34 | +} |
| 35 | +
|
| 36 | +const feedId = ref('') |
| 37 | +
|
| 38 | +const fetchPolls = async (feed: OptionsDatabase): Promise<Polls> => { |
| 39 | + feedId.value = feed.id |
| 40 | + const title = feed.title |
20 | 41 |
|
21 | 42 | const url = new URL(
|
22 |
| - `/api/devprotocol:clubs:plugin:posts/default-2/search/has:option/%23poll`, |
| 43 | + `/api/${postsPluginId}/${feedId.value}/search/has:option/%23poll`, |
23 | 44 | window.location.origin,
|
24 | 45 | )
|
25 | 46 |
|
26 |
| - fetch(url.toString()) |
27 |
| - .then(async (res) => { |
28 |
| - console.log('res', res.status) |
29 |
| - if (res.status === 200) { |
30 |
| - const json = await res.json() |
31 |
| - polls.value = decode(json.contents) |
32 |
| - } |
33 |
| - }) |
34 |
| - .catch((err) => { |
35 |
| - console.error(err) |
36 |
| - error.value = |
37 |
| - 'Sorry, but there was an error loading the timeline. Please try again later.' |
38 |
| - }) |
39 |
| - .finally(() => { |
40 |
| - isLoading.value = false |
41 |
| - }) |
| 47 | + const res = await fetch(url.toString()) |
| 48 | + const json = await res.json() |
| 49 | +
|
| 50 | + return { |
| 51 | + title: title ? title : feedId.value, |
| 52 | + values: decode(json.contents) |
| 53 | + } |
42 | 54 | }
|
43 | 55 |
|
44 | 56 | onMounted(async () => {
|
45 |
| - await fetchPolls() |
46 |
| - console.log('mounted', polls.value) |
| 57 | + await Promise.all(feeds.map(async (feed) => { |
| 58 | + const data = await fetchPolls(feed) |
| 59 | +
|
| 60 | + data.values = data.values.map((post) => { |
| 61 | + post.updated_at = new Date(post.updated_at).toLocaleString('ja-JP') |
| 62 | +
|
| 63 | + const images = post.options.find((item) => item.key === '#images') |
| 64 | + if (images && images.value.length > 0) { |
| 65 | + post.image = images.value[0] |
| 66 | + } |
| 67 | +
|
| 68 | + remark() |
| 69 | + .use(strip) |
| 70 | + .process(post.content).then((text) => { |
| 71 | + post.stripedMarkdown = text.toString() |
| 72 | + }) |
| 73 | +
|
| 74 | + return post |
| 75 | + }) |
| 76 | +
|
| 77 | + polls.value = [...polls.value, data] |
| 78 | + })) |
47 | 79 | })
|
48 | 80 | </script>
|
49 | 81 |
|
50 | 82 | <template>
|
51 |
| - <h1>Votes2</h1> |
| 83 | + <div class="p-4"> |
| 84 | + <div v-for="poll in polls" :key="poll.title"> |
| 85 | + <h2 class="mb-4 text-xl">{{ poll.title }}</h2> |
| 86 | + <a |
| 87 | + v-for="post in poll.values" |
| 88 | + :key="post.id" |
| 89 | + :href="`/posts/${feedId}/${post.id}`" |
| 90 | + class="block mb-4 p-2 bg-gray-100 rounded" |
| 91 | + > |
| 92 | + <div class="flex justify-between gap-2"> |
| 93 | + <div class="w-full"> |
| 94 | + <p class="text-lg font-bold">Post_title: {{ post.title }}</p> |
| 95 | + <p class="mb-1 text-xs text-gray-400">{{ post.updated_at }}</p> |
| 96 | + <div class="flex justify-between gap-2"> |
| 97 | + <Profile :address="post.created_by" /> |
| 98 | + <p v-if="true" class="flex-grow flex-wrap text-lg truncate"> |
| 99 | + {{ post.stripedMarkdown }} |
| 100 | + </p> |
| 101 | + <div v-else class="flex flex-col justify-center items-center flex-grow p-2 bg-gray-200 rounded"> |
| 102 | + <img |
| 103 | + class="mb-1 w-5" |
| 104 | + :src="IconLock.src" |
| 105 | + alt="paper-airplane" |
| 106 | + /> |
| 107 | + <p class="leading-none">Locked</p> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + <figure v-if="!isMasked && post.image"> |
| 112 | + <img :src="post.image" class="rounded max-w-20 max-h-20 object-cover object-center" alt="post image" /> |
| 113 | + </figure> |
| 114 | + </div> |
| 115 | + </a> |
| 116 | + <div v-if="poll.length < 1" class="mb-4 p-2 bg-gray-100 rounded"> |
| 117 | + <p class="w-full text-gray-400 text-center"> |
| 118 | + Empty :) |
| 119 | + </p> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </div> |
52 | 123 | </template>
|
0 commit comments