Skip to content

Commit

Permalink
feat: link sheet data in individual bill page
Browse files Browse the repository at this point in the history
  • Loading branch information
Th1nkK1D committed Mar 22, 2024
1 parent f9dd997 commit 7dac262
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 175 deletions.
14 changes: 10 additions & 4 deletions src/components/bills/CoPartyProposer.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
<script lang="ts">
import type { Politician } from '$models/politician';
export let party: string;
export let politicians: Politician[];
export let name: string;
export let politicians: (Politician | string)[];
$: party =
typeof politicians[0] === 'object'
? politicians[0].partyRoles.find((e) => !e.endedAt)?.party
: undefined;
</script>

<div class="flex gap-2">
<div
class="flex items-center justify-center overflow-hidden rounded-full border border-gray-30"
style="width: 24px; height: 24px;"
>
<img src={politicians[0].partyRoles.find((e) => !e.endedAt)?.party.logo} alt="" />
<img src={party?.logo || '/images/politicians/_placeholder.webp'} alt="" />
</div>
<div class="flex">
<p class="underline">พรรค{party}</p>
<!-- TODO: link to party when implemented -->
<p>{party ? 'พรรค' : ''}{name}</p>
<p class="body-02 ml-1 text-text-02">{politicians.length} คน</p>
</div>
</div>
23 changes: 16 additions & 7 deletions src/components/bills/CoProposer.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<script lang="ts">
import type { Politician } from '$models/politician';
export let index: number;
export let firstname: string;
export let lastname: string;
export let logo: string | undefined;
export let politician: Politician | string;
$: party =
typeof politician === 'object'
? politician.partyRoles.find((e) => !e.endedAt)?.party
: undefined;
</script>

<tr>
Expand All @@ -13,11 +18,15 @@
class="flex items-center justify-center overflow-hidden rounded-full border border-gray-30"
style="width: 16px; height: 16px;"
>
{#if logo}
<img src={logo} alt="" />
{/if}
<img src={party?.logo || '/images/politicians/_placeholder.webp'} alt="" />
</div>
<p class="text-start underline">{firstname + ' ' + lastname}</p>
{#if typeof politician === 'object'}
<a class="underline" href="/policitians/{politician.id}"
>{politician.firstname} {politician.lastname}</a
>
{:else}
<p>{politician}</p>
{/if}
</div>
</td>
</tr>
9 changes: 2 additions & 7 deletions src/components/bills/ModalListCoProposers.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import CloseLarge from 'carbon-icons-svelte/lib/CloseLarge.svelte';
import CoProposer from './CoProposer.svelte';
export let coProposedByPoliticians: Politician[];
export let coProposedByPoliticians: (Politician | string)[];
</script>

{#if $showModalListCoProposer}
Expand All @@ -31,12 +31,7 @@
<div class="flex flex-col pb-5 pl-8 pr-5">
<table class="w-full">
{#each coProposedByPoliticians as politician, i}
<CoProposer
index={i + 1}
logo={politician.partyRoles.find((e) => !e.endedAt)?.party.logo}
firstname={politician.firstname}
lastname={politician.lastname}
/>
<CoProposer index={i + 1} {politician} />
{/each}
</table>
</div>
Expand Down
20 changes: 14 additions & 6 deletions src/models/bill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { safeFind } from '$lib/datasheets/processor';
import type { Assembly } from './assembly';
import type { Link } from './link';
import type { Politician } from './politician';
import md5 from 'md5';
import { z } from 'zod';

export enum BillStatus {
Expand Down Expand Up @@ -47,6 +48,7 @@ export const createBillSchema = (politicians: Politician[], assemblies: Assembly
})
.transform(
({
id,
categories,
attachmentName,
attachmentUrl,
Expand All @@ -57,8 +59,9 @@ export const createBillSchema = (politicians: Politician[], assemblies: Assembly
peopleSignatureCount,
...rest
}) => ({
...rest,
categories: categories?.split(',').map((c) => c.trim()) || [ // TODO: Mock category while datasheet is not ready
id: md5(id),
categories: categories?.split(',').map((c) => c.trim()) || [
// TODO: Mock category while datasheet is not ready
['ขนส่งสาธารณะ', 'เศรษฐกิจ', 'แก้รัฐธรรมนูญ', 'วัฒนธรรม', 'เกษตรกรรม'][
Math.floor(Math.random() * 5)
]
Expand All @@ -73,19 +76,24 @@ export const createBillSchema = (politicians: Politician[], assemblies: Assembly
? BillProposerType.Cabinet
: BillProposerType.People,
proposedLedByPolitician: proposedLedByPoliticianId
? politicians.find(({ id }) => id === proposedLedByPoliticianId)
? politicians.find((politician) => politician.id === proposedLedByPoliticianId)
: undefined,
coProposedByPoliticians: coProposedByPoliticians?.split(',').map((name) => name.trim()),
coProposedByPoliticians: coProposedByPoliticians?.split(',').map((text) => {
const name = text.trim();
const expectedId = name.replaceAll(' ', '-');
return politicians.find((politician) => politician.id === expectedId) || name;
}),
proposedByAssembly: proposedByAssemblyId
? safeFind(assemblies, ({ id }) => id === proposedByAssemblyId)
? safeFind(assemblies, (assembly) => assembly.id === proposedByAssemblyId)
: undefined,
proposedByPeople:
proposedLedByPeople && peopleSignatureCount
? ({
ledBy: proposedLedByPeople,
signatoryCount: peopleSignatureCount
} as PeopleProposer)
: undefined
: undefined,
...rest
})
);

Expand Down
146 changes: 9 additions & 137 deletions src/routes/bills/[id]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,150 +1,22 @@
import type { RelatedVotingResults } from '$components/bills/Progress.svelte';
import { fetchVotings } from '$lib/datasheets/index';
import { fetchFromIdOr404, fetchBills } from '$lib/datasheets';
import { createSeo } from '$lib/seo';
import { BillStatus, type Bill } from '$models/bill';
import type { Bill } from '$models/bill';
import type { BillEvent } from '$models/bill-event';
import { inProgressBill, enactedBill } from '../../../mocks/data/bill';
import {
enforcementEvent,
failingMp3Event,
hearingEvent,
inProgressMp2Event,
passingMergedMp2Event,
passingMp1Event,
passingMp2Event,
passingMp3Event,
passingSenate1Event,
passingSenate2Event,
passingSenate3Event,
royalAssentEvent
} from '../../../mocks/data/event';

export interface VotingResultSummary {
agreed: number;
total: number;
subResults?: {
affiliationName: string;
agreed: number;
total: number;
}[];
}

export async function load({ params }) {
/*
* | billId | Status |
* | ------ | ----------- |
* | 1 | InProgress |
* | 2 | Success |
* | 3 | Rejected |
* | 4 | Merged |
*/
const billId = Number(params.id);
const bill = enactedBill;
bill.id = billId;

let mergedBills: Bill[] | undefined;

let events: BillEvent[] = [];
let mergedIntoBill: Bill | undefined;
let mergedIntoBillLatestEvent: BillEvent | undefined;
let relatedVotingResults: RelatedVotingResults = {};

const votings = await fetchVotings();

if (billId === 1) {
bill.status = BillStatus.InProgress;
mergedBills = [
{ ...enactedBill, status: BillStatus.Merged },
{ ...enactedBill, status: BillStatus.Merged }
];
events = [hearingEvent, passingMp1Event, inProgressMp2Event];
relatedVotingResults = {
1: { voting: votings[0], resultSummary: fakeMpPassedVotingResultSummary }
};
} else if (billId === 2) {
bill.status = BillStatus.Enacted;
events = [
hearingEvent,
passingMp1Event,
passingMp2Event,
passingMp3Event,
passingSenate1Event,
passingSenate2Event,
passingSenate3Event,
royalAssentEvent,
enforcementEvent
];
relatedVotingResults = {
'1': { voting: votings[0], resultSummary: fakeMpPassedVotingResultSummary },
'3': { voting: votings[0], resultSummary: fakeSenatePassedVotingResultSummary }
};
} else if (billId === 3) {
bill.status = BillStatus.Rejected;
events = [hearingEvent, passingMp1Event, passingMp2Event, failingMp3Event];
relatedVotingResults = {
'1': { voting: votings[0], resultSummary: fakeMpPassedVotingResultSummary },
'2': { voting: votings[1], resultSummary: fakeMpFailedVotingResultSummary }
};
} else if (billId === 4) {
bill.status = BillStatus.Merged;
events = [hearingEvent, passingMp1Event, passingMergedMp2Event];
mergedIntoBill = inProgressBill;
mergedIntoBillLatestEvent = { ...inProgressMp2Event, billId: inProgressBill.id };
relatedVotingResults = {
'1': { voting: votings[0], resultSummary: fakeMpPassedVotingResultSummary }
};
}

events.reverse();
const bill = await fetchFromIdOr404(fetchBills, params.id);

return {
bill,
mergedBills, // Bills that got merged into this bill.
events,
mergedIntoBill, // The bill that this bill got merged into. (merged event)
mergedIntoBillLatestEvent,
relatedVotingResults, // Info of votings in events
// TODO: merged bill and event data is not ready yet
mergedBills: [] as Bill[], // Bills that got merged into this bill.
events: [] as BillEvent[],
mergedIntoBill: undefined as Bill | undefined, // The bill that this bill got merged into. (merged event)
mergedIntoBillLatestEvent: undefined as BillEvent | undefined,
relatedVotingResults: {} as RelatedVotingResults, // Info of votings in events
seo: createSeo({
title: bill.nickname
})
};
}

const fakeMpPassedVotingResultSummary: VotingResultSummary = {
agreed: 324,
total: 500,
subResults: [
{
affiliationName: 'สส.ฝ่ายรัฐบาล',
agreed: 310,
total: 310
},
{
affiliationName: 'สส.ฝ่ายค้าน',
agreed: 14,
total: 190
}
]
};

const fakeMpFailedVotingResultSummary: VotingResultSummary = {
agreed: 130,
total: 500,
subResults: [
{
affiliationName: 'สส.ฝ่ายรัฐบาล',
agreed: 0,
total: 310
},
{
affiliationName: 'สส.ฝ่ายค้าน',
agreed: 130,
total: 190
}
]
};

const fakeSenatePassedVotingResultSummary: VotingResultSummary = {
agreed: 200,
total: 250
};
28 changes: 14 additions & 14 deletions src/routes/bills/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import CoPartyProposer from '$components/bills/CoPartyProposer.svelte';
import Progress from '$components/bills/Progress.svelte';
const NO_PARTY_FOUND_LABEL = 'ไม่พบข้อมูลพรรค';
export let data;
const {
Expand Down Expand Up @@ -48,10 +50,11 @@
{} as Record<K, T[]>
);
$: partiescoProposed = bill.coProposedByPoliticians
? groupBy(
bill.coProposedByPoliticians,
(politician) => politician.partyRoles.find((e) => !e.endedAt)!.party.name
$: partiesCoProposed = bill.coProposedByPoliticians
? groupBy(bill.coProposedByPoliticians, (politician) =>
typeof politician === 'string'
? NO_PARTY_FOUND_LABEL
: politician.partyRoles.find((e) => !e.endedAt)!.party.name
)
: null;
Expand Down Expand Up @@ -82,7 +85,9 @@
}
$: dayElapsed = dayjs(
bill.status === BillStatus.InProgress ? new Date() : events[events.length - 1].date
bill.status === BillStatus.InProgress
? new Date()
: events[events.length - 1]?.date || new Date()
).diff(bill.proposedOn, 'days');
$: innerWidth = 0;
Expand Down Expand Up @@ -259,9 +264,9 @@
{bill.coProposedByPoliticians.length} คน
</p>
<div class="flex flex-col flex-wrap gap-3 px-3 md:flex-row">
{#if partiescoProposed}
{#each Object.entries(partiescoProposed) as [party, politicians] (party)}
<CoPartyProposer {party} {politicians} />
{#if partiesCoProposed}
{#each Object.entries(partiesCoProposed) as [name, politicians] (name)}
<CoPartyProposer {name} {politicians} />
{/each}
{/if}
</div>
Expand All @@ -271,12 +276,7 @@
<div class="relative flex flex-col">
<table class="w-full">
{#each bill.coProposedByPoliticians.slice(0, MAX_DISPLAY_COPROPOSER) as politician, i}
<CoProposer
index={i + 1}
logo={politician.partyRoles.find((e) => !e.endedAt)?.party.logo}
firstname={politician.firstname}
lastname={politician.lastname}
/>
<CoProposer index={i + 1} {politician} />
{/each}
</table>

Expand Down

0 comments on commit 7dac262

Please sign in to comment.