@imerss/inat-components 1.1.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PUBLISH.md +13 -0
- package/README.md +20 -0
- package/dist/index.d.ts +86 -83
- package/dist/index.js +280 -395
- package/dist/standalone.min.js +3 -0
- package/package.json +17 -5
- package/public/standalone.html +55 -0
- package/rollup.config.js +15 -4
- package/src/__shared/api/common-taxa.ts +3 -3
- package/src/__shared/api/favourites.ts +3 -3
- package/src/__shared/api/recent-observations.ts +3 -3
- package/src/__shared/api/summary.ts +1 -0
- package/src/__shared/constants.ts +3 -2
- package/src/__shared/index.ts +1 -1
- package/src/__shared/typings.ts +49 -57
- package/src/__shared/utils/configUtils.ts +7 -7
- package/src/__shared/utils/dateUtils.ts +1 -1
- package/src/components/common-taxa/common-taxa.tsx +22 -57
- package/src/components/error/error.tsx +1 -1
- package/src/components/favourites/favourites.tsx +23 -58
- package/src/components/hooks/hooks.tsx +20 -10
- package/src/components/loader/loader.tsx +1 -1
- package/src/components/observation/observation.tsx +1 -1
- package/src/components/recent-observations/recent-observations.tsx +24 -58
- package/src/components/summary/ObserverList.tsx +3 -3
- package/src/components/summary/summary.tsx +53 -54
- package/src/components/tabs/tabs.tsx +41 -26
- package/src/components/taxon-panel/taxon-panel.tsx +75 -96
- package/src/components/year-dropdown/year-dropdown.tsx +11 -7
- package/src/hooks/useLoadSourceData.ts +81 -0
- package/src/index.ts +1 -1
- package/src/types/index.d.ts +17 -0
- package/src/types/standalone.d.ts +6 -0
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
import React, {useState} from "react";
|
|
2
|
-
import {getCurrentYear} from "../../__shared";
|
|
1
|
+
import React, { useEffect, useState } from "react";
|
|
2
|
+
import { getCurrentYear } from "../../__shared";
|
|
3
3
|
import styles from "./year-dropdown.module.scss";
|
|
4
4
|
|
|
5
5
|
interface YearsProps {
|
|
6
6
|
value: string;
|
|
7
7
|
onChange: (year: string) => void;
|
|
8
|
+
numYears: number;
|
|
8
9
|
className?: string;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
const Years = ({ value, onChange, className }: YearsProps) => {
|
|
12
|
-
const [years] = useState(
|
|
12
|
+
const Years = ({ value, onChange, numYears, className }: YearsProps) => {
|
|
13
|
+
const [years, setYears] = useState([]);
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
13
16
|
const currentYear = getCurrentYear();
|
|
14
17
|
const years: any = [];
|
|
15
|
-
for (let i = currentYear; i
|
|
18
|
+
for (let i = currentYear; i>currentYear-numYears; i--) {
|
|
16
19
|
years.push(i);
|
|
17
20
|
}
|
|
18
|
-
|
|
19
|
-
});
|
|
21
|
+
setYears(years);
|
|
22
|
+
}, [numYears]);
|
|
20
23
|
|
|
21
24
|
let classes = styles.dropdown;
|
|
22
25
|
if (className) {
|
|
23
26
|
classes += ` ${className}`;
|
|
24
27
|
}
|
|
28
|
+
|
|
25
29
|
return (
|
|
26
30
|
<select className={classes} onChange={(e) => onChange(e.target.value)} defaultValue={value}>
|
|
27
31
|
<option value="all">All years</option>
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { DataSource, DataSourceEnum } from "../__shared";
|
|
3
|
+
|
|
4
|
+
type UseLoadSourceData = {
|
|
5
|
+
action: any;
|
|
6
|
+
taxonId?: number;
|
|
7
|
+
placeId?: number;
|
|
8
|
+
numResults?: number;
|
|
9
|
+
source?: DataSource;
|
|
10
|
+
dataUrl?: string;
|
|
11
|
+
year?: number | string,
|
|
12
|
+
isSummaryData?: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* This hook handles loading the iNat data from whatever source has been specified (DataSource):
|
|
17
|
+
* nothing needs to be done.
|
|
18
|
+
* - "autoLoad": the user is developing locally and wants to just ping iNat directly for the source
|
|
19
|
+
* - "url": the user has generated the data sources at a URL somewhere via @imerss/inat-component-utils
|
|
20
|
+
*/
|
|
21
|
+
export const useLoadSourceData = ({
|
|
22
|
+
taxonId,
|
|
23
|
+
placeId,
|
|
24
|
+
numResults,
|
|
25
|
+
source,
|
|
26
|
+
dataUrl,
|
|
27
|
+
year,
|
|
28
|
+
action,
|
|
29
|
+
isSummaryData = false
|
|
30
|
+
}: UseLoadSourceData) => {
|
|
31
|
+
const [loading, setLoading] = useState(false);
|
|
32
|
+
const [results, setResults] = useState<any>([]);
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (source === DataSourceEnum.autoLoad) {
|
|
36
|
+
if (!taxonId) {
|
|
37
|
+
console.error("Please supply a `taxonId` prop for the `autoLoad` source prop option.");
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (!placeId) {
|
|
41
|
+
console.error("Please supply a `placeId` prop for the `autoLoad` source prop option.");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
(async () => {
|
|
46
|
+
setLoading(true);
|
|
47
|
+
|
|
48
|
+
// note that `year` isn't used for recentObservations, but we pass it anyway to get more reusability out
|
|
49
|
+
// of this hook
|
|
50
|
+
const obs = await action({ taxonId, placeId, numResults, year });
|
|
51
|
+
setResults(obs.results);
|
|
52
|
+
setLoading(false);
|
|
53
|
+
})();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (source === DataSourceEnum.url) {
|
|
57
|
+
if (!dataUrl) {
|
|
58
|
+
console.error("Please supply a `dataUrl` prop for the `url` source prop option.");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
(async () => {
|
|
63
|
+
setLoading(true);
|
|
64
|
+
const obs = await fetch(dataUrl);
|
|
65
|
+
const json = await obs.json();
|
|
66
|
+
|
|
67
|
+
// hack workaround. Better solution would have been to standardize the JSON structure at the top-level
|
|
68
|
+
if (isSummaryData) {
|
|
69
|
+
setResults(json);
|
|
70
|
+
} else {
|
|
71
|
+
setResults(json.results);
|
|
72
|
+
}
|
|
73
|
+
setLoading(false);
|
|
74
|
+
})();
|
|
75
|
+
}
|
|
76
|
+
}, [source, dataUrl, taxonId, placeId, year, numResults, isSummaryData]);
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
loading, results
|
|
80
|
+
};
|
|
81
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TaxonPanelProps } from "../components/taxon-panel/taxon-panel";
|
|
2
|
+
|
|
3
|
+
export {};
|
|
4
|
+
declare global {
|
|
5
|
+
interface Window {
|
|
6
|
+
inatComponents: {
|
|
7
|
+
initTaxonPanel: (domId: string, taxonPanelProps: TaxonPanelProps) => void;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
window.initStandalone = window.initStandalone || {};
|
|
13
|
+
|
|
14
|
+
export enum DataSource {
|
|
15
|
+
autoLoad = "autoLoad",
|
|
16
|
+
url = "url"
|
|
17
|
+
}
|