|
| 1 | +<script lang="ts"> |
| 2 | + import { DefaultVotingResult, type Voting } from '$models/voting'; |
| 3 | + import { Tag } from 'carbon-components-svelte'; |
| 4 | + import dayjs from 'dayjs'; |
| 5 | + import 'dayjs/locale/th'; |
| 6 | + import buddhistEra from 'dayjs/plugin/buddhistEra'; |
| 7 | + import { failedVoting, failedVotingSummary } from '../../mocks/data/voting'; |
| 8 | +
|
| 9 | + dayjs.extend(buddhistEra); |
| 10 | + dayjs.locale('th'); |
| 11 | +
|
| 12 | + type VoteCardTheme = { |
| 13 | + bg: string; |
| 14 | + hoveredBg: string; |
| 15 | + tagBg: string; |
| 16 | + tagFontColor: string; |
| 17 | + }; |
| 18 | + const CARD_THEMES: Record<DefaultVotingResult, VoteCardTheme> = { |
| 19 | + [DefaultVotingResult.Passed]: { |
| 20 | + bg: 'bg-teal-10', |
| 21 | + hoveredBg: 'hover:bg-teal-20', |
| 22 | + tagBg: 'bg-teal-30', |
| 23 | + tagFontColor: 'text-text-01' |
| 24 | + }, |
| 25 | + [DefaultVotingResult.Failed]: { |
| 26 | + bg: 'bg-red-10', |
| 27 | + hoveredBg: 'hover:bg-red-20', |
| 28 | + tagBg: 'bg-red-30', |
| 29 | + tagFontColor: 'text-text-01' |
| 30 | + } |
| 31 | + }; |
| 32 | + const CANDIDATE_CARD_THEME: VoteCardTheme = { |
| 33 | + bg: 'bg-purple-10', |
| 34 | + hoveredBg: 'hover:bg-purple-20', |
| 35 | + tagBg: 'bg-purple-70', |
| 36 | + tagFontColor: 'text-white' |
| 37 | + }; |
| 38 | +
|
| 39 | + export let voting: Voting = {} as Voting; |
| 40 | +
|
| 41 | + const { totalCount, totalAmount } = failedVotingSummary.reduce( |
| 42 | + (acc, assembly) => { |
| 43 | + return { |
| 44 | + totalCount: acc.totalCount + assembly.count, |
| 45 | + totalAmount: acc.totalAmount + assembly.total |
| 46 | + }; |
| 47 | + }, |
| 48 | + { totalCount: 0, totalAmount: 0 } |
| 49 | + ); |
| 50 | +
|
| 51 | + $: theme = CARD_THEMES[voting.result as DefaultVotingResult] || CANDIDATE_CARD_THEME; |
| 52 | + $: isCandidate = ![DefaultVotingResult.Failed, DefaultVotingResult.Passed].includes( |
| 53 | + voting.result as DefaultVotingResult |
| 54 | + ); |
| 55 | +</script> |
| 56 | + |
| 57 | +<div |
| 58 | + class={`vote-card relative p-4 flex flex-col gap-y-2 w-72 h-64.5 ${theme.bg} ${theme.hoveredBg}`} |
| 59 | +> |
| 60 | + <p class="text-02">{dayjs(voting.date).format('D MMM BB')}</p> |
| 61 | + <a |
| 62 | + class="vote-card--url after:inset text-text-01 text-xl font-[700] no-underline after:absolute w-56" |
| 63 | + href={voting.sourceUrl} |
| 64 | + > |
| 65 | + {voting.title} |
| 66 | + </a> |
| 67 | + <section class="vote-card__result flex flex-col gap-y-2 w-56"> |
| 68 | + <Tag class={`${theme.tagFontColor} ${theme.tagBg} w-fit m-0`}>{voting.result}</Tag> |
| 69 | + <div> |
| 70 | + <div class="flex align-middle justify-between"> |
| 71 | + <p class="text-text-01 font-[600]">{isCandidate ? voting.result : 'เห็นด้วย'}</p> |
| 72 | + <p> |
| 73 | + <span class="mr-[2px] text-text-primary font-[600]">{totalCount}</span> |
| 74 | + <span class="text-text-02">/{totalAmount}</span> |
| 75 | + </p> |
| 76 | + </div> |
| 77 | + <ul class="vote-card__result--list"> |
| 78 | + {#each failedVotingSummary as assembly} |
| 79 | + <li class="vote-card__result--item flex flex-row align-middle justify-between"> |
| 80 | + <p>{assembly.name}</p> |
| 81 | + <p> |
| 82 | + <span class="mr-[2px] text-text-primary">{assembly.count}</span> |
| 83 | + <span class="text-text-02">/{assembly.total}</span> |
| 84 | + </p> |
| 85 | + </li> |
| 86 | + {/each} |
| 87 | + </ul> |
| 88 | + </div> |
| 89 | + </section> |
| 90 | +</div> |
| 91 | + |
| 92 | +<style lang="scss"> |
| 93 | + .vote-card { |
| 94 | + position: relative; |
| 95 | +
|
| 96 | + &--url { |
| 97 | + &::after { |
| 98 | + content: ''; |
| 99 | + position: absolute; |
| 100 | + inset: 0; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +</style> |
0 commit comments