mirror of
https://github.com/PeWu/topola-viewer.git
synced 2026-07-25 05:01:50 +00:00
Refactoring: Move data loading code under the datasource directory
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import Cookies from 'js-cookie';
|
||||
import {analyticsEvent} from '../util/analytics';
|
||||
import {DataSource, DataSourceEnum, SourceSelection} from './data_source';
|
||||
import {Date, DateOrRange, JsonFam, JsonIndi} from 'topola';
|
||||
import {GedcomData, normalizeGedcom, TopolaData} from '../util/gedcom_util';
|
||||
import {GedcomEntry} from 'parse-gedcom';
|
||||
@@ -559,3 +561,55 @@ function getSet<K, V>(map: Map<K, Set<V>>, key: K): Set<V> {
|
||||
map.set(key, newSet);
|
||||
return newSet;
|
||||
}
|
||||
|
||||
export interface WikiTreeSourceSpec {
|
||||
source: DataSourceEnum.WIKITREE;
|
||||
authcode?: string;
|
||||
}
|
||||
|
||||
/** Loading data from the WikiTree API. */
|
||||
export class WikiTreeDataSource implements DataSource<WikiTreeSourceSpec> {
|
||||
constructor(private intl: InjectedIntl) {}
|
||||
|
||||
isNewData(
|
||||
newSource: SourceSelection<WikiTreeSourceSpec>,
|
||||
oldSource: SourceSelection<WikiTreeSourceSpec>,
|
||||
data?: TopolaData,
|
||||
): boolean {
|
||||
if (!newSource.selection) {
|
||||
return false;
|
||||
}
|
||||
if (oldSource.selection?.id === newSource.selection.id) {
|
||||
// Selection unchanged -> don't reload.
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
data &&
|
||||
data.chartData.indis.some((indi) => indi.id === newSource.selection?.id)
|
||||
) {
|
||||
// New selection exists in current view -> animate instead of reloading.
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
async loadData(
|
||||
source: SourceSelection<WikiTreeSourceSpec>,
|
||||
): Promise<TopolaData> {
|
||||
if (!source.selection) {
|
||||
throw new Error('WikiTree id needs to be provided');
|
||||
}
|
||||
try {
|
||||
const data = await loadWikiTree(
|
||||
source.selection.id,
|
||||
this.intl,
|
||||
source.spec.authcode,
|
||||
);
|
||||
analyticsEvent('wikitree_loaded');
|
||||
return data;
|
||||
} catch (error) {
|
||||
analyticsEvent('wikitree_error');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user