Skip to content

Commit

Permalink
Restructure lib and make some progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Ackuq committed Sep 28, 2023
1 parent f060307 commit 8ea9cf8
Show file tree
Hide file tree
Showing 37 changed files with 484 additions and 448 deletions.
12 changes: 5 additions & 7 deletions apps/web/app/ledamot/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
memberController,
membersController,
} from "@lib/api/controllers/members";
import getMember from "@lib/api/member/get-member";
import getMembers from "@lib/api/member/get-members";
import { ERROR_404_TITLE } from "@lib/constants";
import { notFound } from "next/navigation";

Expand All @@ -12,7 +10,7 @@ interface PageProps {
}

export async function generateMetadata({ params: { id } }: PageProps) {
const member = await memberController(id);
const member = await getMember(id);

if (!member) {
return {
Expand All @@ -31,7 +29,7 @@ export async function generateMetadata({ params: { id } }: PageProps) {
}

export default async function MemberPage({ params: { id } }: PageProps) {
const member = await memberController(id);
const member = await getMember(id);
if (!member) {
return notFound();
}
Expand All @@ -44,7 +42,7 @@ export default async function MemberPage({ params: { id } }: PageProps) {
}

export async function generateStaticParams() {
const members = await membersController();
const members = await getMembers();

return members.map((member) => ({
id: member.id,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/ledamot/member-card.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MemberImage from "@components/parliament/member-image";
import type { MemberListEntry } from "@lib/api/types/member";
import type { MemberListEntry } from "@lib/api/member/types";
import { partyLogo } from "@lib/assets";
import { routes } from "@lib/navigation";
import Image from "next/image";
Expand Down
4 changes: 2 additions & 2 deletions apps/web/app/ledamot/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PageTitle from "@components/common/page-title";
import { membersController } from "@lib/api/controllers/members";
import MemberCard from "./member-card";
import Filter from "@components/common/filter";
import getMembers from "@lib/api/member/get-members";

export const metadata = {
title: "Riksdagsledamöter | Partiguiden",
Expand All @@ -10,7 +10,7 @@ export const metadata = {
};

export default async function MembersPage() {
const members = await membersController();
const members = await getMembers();

return (
<main>
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/parti/[party]/leader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import type { Leader } from "@lib/api/types/member";
import type { Leader } from "@lib/api/member/types";
import Link from "next/link";
import { routes } from "@lib/navigation";
import MemberImage from "@components/parliament/member-image";
Expand Down
4 changes: 2 additions & 2 deletions apps/web/app/parti/[party]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import ExternalLink from "@components/common/external-link";
import PageTitle from "@components/common/page-title";
import SocialMediaShare from "@components/common/social-media-share";
import PartyIcon from "@components/party/icon";
import { partyController } from "@lib/api/controllers/party";
import { ERROR_404_TITLE } from "@lib/constants";
import { Party } from "@partiguiden/party-data/types";
import { getPartyName } from "@partiguiden/party-data/utils";
import { notFound } from "next/navigation";
import Leader from "./leader";
import { getParty } from "@lib/api/party/get-party";

interface PageProps {
params: {
Expand All @@ -37,7 +37,7 @@ export default async function PartyPage({
if (!Object.values(Party).includes(partyAbbreviation)) {
return notFound();
}
const party = await partyController(partyAbbreviation);
const party = await getParty(partyAbbreviation);

return (
<main>
Expand Down
2 changes: 1 addition & 1 deletion apps/web/components/parliament/member-image.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import type { MemberListEntry } from "@lib/api/types/member";
import type { MemberListEntry } from "@lib/api/member/types";
import Image from "next/image";
import { useState } from "react";
import { twMerge } from "tailwind-merge";
Expand Down
151 changes: 0 additions & 151 deletions apps/web/lib/api/controllers/members.ts

This file was deleted.

45 changes: 0 additions & 45 deletions apps/web/lib/api/controllers/wikipedia.ts

This file was deleted.

28 changes: 28 additions & 0 deletions apps/web/lib/api/member/get-absence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { PARLIAMENT_BASE_URL } from "@lib/constants";
import serializeAbsence from "./parsers/absence";

interface Query {
id: string;
parliamentYears: string[];
}

export default async function getAbsence({
id,
parliamentYears,
}: Query): Promise<number | null> {
const query = new URLSearchParams({
iid: id,
rm: parliamentYears.join(","),
utformat: "json",
gruppering: "namn",
});

const response = await fetch(
`${PARLIAMENT_BASE_URL}/voteringlista/?${query}`,
{
next: { revalidate: 60 * 60 * 24 },
},
);
const data = await response.json();
return serializeAbsence(data);
}
46 changes: 46 additions & 0 deletions apps/web/lib/api/member/get-member-with-absence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { MemberDetailedResponse } from "./types";
import getAbsence from "./get-absence";
import getMember from "./get-member";
import { getCurrentMandatePeriod } from "../parliament/get-current-mandate-period";

export default async function getMemberWithAbsence(
id: string,
): Promise<MemberDetailedResponse | null> {
// Get latest parliament year and mandate period
const mandatePeriod = await getCurrentMandatePeriod();

const memberPromise = getMember(id);
const absenceParliamentYearPromise = getAbsence({
id,
parliamentYears: [mandatePeriod.latestParliamentYear],
});
const absenceMandatePeriodPromise = getAbsence({
id,
parliamentYears: mandatePeriod.parliamentYears,
});

const [memberData, absenceParliamentYear, absenceMandatePeriod] =
await Promise.all([
memberPromise,
absenceParliamentYearPromise,
absenceMandatePeriodPromise,
]);

if (!memberData) {
return null;
}

return {
...memberData,
absence: {
mandatePeriod: {
value: absenceMandatePeriod,
description: mandatePeriod.period,
},
parliamentYear: {
value: absenceParliamentYear,
description: mandatePeriod.latestParliamentYear,
},
},
};
}
12 changes: 12 additions & 0 deletions apps/web/lib/api/member/get-member.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PARLIAMENT_BASE_URL } from "@lib/constants";
import type { MemberResponse } from "./types";
import { parseMember } from "./parsers/member";
import type { PersonListSingle } from "../parliament/types";

export default async function getMember(id: string): Promise<MemberResponse> {
const response = await fetch(`${PARLIAMENT_BASE_URL}/person/${id}/json`, {
next: { revalidate: 60 * 60 * 24 },
});
const data: PersonListSingle = await response.json();
return parseMember(data.personlista.person);
}
Loading

0 comments on commit 8ea9cf8

Please sign in to comment.