mirror of
https://github.com/PeWu/topola-viewer.git
synced 2026-08-04 01:51:49 +00:00
Refactoring wikitree.ts
This commit is contained in:
@@ -2,12 +2,24 @@ import {TopolaData, GedcomData} from './gedcom_util';
|
|||||||
import {JsonFam, JsonIndi} from 'topola';
|
import {JsonFam, JsonIndi} from 'topola';
|
||||||
import {GedcomEntry} from 'parse-gedcom';
|
import {GedcomEntry} from 'parse-gedcom';
|
||||||
|
|
||||||
|
/** WikiTree API getAncestors request. */
|
||||||
interface GetAncestorsRequest {
|
interface GetAncestorsRequest {
|
||||||
action: 'getAncestors';
|
action: 'getAncestors';
|
||||||
key: string;
|
key: string;
|
||||||
fields: string;
|
fields: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** WikiTree API getRelatives request. */
|
||||||
|
interface GetRelatives {
|
||||||
|
action: 'getRelatives';
|
||||||
|
keys: string;
|
||||||
|
getChildren?: true;
|
||||||
|
getSpouses?: true;
|
||||||
|
}
|
||||||
|
|
||||||
|
type WikiTreeRequest = GetAncestorsRequest | GetRelatives;
|
||||||
|
|
||||||
|
/** Person structure returned from WikiTree API. */
|
||||||
interface Person {
|
interface Person {
|
||||||
Id: number;
|
Id: number;
|
||||||
Name: string;
|
Name: string;
|
||||||
@@ -19,15 +31,7 @@ interface Person {
|
|||||||
Father: number;
|
Father: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GetRelatives {
|
/** Sends a request to the WikiTree API. Returns the parsed response JSON. */
|
||||||
action: 'getRelatives';
|
|
||||||
keys: string;
|
|
||||||
getChildren?: true;
|
|
||||||
getSpouses?: true;
|
|
||||||
}
|
|
||||||
|
|
||||||
type WikiTreeRequest = GetAncestorsRequest | GetRelatives;
|
|
||||||
|
|
||||||
async function wikiTreeGet(request: WikiTreeRequest, handleCors: boolean) {
|
async function wikiTreeGet(request: WikiTreeRequest, handleCors: boolean) {
|
||||||
const requestData = new FormData();
|
const requestData = new FormData();
|
||||||
requestData.append('format', 'json');
|
requestData.append('format', 'json');
|
||||||
@@ -45,6 +49,7 @@ async function wikiTreeGet(request: WikiTreeRequest, handleCors: boolean) {
|
|||||||
return JSON.parse(responseBody);
|
return JSON.parse(responseBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Retrieves ancestors from WikiTree for the given person ID. */
|
||||||
async function getAncestors(key: string, handleCors: boolean) {
|
async function getAncestors(key: string, handleCors: boolean) {
|
||||||
const response = await wikiTreeGet(
|
const response = await wikiTreeGet(
|
||||||
{
|
{
|
||||||
@@ -57,6 +62,7 @@ async function getAncestors(key: string, handleCors: boolean) {
|
|||||||
return response[0].ancestors as Person[];
|
return response[0].ancestors as Person[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Retrieves relatives from WikiTree for the given array of person IDs. */
|
||||||
async function getRelatives(keys: string[], handleCors: boolean) {
|
async function getRelatives(keys: string[], handleCors: boolean) {
|
||||||
const response = await wikiTreeGet(
|
const response = await wikiTreeGet(
|
||||||
{
|
{
|
||||||
@@ -70,13 +76,18 @@ async function getRelatives(keys: string[], handleCors: boolean) {
|
|||||||
return response[0].items.map((x: {person: Person}) => x.person) as Person[];
|
return response[0].items.map((x: {person: Person}) => x.person) as Person[];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFamilyId(id1: number, id2: number) {
|
/** Creates a family identifier given 2 spouse identifiers. */
|
||||||
if (id2 > id1) {
|
function getFamilyId(spouse1: number, spouse2: number) {
|
||||||
return `${id1}_${id2}`;
|
if (spouse2 > spouse1) {
|
||||||
|
return `${spouse1}_${spouse2}`;
|
||||||
}
|
}
|
||||||
return `${id2}_${id1}`;
|
return `${spouse2}_${spouse1}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads data from WikiTree to populate an hourglass chart starting from the
|
||||||
|
* given person ID.
|
||||||
|
*/
|
||||||
export async function loadWikiTree(
|
export async function loadWikiTree(
|
||||||
key: string,
|
key: string,
|
||||||
handleCors: boolean,
|
handleCors: boolean,
|
||||||
@@ -178,8 +189,16 @@ export async function loadWikiTree(
|
|||||||
return fam;
|
return fam;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create a GEDCOM structure only for the purpose of displaying the details
|
const gedcom = buildGedcom(indis);
|
||||||
// panel.
|
return {chartData: {indis, fams}, gedcom};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a GEDCOM structure for the purpose of displaying the details
|
||||||
|
* panel.
|
||||||
|
*/
|
||||||
|
function buildGedcom(indis: JsonIndi[]): GedcomData {
|
||||||
const gedcomIndis: {[key: string]: GedcomEntry} = {};
|
const gedcomIndis: {[key: string]: GedcomEntry} = {};
|
||||||
indis.forEach((indi) => {
|
indis.forEach((indi) => {
|
||||||
gedcomIndis[indi.id] = {
|
gedcomIndis[indi.id] = {
|
||||||
@@ -206,12 +225,10 @@ export async function loadWikiTree(
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const gedcom: GedcomData = {
|
return {
|
||||||
head: {level: 0, pointer: '', tag: 'HEAD', data: '', tree: []},
|
head: {level: 0, pointer: '', tag: 'HEAD', data: '', tree: []},
|
||||||
indis: gedcomIndis,
|
indis: gedcomIndis,
|
||||||
fams: {},
|
fams: {},
|
||||||
other: {},
|
other: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
return {chartData: {indis, fams}, gedcom};
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user