Remove deprecated ContactsAPI use

This commit is contained in:
Aarni Halinen
2020-12-30 00:15:39 +02:00
parent b2deca4ce8
commit d3ab0f5e41
2 changed files with 13 additions and 134 deletions
-53
View File
@@ -1,53 +0,0 @@
import axios from "axios";
const url = `${process.env.NEXT_PUBLIC_API_URL}/contacts`;
const committeeUrl = `${process.env.NEXT_PUBLIC_API_URL}/committees`;
export interface Committee {
name_fi: string;
name_en: string;
}
export interface Role {
name_fi: string;
name_en: string;
description_fi: string;
description_en: string;
committee: Committee;
is_board: boolean;
}
export interface Contact {
first_name: string;
last_name: string;
phone_number: number;
email: string;
image: string;
}
export interface Occupation {
role: Role;
start_date: string;
end_date: string;
officials: Contact[];
}
export async function getContacts(year: number = new Date().getFullYear()): Promise<Occupation[]> {
try {
const resp = await axios.get(`${url}?year=${year}`);
return resp.data["results"];
} catch (err) {
console.error(err);
throw err;
}
}
export async function getCommittees(): Promise<Committee[]> {
try {
const resp = await axios.get(`${committeeUrl}`);
return resp.data["results"];
} catch (err) {
console.error(err);
throw err;
}
}
+13 -81
View File
@@ -1,86 +1,18 @@
import React from "react";
import { Helmet } from "react-helmet";
import { StaticContext } from "@server/StaticContext";
import { getContacts, Occupation, Committee, getCommittees } from "@models/Contacts";
import { NextPage } from "next";
import Head from "next/head";
import ContactsPageView from "@views/ContactsPage/ContactsPageView";
import PageWrapper from "@views/common/PageWrapper";
interface ContactsPageProps {
staticContext: StaticContext;
}
interface ContactsPageState {
contacts: Occupation[];
committees: Committee[];
}
class ContactsPage extends React.Component<ContactsPageProps, ContactsPageState> {
constructor(props: ContactsPageProps) {
super(props);
const { staticContext } = props;
if (staticContext) {
/* The static context is an object that manages promises when
rendering on the server. If staticContext exists, that means
we have to store all promises in it. Otherwise, operate
normally. See server/index.ts. */
if (staticContext.resolutions.getContacts) {
const contacts = staticContext.resolutions.getContacts as Occupation[];
const committees = staticContext.resolutions.getCommittees as Committee[];
this.state = {
contacts,
committees,
};
} else {
this.state = {
contacts: [],
committees: [],
};
const promiseContacts = this.fetchContacts();
const promiseCommittees = this.fetchCommittees();
staticContext.promises.getContacts = promiseContacts;
staticContext.promises.getCommittees = promiseCommittees;
}
} else {
this.state = {
contacts: [],
committees: [],
};
this.fetchContacts();
this.fetchCommittees();
}
}
fetchContacts = () => {
const getContactsPromise = getContacts();
getContactsPromise.then(contacts => {
this.setState({
contacts,
});
});
return getContactsPromise;
}
fetchCommittees = () => {
const getCommitteesPromise = getCommittees();
getCommitteesPromise.then(committees => {
this.setState({
committees,
});
});
return getCommitteesPromise;
}
render() {
const { contacts, committees } = this.state;
return (
<>
<Helmet>
<link rel="canonical" href="https://sik.ayy.fi/INSERT_PATH_HERE!" />
</Helmet>
<ContactsPageView contacts={contacts} committees={committees} />
</>
);
}
}
const ContactsPage: NextPage = () => (
<>
<Head>
<link rel="canonical" href="https://sik.ayy.fi/yhteystiedot" />
</Head>
<PageWrapper>
<ContactsPageView contacts={[]} committees={[]} />
</PageWrapper>
</>
);
export default ContactsPage;