@imerss/inat-components 1.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.
Files changed (60) hide show
  1. package/README.md +0 -0
  2. package/declaration.d.ts +2 -0
  3. package/dist/index.d.ts +214 -0
  4. package/dist/index.js +876 -0
  5. package/package.json +53 -0
  6. package/public/favicon.ico +0 -0
  7. package/public/index.html +20 -0
  8. package/public/logo192.png +0 -0
  9. package/public/logo512.png +0 -0
  10. package/public/manifest.json +25 -0
  11. package/public/robots.txt +3 -0
  12. package/rollup.config.js +36 -0
  13. package/src/__shared/api/common-taxa.ts +56 -0
  14. package/src/__shared/api/favourites.ts +61 -0
  15. package/src/__shared/api/index.ts +4 -0
  16. package/src/__shared/api/recent-observations.ts +43 -0
  17. package/src/__shared/api/summary.ts +84 -0
  18. package/src/__shared/constants.ts +4 -0
  19. package/src/__shared/index.ts +4 -0
  20. package/src/__shared/typings.ts +69 -0
  21. package/src/__shared/utils/configUtils.ts +21 -0
  22. package/src/__shared/utils/dateUtils.ts +14 -0
  23. package/src/__shared/utils/index.ts +3 -0
  24. package/src/__shared/utils/numberUtils.ts +1 -0
  25. package/src/components/common-taxa/__tests__/common-taxa.test.tsx +14 -0
  26. package/src/components/common-taxa/common-taxa.tsx +99 -0
  27. package/src/components/error/__tests__/error.test.tsx +9 -0
  28. package/src/components/error/error.module.scss +7 -0
  29. package/src/components/error/error.tsx +15 -0
  30. package/src/components/favourites/__tests__/favourites.test.tsx +13 -0
  31. package/src/components/favourites/favourites.tsx +98 -0
  32. package/src/components/hooks/hooks.tsx +24 -0
  33. package/src/components/loader/__tests__/loader.test.tsx +10 -0
  34. package/src/components/loader/loader.tsx +26 -0
  35. package/src/components/no-results/__tests__/no-results.test.tsx +9 -0
  36. package/src/components/no-results/no-results.tsx +9 -0
  37. package/src/components/observation/__tests__/observation.test.tsx +14 -0
  38. package/src/components/observation/observation.module.scss +20 -0
  39. package/src/components/observation/observation.tsx +29 -0
  40. package/src/components/recent-observations/__tests__/recent-observations.test.tsx +9 -0
  41. package/src/components/recent-observations/recent-observations.tsx +101 -0
  42. package/src/components/seasonality-graph/__tests__/seasonality-graph.test.tsx +9 -0
  43. package/src/components/seasonality-graph/seasonality-graph.module.scss +7 -0
  44. package/src/components/seasonality-graph/seasonality-graph.tsx +46 -0
  45. package/src/components/shared/css/general.module.scss +69 -0
  46. package/src/components/summary/ObserverList.tsx +18 -0
  47. package/src/components/summary/__tests__/summary.test.tsx +18 -0
  48. package/src/components/summary/summary.module.scss +88 -0
  49. package/src/components/summary/summary.tsx +83 -0
  50. package/src/components/tabs/__tests__/tabs.test.tsx +16 -0
  51. package/src/components/tabs/tabs.module.scss +28 -0
  52. package/src/components/tabs/tabs.tsx +44 -0
  53. package/src/components/taxon-panel/__tests__/taxon-panel.test.tsx +10 -0
  54. package/src/components/taxon-panel/taxon-panel.module.scss +7 -0
  55. package/src/components/taxon-panel/taxon-panel.tsx +104 -0
  56. package/src/components/year-dropdown/__tests__/year-dropdown.test.tsx +14 -0
  57. package/src/components/year-dropdown/year-dropdown.module.scss +3 -0
  58. package/src/components/year-dropdown/year-dropdown.tsx +33 -0
  59. package/src/index.ts +15 -0
  60. package/tsconfig.json +6 -0
@@ -0,0 +1,13 @@
1
+ import {render} from '@testing-library/react';
2
+ import {Favourites} from '../favourites';
3
+
4
+ const defaultProps = {
5
+ year: 2022
6
+ };
7
+
8
+ describe('Favourites', () => {
9
+ it('should render successfully', () => {
10
+ const {baseElement} = render(<Favourites {...defaultProps} />);
11
+ expect(baseElement).toBeTruthy();
12
+ });
13
+ });
@@ -0,0 +1,98 @@
1
+ import React, {useEffect, useState} from "react";
2
+ import {formatDate, C, getFavourites, FavouritesData, BaseComponentProps, DataSource} from "../../__shared";
3
+ import {Observation} from "../observation/observation";
4
+ import Loader from "../loader/loader";
5
+ import {NoResults} from "../no-results/no-results";
6
+ import generalStyles from "../shared/css/general.module.scss";
7
+
8
+ export type FavouritesProps = BaseComponentProps & {
9
+ year: string | number;
10
+ }
11
+
12
+ export const FavouritesLabel = (data: FavouritesData) => (
13
+ <div className={generalStyles.obsLabel}>
14
+ <h3>{data.taxonCommonName || data.taxonName}</h3>
15
+ <div>{data.observerUsername}</div>
16
+ <div>{formatDate(data.obsDate)}</div>
17
+ <label className={generalStyles.count}>{data.numFaves}</label>
18
+ </div>
19
+ );
20
+
21
+ export const Favourites = ({
22
+ year,
23
+ source,
24
+ taxonId,
25
+ placeId,
26
+ data,
27
+ dataUrl,
28
+ components,
29
+ className,
30
+ perPage = C.PER_PAGE
31
+ }: FavouritesProps) => {
32
+ const [observations, setObservations] = useState<any>(() => (source === DataSource.dataProp) ? data : []);
33
+ const [loading, setLoading] = useState(false);
34
+
35
+ useEffect(() => {
36
+ if (source !== DataSource.autoLoad) {
37
+ return;
38
+ }
39
+ if (!taxonId) {
40
+ console.error("Please supply a `taxonId` prop for the `autoLoad` source prop option.");
41
+ return;
42
+ }
43
+ if (!placeId) {
44
+ console.error("Please supply a `placeId` prop for the `autoLoad` source prop option.");
45
+ return;
46
+ }
47
+
48
+ (async () => {
49
+ setLoading(true);
50
+ const resp = await getFavourites({taxonId, placeId, year, perPage});
51
+ setObservations(resp.results);
52
+ setLoading(false);
53
+ })();
54
+ }, [source, year, placeId, taxonId, perPage]);
55
+
56
+ useEffect(() => {
57
+ if (source !== DataSource.url) {
58
+ return;
59
+ }
60
+
61
+ if (!dataUrl) {
62
+ console.error("Please supply a `dataUrl` prop for the `url` source prop option.");
63
+ return;
64
+ }
65
+
66
+ (async () => {
67
+ setLoading(true);
68
+ const obs = await fetch(dataUrl);
69
+ const json = await obs.json();
70
+ setObservations(json.results);
71
+ setLoading(false);
72
+ })();
73
+ }, [source, dataUrl]);
74
+
75
+ const Load = components?.loader ? components.loader as any : Loader;
76
+ const Label = components?.label ? components.label as any : FavouritesLabel;
77
+
78
+ let classes = generalStyles.panel;
79
+ if (className) {
80
+ classes += ` ${className}`;
81
+ }
82
+ return (
83
+ <div className={classes}>
84
+ <Load loading={loading}/>
85
+ {!loading && observations.length === 0 && <NoResults/>}
86
+ <div className={generalStyles.grid}>
87
+ {observations.map((obs: any) => (
88
+ <Observation
89
+ key={obs.id}
90
+ imageUrl={obs.imageUrl.replace(/square/, "medium")}
91
+ linkUrl={obs.obsUrl}>
92
+ <Label {...obs} />
93
+ </Observation>
94
+ ))}
95
+ </div>
96
+ </div>
97
+ );
98
+ };
@@ -0,0 +1,24 @@
1
+ import {useEffect, useState} from "react";
2
+ import {Tab} from "../../__shared";
3
+
4
+ const defaultTitles = {
5
+ [Tab.recent]: "Recent",
6
+ [Tab.commonTaxa]: "Most common",
7
+ [Tab.favourites]: "Most favourited",
8
+ [Tab.stats]: "General stats"
9
+ };
10
+
11
+ export const useFeatureTitles = (features: any) => {
12
+ const [titles, setTitles] = useState(defaultTitles);
13
+
14
+ useEffect(() => {
15
+ setTitles({
16
+ [Tab.recent]: features.recentObservations?.label || defaultTitles[Tab.recent],
17
+ [Tab.commonTaxa]: features.commonTaxa?.label || defaultTitles[Tab.commonTaxa],
18
+ [Tab.favourites]: features.favourites?.label || defaultTitles[Tab.favourites],
19
+ [Tab.stats]: features.stats?.label || defaultTitles[Tab.stats]
20
+ });
21
+ }, [features]);
22
+
23
+ return titles;
24
+ };
@@ -0,0 +1,10 @@
1
+ import {render} from '@testing-library/react';
2
+
3
+ import Loader from '../loader';
4
+
5
+ describe('Loader', () => {
6
+ it('should render successfully', () => {
7
+ const {baseElement} = render(<Loader loading={true} />);
8
+ expect(baseElement).toBeTruthy();
9
+ });
10
+ });
@@ -0,0 +1,26 @@
1
+ import React from "react";
2
+ import LoadingSpinner from "react-spinners/MoonLoader.js"; // TODO. Interesting. Why is this required?
3
+ import styles from "../shared/css/general.module.scss";
4
+
5
+ export type LoaderProps = {
6
+ loading: boolean;
7
+ };
8
+
9
+ const Loader = ({ loading }: LoaderProps) => {
10
+ let loaderClasses = styles.loader;
11
+ // if (loading) {
12
+ // loaderClasses += ` ${styles.loading}`;
13
+ // }
14
+ if (!loading) {
15
+ return null;
16
+ }
17
+
18
+ return (
19
+ <div className={loaderClasses}>
20
+ <div className={styles.loaderBg} />
21
+ <LoadingSpinner color="#000000" />
22
+ </div>
23
+ );
24
+ };
25
+
26
+ export default Loader;
@@ -0,0 +1,9 @@
1
+ import {render} from '@testing-library/react';
2
+ import {NoResults} from '../no-results';
3
+
4
+ describe('NoResults', () => {
5
+ it('should render successfully', () => {
6
+ const {baseElement} = render(<NoResults/>);
7
+ expect(baseElement).toBeTruthy();
8
+ });
9
+ });
@@ -0,0 +1,9 @@
1
+ import React from "react";
2
+
3
+ export const NoResults = () => {
4
+ return (
5
+ <p>
6
+ No results found.
7
+ </p>
8
+ );
9
+ };
@@ -0,0 +1,14 @@
1
+ import {render} from '@testing-library/react';
2
+ import {Observation} from '../observation';
3
+
4
+ const defaultProps = {
5
+ linkUrl: "",
6
+ imageUrl: "",
7
+ };
8
+
9
+ describe('Observation', () => {
10
+ it('should render successfully', () => {
11
+ const {baseElement} = render(<Observation {...defaultProps}>test</Observation>);
12
+ expect(baseElement).toBeTruthy();
13
+ });
14
+ });
@@ -0,0 +1,20 @@
1
+ .obs {
2
+ cursor: pointer;
3
+ border-radius: 8px;
4
+ background-color: #f9f9f9;
5
+ text-align: center;
6
+ transition: all 0.15s ease-in-out;
7
+
8
+ &:hover {
9
+ background-color: #e1e1e1;
10
+ }
11
+ }
12
+
13
+ .obs h3 {
14
+ margin: 0;
15
+ }
16
+
17
+ .image {
18
+ max-width: 100%;
19
+ max-height: 100%;
20
+ }
@@ -0,0 +1,29 @@
1
+ import React, {useCallback} from 'react';
2
+ import styles from "./observation.module.scss";
3
+
4
+ export type ObservationProps = {
5
+ imageUrl: string;
6
+ linkUrl: string;
7
+ children: any;
8
+ };
9
+
10
+ export const Observation = ({imageUrl, linkUrl, children}: ObservationProps) => {
11
+ const gotoObservation = useCallback(() => {
12
+ window.open(linkUrl);
13
+ }, [linkUrl]);
14
+
15
+ return (
16
+ <article className={styles.obs} onClick={gotoObservation}>
17
+ <div style={{
18
+ width: 200,
19
+ height: 200,
20
+ overflow: "hidden",
21
+ borderRadius: 5,
22
+ background: `url(${imageUrl}) 50% 50% no-repeat`,
23
+ margin: "0 auto",
24
+ backgroundSize: "cover"
25
+ }}/>
26
+ {children}
27
+ </article>
28
+ );
29
+ };
@@ -0,0 +1,9 @@
1
+ import {render} from '@testing-library/react';
2
+ import {RecentObservations} from '../recent-observations';
3
+
4
+ describe('RecentObservations', () => {
5
+ it('should render successfully', () => {
6
+ const {baseElement} = render(<RecentObservations/>);
7
+ expect(baseElement).toBeTruthy();
8
+ });
9
+ });
@@ -0,0 +1,101 @@
1
+ import React, {useEffect, useState} from "react";
2
+ import {formatDate, C, getRecentObservations, RecentObservationData, BaseComponentProps, DataSource} from "../../__shared";
3
+ import {Observation} from "../observation/observation";
4
+ import Loader from "../loader/loader";
5
+ import {NoResults} from "../no-results/no-results";
6
+ import styles from "../shared/css/general.module.scss";
7
+
8
+ export type RecentObservationsProps = BaseComponentProps;
9
+
10
+ export const RecentObservationLabel = (obs: RecentObservationData) => (
11
+ <div className={styles.obsLabel}>
12
+ <h3>{obs.taxonCommonName || obs.taxonName}</h3>
13
+ <div>{formatDate(obs.obsDate)}</div>
14
+ <div>
15
+ <a href={`${C.BASE_URL}/people/${obs.observerUsername}`}
16
+ target="_blank"
17
+ rel="noreferrer"
18
+ onClick={(e) => e.stopPropagation()}>
19
+ {obs.observerUsername}
20
+ </a>
21
+ </div>
22
+ </div>
23
+ );
24
+
25
+ export const RecentObservations = ({
26
+ taxonId,
27
+ placeId,
28
+ data,
29
+ dataUrl,
30
+ source = DataSource.autoLoad,
31
+ perPage = C.PER_PAGE,
32
+ components,
33
+ className
34
+ }: RecentObservationsProps) => {
35
+ const [loading, setLoading] = useState(false);
36
+ const [observations, setObservations] = useState<any>(() => (source === DataSource.dataProp) ? data : []);
37
+
38
+ useEffect(() => {
39
+ if (source !== DataSource.autoLoad) {
40
+ return;
41
+ }
42
+ if (!taxonId) {
43
+ console.error("Please supply a `taxonId` prop for the `autoLoad` source prop option.");
44
+ return;
45
+ }
46
+ if (!placeId) {
47
+ console.error("Please supply a `placeId` prop for the `autoLoad` source prop option.");
48
+ return;
49
+ }
50
+
51
+ (async () => {
52
+ setLoading(true);
53
+ const obs = await getRecentObservations({taxonId, placeId, perPage});
54
+ setObservations(obs.results);
55
+ setLoading(false);
56
+ })();
57
+ }, [source, taxonId, placeId, perPage]);
58
+
59
+ useEffect(() => {
60
+ if (source !== DataSource.url) {
61
+ return;
62
+ }
63
+
64
+ if (!dataUrl) {
65
+ console.error("Please supply a `dataUrl` prop for the `url` source prop option.");
66
+ return;
67
+ }
68
+
69
+ (async () => {
70
+ setLoading(true);
71
+ const obs = await fetch(dataUrl);
72
+ const json = await obs.json();
73
+ setObservations(json.results);
74
+ setLoading(false);
75
+ })();
76
+ }, [source, dataUrl]);
77
+
78
+ const Load = components?.loader ? components.loader as any : Loader;
79
+ const Label = components?.label ? components.label as any : RecentObservationLabel;
80
+
81
+ let classes = styles.panel;
82
+ if (className) {
83
+ classes += ` ${className}`;
84
+ }
85
+ return (
86
+ <div className={classes}>
87
+ <Load loading={loading}/>
88
+ <div className={styles.grid}>
89
+ {!loading && observations.length === 0 && <NoResults/>}
90
+ {observations.map((obs: RecentObservationData) => (
91
+ <Observation
92
+ key={obs.id}
93
+ imageUrl={obs.imageUrl.replace(/square/, "medium")}
94
+ linkUrl={obs.obsUrl}>
95
+ <Label {...obs} />
96
+ </Observation>
97
+ ))}
98
+ </div>
99
+ </div>
100
+ );
101
+ };
@@ -0,0 +1,9 @@
1
+ import {render} from '@testing-library/react';
2
+ import {SeasonalityGraph} from '../seasonality-graph';
3
+
4
+ describe('SeasonalityGraph', () => {
5
+ it('should render successfully', () => {
6
+ const {baseElement} = render(<SeasonalityGraph/>);
7
+ expect(baseElement).toBeTruthy();
8
+ });
9
+ });
@@ -0,0 +1,7 @@
1
+ /*
2
+ * Replace this with your own classes
3
+ *
4
+ * e.g.
5
+ * .container {
6
+ * }
7
+ */
@@ -0,0 +1,46 @@
1
+ import React from "react";
2
+ import { AxisOptions, Chart } from "react-charts";
3
+ import { format } from "date-fns";
4
+
5
+ export const SeasonalityGraph = ({ data }: any) => {
6
+ const primaryAxis = React.useMemo<
7
+ AxisOptions<typeof data[number]["data"][number]>
8
+ >(
9
+ () => ({
10
+ getValue: (datum: any) => datum.primary as any
11
+ }),
12
+ []
13
+ );
14
+
15
+ const secondaryAxes = React.useMemo<
16
+ AxisOptions<typeof data[number]["data"][number]>[]
17
+ >(
18
+ () => [
19
+ {
20
+ getValue: (datum: any) => datum.secondary,
21
+ elementType: "area"
22
+ }
23
+ ],
24
+ []
25
+ );
26
+
27
+ const formattedData = [
28
+ {
29
+ label: "Observations",
30
+ data: Object.keys(data).map((monthNum) => ({
31
+ primary: format(new Date(2000, parseInt(monthNum, 10)-1, 1), 'MMMM'),
32
+ secondary: data[monthNum]
33
+ }))
34
+ }
35
+ ];
36
+
37
+ return (
38
+ <Chart
39
+ options={{
40
+ data: formattedData,
41
+ primaryAxis,
42
+ secondaryAxes
43
+ }}
44
+ />
45
+ );
46
+ }
@@ -0,0 +1,69 @@
1
+ .page {
2
+ padding: 80px 10px 10px;
3
+ margin: 0 auto;
4
+ max-width: 1100px;
5
+
6
+ h1 {
7
+ border-bottom: 1px solid #efefef;
8
+ padding-bottom: 2px;
9
+ margin-bottom: 25px;
10
+ }
11
+ }
12
+
13
+ .panel {
14
+ position: relative;
15
+ }
16
+
17
+ .grid {
18
+ display: grid;
19
+ grid-template-columns: repeat(auto-fit, minmax(200px, 1fr) );
20
+ width: 100%;
21
+ grid-gap: 8px;
22
+ text-align: center;
23
+ }
24
+
25
+ .obsLabel {
26
+ padding: 10px 20px;
27
+
28
+ a {
29
+ text-decoration: none;
30
+ color: #336699;
31
+
32
+ &:hover {
33
+ text-decoration: underline;
34
+ }
35
+ }
36
+ }
37
+
38
+ .loader {
39
+ position: absolute;
40
+ top: 50px;
41
+ left: 0;
42
+ right: 0;
43
+ z-index: 1;
44
+ margin: 0;
45
+ display: flex;
46
+ align-items: center;
47
+ justify-content: center;
48
+ }
49
+
50
+ .loaderBg {
51
+ position: absolute;
52
+ background-color: #cccccc;
53
+ padding: 20px;
54
+ width: 100px;
55
+ border-radius: 10px;
56
+ height: 100%;
57
+ opacity: 0.8;
58
+ }
59
+
60
+ .count {
61
+ background-color: #efefef;
62
+ color: #666666;
63
+ padding: 2px 8px;
64
+ margin-top: 6px;
65
+ display: inline-block;
66
+ border-radius: 3px;
67
+ font-weight: bold;
68
+ font-size: 18px;
69
+ }
@@ -0,0 +1,18 @@
1
+ import React from "react";
2
+ import {numberWithCommas, Observer, C} from "../../__shared";
3
+ import styles from "./summary.module.scss";
4
+
5
+ export const ObserverList = ({observers}: { observers: Observer[] }) => (
6
+ <ul className={styles.top}>
7
+ {observers.map((obs) => (
8
+ <li key={obs.id}>
9
+ <a href={`${C.BASE_URL}/people/${obs.userName}`} target="_blank" rel="noreferrer">
10
+ {obs.iconUrl ? <img src={obs.iconUrl || ""} className={styles.avatar} alt="User icon"/> :
11
+ <div className={styles.noAvatar}/>}
12
+ <h3>{obs.userName}</h3>
13
+ <label>{numberWithCommas(obs.numObservations)}</label>
14
+ </a>
15
+ </li>
16
+ ))}
17
+ </ul>
18
+ );
@@ -0,0 +1,18 @@
1
+ import {render} from "@testing-library/react";
2
+ import {DataSource} from "../../../index";
3
+ import {Summary} from "../summary";
4
+
5
+ describe("Summary", () => {
6
+ it("should render successfully", () => {
7
+ const {baseElement} = render(
8
+ <Summary
9
+ placeId={1}
10
+ taxonId={1}
11
+ source={DataSource.url}
12
+ year={2020}
13
+ dataUrl=""
14
+ />
15
+ );
16
+ expect(baseElement).toBeTruthy();
17
+ });
18
+ });
@@ -0,0 +1,88 @@
1
+ .summaryPage {
2
+ position: relative;
3
+
4
+ section {
5
+ display: flex;
6
+ }
7
+ }
8
+
9
+ .top {
10
+ list-style-type: none;
11
+ margin: 0;
12
+ padding: 0;
13
+
14
+ .avatar, .noAvatar {
15
+ margin-right: 10px;
16
+ border-radius: 20px;
17
+ width: 40px;
18
+ height: 40px;
19
+ }
20
+
21
+ .noAvatar {
22
+ display: inline-block;
23
+ background-color: #efefef;
24
+ }
25
+
26
+ h3 {
27
+ flex: 1;
28
+ color: black;
29
+ }
30
+
31
+ label {
32
+ flex: 0;
33
+ background-color: #efefef;
34
+ color: #666666;
35
+ padding: 2px 8px;
36
+ margin-top: 6px;
37
+ border-radius: 3px;
38
+ font-weight: bold;
39
+ font-size: 16px;
40
+ }
41
+
42
+ li {
43
+ border-radius: 3px;
44
+
45
+ a {
46
+ display: flex;
47
+ align-items: center;
48
+ text-decoration: none;
49
+ }
50
+
51
+ &:hover {
52
+ background-color: #efefef;
53
+ }
54
+ }
55
+ }
56
+
57
+ .countSummary {
58
+ list-style-type: none;
59
+ line-height: 28px;
60
+ font-size: 16px;
61
+ padding: 0;
62
+ margin: 0 0 30px;
63
+ }
64
+
65
+ .seasonalityBlock {
66
+ flex: 3;
67
+ }
68
+
69
+ .observersBlock {
70
+ flex: 1;
71
+ margin-left: 40px;
72
+ margin-bottom: -40px;
73
+ }
74
+
75
+ @media screen and (max-width: 800px) {
76
+ .summaryPage section {
77
+ display: block;
78
+ }
79
+
80
+ .seasonalityBlock {
81
+ height: 250px;
82
+ margin-bottom: 80px;
83
+ }
84
+
85
+ .observersBlock {
86
+ margin-left: 0;
87
+ }
88
+ }