Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 195 additions & 47 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"@storybook/testing-library": "^0.0.11",
"@types/d3-ease": "^1.0.9",
"@types/node": "^14.0.26",
"@types/react": "^16.9.43",
"@types/react": "^16.9.49",
"@types/react-map-gl": "^5.2.7",
"@types/styled-jsx": "^2.2.8",
"@types/uuid": "^8.3.0",
Expand Down
54 changes: 52 additions & 2 deletions pages/[p].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { ErrorHandlingContext } from '../src/features/common/Layout/ErrorHandlin
import { ProjectPropsContext } from '../src/features/common/Layout/ProjectPropsContext';
import Credits from '../src/features/projects/components/maps/Credits';
import SingleProjectDetails from '../src/features/projects/screens/SingleProjectDetails';
import { ThemeContext } from '../src/theme/themeContext';
import { getRequest } from '../src/utils/apiRequests/api';
import getStoredCurrency from '../src/utils/countryCurrency/getStoredCurrency';
import GetProjectMeta from '../src/utils/getMetaTags/GetProjectMeta';
import { getAllPlantLocations } from '../src/utils/maps/plantLocations';
import i18next from '../i18n';
import { SingleProjectGeojson } from '../src/features/common/types/project';
import { Treemapper } from '../src/features/user/TreeMapper/Treemapper';

const { useTranslation } = i18next;

Expand All @@ -28,11 +29,15 @@ export default function Donate({
const [internalCurrencyCode, setInternalCurrencyCode] = React.useState('');
const [internalLanguage, setInternalLanguage] = React.useState('');
const [open, setOpen] = React.useState(false);
const { theme } = React.useContext(ThemeContext);

const { i18n } = useTranslation();
const {
geoJson,
project,
setSelectedSite,
setProject,
setSelectedPl,
plantLocations,
setShowSingleProject,
setZoomLevel,
setPlantLocations,
Expand Down Expand Up @@ -116,6 +121,51 @@ export default function Donate({
}
}
}, [router.asPath]);

React.useEffect(() => {
if (geoJson && !router.query.site && !router.query.ploc) {
router.push(
`/${project.slug}?site=${geoJson.features[0].properties.name}`,
undefined,
{ shallow: true }
);
}
}, [router, geoJson]);

React.useEffect(() => {
//for selecting one of the site of project if user use link to directly visit to site from home page
if (geoJson && router.query.site) {
const siteIndex: number = geoJson?.features.findIndex(
(singleSite: SingleProjectGeojson) => {
return router.query.site === singleSite?.properties.name;
}
);
if (siteIndex === -1) {
router.push(`/${project.slug}`);
} else {
setSelectedSite(siteIndex);
}
}
}, [setSelectedSite, geoJson]);

React.useEffect(() => {
//for selecting one of the plant location. if user use link to directly visit to plantLocation from home page
if (geoJson && router.query.ploc && plantLocations) {
const singlePlantLocation: Treemapper.PlantLocation | undefined =
plantLocations?.find(
(dataOfSinglePlantLocation: Treemapper.PlantLocation) => {
return router.query.ploc === dataOfSinglePlantLocation?.hid;
}
);

if (singlePlantLocation === undefined) {
router.push(`/${project.slug}`);
Comment thread
prachigarg19 marked this conversation as resolved.
} else {
setSelectedPl(singlePlantLocation);
}
}
}, [router, router.query.ploc, plantLocations, setSelectedPl, project]);

return (
<>
{project ? <GetProjectMeta {...ProjectProps} /> : null}
Expand Down
4 changes: 1 addition & 3 deletions src/features/common/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ const Layout: FC = ({ children }) => {
return (
<>
<Header />
<style jsx global>
{theme}
</style>
<style>{theme}</style>
<div className={`${themeType}`}>
{!isEmbed && <Navbar theme={themeType} />}
<div>{children}</div>
Expand Down
27 changes: 27 additions & 0 deletions src/features/common/types/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@ export interface SingleProject {
currency: string;
unitCost: number;
}

export interface SingleProjectGeojson {
geometry: Geometry
type: string
properties: Properties
}

export interface Geometry {
coordinates: number[][][]
type: string
}

export interface Properties {
lastUpdated: LastUpdated
name: string
description: string
id: string
status: string
}

export interface LastUpdated {
date: string
timezone: string
timezone_type: number
}


3 changes: 2 additions & 1 deletion src/features/projects/components/ProjectSnippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export default function ProjectSnippet({
? getImageUrl('project', 'medium', project.image)
: '';

const { selectedPl, hoveredPl } = React.useContext(ProjectPropsContext);
const { selectedPl, hoveredPl, selectedSite } =
React.useContext(ProjectPropsContext);

let progressPercentage = (project.countPlanted / project.countTarget) * 100;

Expand Down
1 change: 1 addition & 0 deletions src/features/projects/components/ProjectsMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export default function ProjectsMap(): ReactElement {
const element = plantLocations[key];
if (element.id === e.features[0].layer?.source) {
setSelectedPl(element);

break;
}
}
Expand Down
44 changes: 42 additions & 2 deletions src/features/projects/components/maps/Project.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ErrorHandlingContext } from '../../../common/Layout/ErrorHandlingContex
import { ProjectPropsContext } from '../../../common/Layout/ProjectPropsContext';
import Location from './Location';
import Sites from './Sites';
import { useRouter } from 'next/router';
import { zoomToPlantLocation } from '../../../../../src/utils/maps/plantLocations';

interface Props {
project: Object;
Expand All @@ -19,6 +21,8 @@ export default function Project({
setViewPort,
}: Props): ReactElement {
const {
selectedPl,
plantLocations,
geoJson,
selectedSite,
siteExists,
Expand All @@ -29,6 +33,9 @@ export default function Project({
} = React.useContext(ProjectPropsContext);

const { handleError } = React.useContext(ErrorHandlingContext);
const router = useRouter();
const [plantPolygonCoordinates, setPlantPolygonCoordinates] =
React.useState(null);

async function loadRasterData() {
const result = await getRasterData('', handleError);
Expand All @@ -43,8 +50,21 @@ export default function Project({
setRasterData({ ...rasterData, imagery: result.imagery });
}
}

React.useEffect(() => {
if (plantLocations && selectedPl) {
setPlantPolygonCoordinates(selectedPl?.geometry.coordinates[0]);
}
}, [router, selectedPl, plantLocations]);

React.useEffect(() => {
if (selectedPl && plantPolygonCoordinates) {
router.push(`/${project.slug}?ploc=${selectedPl?.hid}`);
}
}, [selectedPl, plantPolygonCoordinates]);

React.useEffect(() => {
if (siteExists) {
if (siteExists && !router.query.ploc) {
loadRasterData();
zoomToProjectSite(
{
Expand All @@ -57,6 +77,19 @@ export default function Project({
setSiteViewPort,
4000
);
} else if (
plantPolygonCoordinates &&
plantLocations &&
router.query.ploc &&
selectedPl
) {
zoomToPlantLocation(
plantPolygonCoordinates,
viewport,
isMobile,
setViewPort,
1200
);
} else {
zoomToLocation(
viewport,
Expand All @@ -67,7 +100,14 @@ export default function Project({
3000
);
}
}, [project, siteExists]);
}, [
project,
siteExists,
plantLocations,
router.query.ploc,
selectedPl,
plantPolygonCoordinates,
]);

//Props
const locationProps = {
Expand Down
39 changes: 27 additions & 12 deletions src/features/projects/components/maps/SitesDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,50 @@ import styles from '../../styles/ProjectsMap.module.scss';
import BootstrapInput from '../../../common/InputTypes/BootstrapInput';
import { ProjectPropsContext } from '../../../common/Layout/ProjectPropsContext';
import { ParamsContext } from '../../../common/Layout/QueryParamsContext';
import { SingleProjectGeojson } from '../../../../features/common/types/project';
interface Props {}

export default function SitesDropdown(): ReactElement {
const {
setSelectedPl,
geoJson,
project,
selectedSite,
setSelectedSite,
isMobile,
isPolygonMenuOpen,
setIsPolygonMenuOpen,
} = React.useContext(ProjectPropsContext);
const { embed } = React.useContext(ParamsContext);
const { pathname } = useRouter();
const router = useRouter();

const handleChangeSite = (event: React.ChangeEvent<{ value: unknown }>) => {
setSelectedSite(event.target.value as string);
setSelectedPl(null);
setSelectedSite(event.target.value as number);
router.push(
`/${project.slug}/?site=${
geoJson.features[event.target.value].properties.name
}`
);

if (isMobile) setIsPolygonMenuOpen(false);
};

const dropdownContainerClasses = `${
embed === 'true' ? styles.embed_dropdownContainer : styles.dropdownContainer
} ${
pathname === '/[p]' ? styles['dropdownContainer--reduce-right-offset'] : ''
router.pathname === '/[p]'
? styles['dropdownContainer--reduce-right-offset']
: ''
}`;

const projectSitesButtonClasses = `${
embed === 'true'
? styles.embed_projectSitesButton
: styles.projectSitesButton
} ${
pathname === '/[p]' ? styles['projectSitesButton--reduce-right-offset'] : ''
router.pathname === '/[p]'
? styles['projectSitesButton--reduce-right-offset']
: ''
}`;

return (
Expand Down Expand Up @@ -69,13 +82,15 @@ export default function SitesDropdown(): ReactElement {
onChange={handleChangeSite}
input={<BootstrapInput />}
>
{geoJson.features.map((site: any, index: any) => {
return (
<option key={index} value={index}>
{site.properties.name}
</option>
);
})}
{geoJson?.features.map(
(site: SingleProjectGeojson, index: number) => {
return (
<option key={index} value={index}>
{site.properties.name}
</option>
);
}
)}
</NativeSelect>
</FormControl>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/features/user/TreeMapper/Treemapper.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare namespace Treemapper {
export declare namespace Treemapper {
export interface DeviceLocation {
coordinates: number[];
type: string;
Expand Down
7 changes: 4 additions & 3 deletions src/utils/maps/plantLocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import * as turf from '@turf/turf';
import { getRequest } from '../apiRequests/api';

export function zoomToPlantLocation(
geoJson: Object,
coordinates: any,
viewport: Object,
isMobile: boolean,
setViewPort: Function,
duration = 1200
) {
if (viewport.width && viewport.height) {
const bbox = turf.bbox(geoJson);
const polygon = turf.polygon([coordinates])
const bbox = turf.bbox(polygon);
const { longitude, latitude, zoom } = new WebMercatorViewport(
viewport
).fitBounds(
Expand All @@ -38,7 +39,7 @@ export function zoomToPlantLocation(
transitionEasing: d3.easeCubic,
};
setViewPort(newViewport);
} else {
} else {
const newViewport = {
...viewport,
height: window.innerHeight,
Expand Down
7 changes: 5 additions & 2 deletions src/utils/maps/zoomToProjectSite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export default function zoomToProjectSite(
//console.log("zoomToProjectSite", viewport, viewport.width, window.innerWidth, viewport.height, window.innerHeight);

const bbox = turf.bbox(geoJson.features[selectedSite]);


const { longitude, latitude, zoom } = new WebMercatorViewport(
viewport
).fitBounds(
Expand All @@ -42,8 +44,8 @@ export default function zoomToProjectSite(
let defaultZoom = 15;
if (zoom < defaultZoom) {
defaultZoom = zoom;
}

}
const newViewport = {
...viewport,
longitude,
Expand All @@ -53,6 +55,7 @@ export default function zoomToProjectSite(
transitionInterpolator: new FlyToInterpolator(),
transitionEasing: d3.easeCubic,
};

setViewPort(newViewport);
setSiteViewPort({ center: [longitude, latitude], zoom: defaultZoom });
} else {
Expand Down