@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.
@@ -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>=currentYear-10; i--) {
18
+ for (let i = currentYear; i>currentYear-numYears; i--) {
16
19
  years.push(i);
17
20
  }
18
- return years;
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
@@ -12,4 +12,4 @@ export * from "./components/summary/summary";
12
12
  // all typings
13
13
  export * from "./__shared";
14
14
 
15
- export default TaxonPanel;
15
+ export default TaxonPanel;
@@ -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
+ }
@@ -0,0 +1,6 @@
1
+ interface InitStandaloneProps {
2
+ taxonId: number;
3
+ placeId: number;
4
+ }
5
+
6
+ export type InitStandalone = (id: string, props: InitStandaloneProps) => void;