|
| 1 | +<script lang="ts"> |
| 2 | + import DirectionStraightRight from 'carbon-icons-svelte/lib/DirectionStraightRight.svelte'; |
| 3 | + import { DefaultVotingResult, type Voting } from '$models/voting'; |
| 4 | + import { Tag } from 'carbon-components-svelte'; |
| 5 | + import dayjs from 'dayjs'; |
| 6 | + import 'dayjs/locale/th'; |
| 7 | + import buddhistEra from 'dayjs/plugin/buddhistEra'; |
| 8 | + import type { VoteCardProps } from '../../routes/assemblies/[id]/+page'; |
| 9 | +
|
| 10 | + dayjs.extend(buddhistEra); |
| 11 | + dayjs.locale('th'); |
| 12 | +
|
| 13 | + interface VoteCardTheme { |
| 14 | + bg: string; |
| 15 | + hoveredBg: string; |
| 16 | + tagBg: string; |
| 17 | + tagFontColor: string; |
| 18 | + } |
| 19 | + const CARD_THEMES: Record<DefaultVotingResult, VoteCardTheme> = { |
| 20 | + [DefaultVotingResult.Passed]: { |
| 21 | + bg: 'bg-teal-10', |
| 22 | + hoveredBg: 'hover:bg-teal-20', |
| 23 | + tagBg: 'bg-teal-30', |
| 24 | + tagFontColor: 'text-text-01' |
| 25 | + }, |
| 26 | + [DefaultVotingResult.Failed]: { |
| 27 | + bg: 'bg-red-10', |
| 28 | + hoveredBg: 'hover:bg-red-20', |
| 29 | + tagBg: 'bg-red-30', |
| 30 | + tagFontColor: 'text-text-01' |
| 31 | + } |
| 32 | + }; |
| 33 | + const CANDIDATE_CARD_THEME: VoteCardTheme = { |
| 34 | + bg: 'bg-purple-10', |
| 35 | + hoveredBg: 'hover:bg-purple-20', |
| 36 | + tagBg: 'bg-purple-70', |
| 37 | + tagFontColor: 'text-white' |
| 38 | + }; |
| 39 | +
|
| 40 | + export let voting: VoteCardProps['voting'] = {} as Voting; |
| 41 | + export let highlightedVoteByGroups: VoteCardProps['highlightedVoteByGroups'] = []; |
| 42 | +
|
| 43 | + interface HighlightedVoteSummary { |
| 44 | + totalCount: number; |
| 45 | + totalAmount: number; |
| 46 | + } |
| 47 | + $: ({ totalCount, totalAmount } = highlightedVoteByGroups.reduce<HighlightedVoteSummary>( |
| 48 | + reduceHighlightedVoteSummary, |
| 49 | + { totalCount: 0, totalAmount: 0 } |
| 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 | +
|
| 56 | + function reduceHighlightedVoteSummary( |
| 57 | + { totalAmount, totalCount }: HighlightedVoteSummary, |
| 58 | + { count, total }: VoteCardProps['highlightedVoteByGroups'][number] |
| 59 | + ): HighlightedVoteSummary { |
| 60 | + return { |
| 61 | + totalCount: totalCount + count, |
| 62 | + totalAmount: totalAmount + total |
| 63 | + }; |
| 64 | + } |
| 65 | +</script> |
| 66 | + |
| 67 | +<div |
| 68 | + class="vote-card rounded-sm relative p-4 flex flex-col gap-y-2 w-72 h-64.5 whitespace-break-spaces {theme.bg} {theme.hoveredBg}" |
| 69 | +> |
| 70 | + <p class="body-compact-01 text-text-02"> |
| 71 | + {dayjs(voting.date).format('D MMM BB')} |
| 72 | + </p> |
| 73 | + <a class="vote-card--url after:inset no-underline after:absolute w-56" href="#{voting.id}"> |
| 74 | + <h3 class="fluid-heading-03 text-text-01">{voting.title}</h3> |
| 75 | + </a> |
| 76 | + <section class="vote-card__result flex flex-col gap-y-2 w-56"> |
| 77 | + <Tag class={`label-01 ${theme.tagFontColor} ${theme.tagBg} w-fit m-0`}>{voting.result}</Tag> |
| 78 | + <div class="flex flex-col gap-x-1"> |
| 79 | + <div class="flex align-middle justify-between"> |
| 80 | + <p class="text-text-01 heading-01">{isCandidate ? voting.result : 'เห็นด้วย'}</p> |
| 81 | + <p> |
| 82 | + <span class="mr-[2px] text-text-primary heading-01">{totalCount}</span> |
| 83 | + <span class="text-text-02 body-01">/{totalAmount}</span> |
| 84 | + </p> |
| 85 | + </div> |
| 86 | + <ul class="vote-card__result--list"> |
| 87 | + {#each highlightedVoteByGroups as voteByGroup (voteByGroup.name)} |
| 88 | + <li class="vote-card__result--item flex flex-row align-middle justify-between body-01"> |
| 89 | + <p class="text-text-01">{voteByGroup.name}</p> |
| 90 | + <p> |
| 91 | + <span class="text-text-primary mr-[2px]">{voteByGroup.count}</span> |
| 92 | + <span class="text-text-02">/{voteByGroup.total}</span> |
| 93 | + </p> |
| 94 | + </li> |
| 95 | + {/each} |
| 96 | + </ul> |
| 97 | + </div> |
| 98 | + </section> |
| 99 | + <DirectionStraightRight class="ml-auto" /> |
| 100 | +</div> |
| 101 | + |
| 102 | +<style lang="scss"> |
| 103 | + .vote-card { |
| 104 | + position: relative; |
| 105 | +
|
| 106 | + &--url { |
| 107 | + &::after { |
| 108 | + content: ''; |
| 109 | + position: absolute; |
| 110 | + inset: 0; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +</style> |
0 commit comments