-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from BU-Spark/bugFixes
ui fixes
- Loading branch information
Showing
7 changed files
with
233 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import { Locations } from "../../__generated__/graphql"; | ||
import { Card, CardContent } from '@mui/material'; | ||
|
||
|
||
interface LocationInfoProps { | ||
location: Locations | null; | ||
tract: string; | ||
} | ||
|
||
const LocationInfo: React.FC<LocationInfoProps> = ({ location, tract }) => { | ||
if (!location) { | ||
return <div>No location selected</div>; | ||
} | ||
|
||
return ( | ||
|
||
<Card sx={{ width: "100%", height: "62vh" }}> | ||
<CardContent sx={{ width: "100%", height: "62vh"}} > | ||
<div> | ||
<div><h2>Location Information</h2></div> | ||
<div><strong>Location Name:</strong> {location.value}</div> | ||
<div><strong>Neighborhood:</strong></div> | ||
<div><strong>Tract:</strong> {tract}</div> | ||
</div> | ||
</CardContent> | ||
</Card> | ||
); | ||
}; | ||
|
||
export default LocationInfo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import React, { useEffect } from 'react'; | ||
import { useNavigate, useLocation } from 'react-router-dom'; | ||
import { LocationContext } from "../../contexts/location_context"; | ||
import MapCard from '../../components/MapCard/MapCard'; | ||
import { useState } from 'react'; | ||
import { Locations } from "../../__generated__/graphql"; | ||
import { LinearProgress, Stack } from '@mui/material'; | ||
import { ArticleContext } from "../../contexts/article_context"; | ||
import { useOrganization, useUser } from "@clerk/clerk-react"; | ||
import { minDate, maxDate } from "../../App"; | ||
import { Article } from '../../__generated__/graphql'; | ||
import ArticleCard from "../../components/ArticleCard/ArticleCard"; | ||
import TopicCount from '../../components/TopicCount/TopicCount'; | ||
import NeighborhoodDemographicsBoard from "../../components/NeighborhoodDemoBoard/NeighborhoodDemoBoard"; | ||
import { TractContext } from "../../contexts/tract_context"; // Import TractContext | ||
import LocationInfo from '../../components/LocationsInfo/LocationsInfo'; | ||
|
||
|
||
const getMostCommonTract = (articles: Article[]) => { | ||
const tractCount: { [key: string]: number } = {}; | ||
|
||
articles.forEach(article => { | ||
article.tracts.forEach(tract => { | ||
tractCount[tract] = (tractCount[tract] || 0) + 1; | ||
}); | ||
}); | ||
|
||
return Object.keys(tractCount).reduce((a, b) => | ||
tractCount[a] > tractCount[b] ? a : b | ||
); | ||
}; | ||
|
||
|
||
const LocationPage: React.FC = () => { | ||
|
||
const { locationsData, queryLocationsData } = React.useContext(LocationContext)!; | ||
const { queryTractDataType } = React.useContext(TractContext)!; // Get query function from TractContext | ||
|
||
|
||
|
||
const [selectedLocation, setSelectedLocation] = useState<Locations | null>(null); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [locationArticles, setLocationArticles] = useState<Article[]>([]); | ||
const [mostCommonTract, setMostCommonTract] = useState<string>(""); | ||
|
||
|
||
const { articleData,articleData2, queryArticleDataType2 } = React.useContext(ArticleContext)!; | ||
|
||
const { user } = useUser(); | ||
const { organization } = useOrganization(); | ||
|
||
|
||
|
||
|
||
|
||
const navigate = useNavigate(); // Initialize useNavigate hook | ||
const location = useLocation(); // Initialize useLocation hook | ||
|
||
React.useEffect(() => { | ||
queryLocationsData(); | ||
if (organization && !articleData2) { | ||
queryArticleDataType2("ARTICLE_DATA", { | ||
dateFrom: parseInt(minDate.format("YYYYMMDD")), | ||
dateTo: parseInt(maxDate.format("YYYYMMDD")), | ||
area: "all", | ||
userId: organization.id, | ||
}); | ||
} else if (user && !articleData2) { | ||
queryArticleDataType2("ARTICLE_DATA", { | ||
dateFrom: parseInt(minDate.format("YYYYMMDD")), | ||
dateTo: parseInt(maxDate.format("YYYYMMDD")), | ||
area: "all", | ||
userId: user?.id, | ||
}); | ||
} | ||
}, []); | ||
|
||
React.useEffect(() => { | ||
const params = new URLSearchParams(location.search); | ||
const locationParam = params.get('location'); | ||
console.log("param",locationParam) | ||
if (locationParam && locationsData) { | ||
const foundLocation = locationsData?.find(loc => loc.value === locationParam); | ||
if (foundLocation) { | ||
setSelectedLocation(foundLocation); | ||
} | ||
} else if (!selectedLocation && locationsData && locationsData.length > 0) { | ||
const sortedLocations = [...locationsData].sort((a, b) => b.articles.length - a.articles.length); | ||
setSelectedLocation(sortedLocations[0]); | ||
params.set('location', sortedLocations[0].value); | ||
navigate({ search: params.toString() }); | ||
} | ||
}, [locationsData]); | ||
|
||
React.useEffect(() => { | ||
if (locationsData && selectedLocation && articleData2) { | ||
setIsLoading(false); | ||
const params = new URLSearchParams(location.search); | ||
const locationParam = params.get('location'); | ||
if (locationParam !== selectedLocation?.value) { | ||
console.log("new param set cuz of new location", selectedLocation?.value) | ||
params.set('location', selectedLocation?.value as string); | ||
navigate({ search: params.toString() }); | ||
} | ||
const newLocationArticles = articleData2.filter(article => | ||
selectedLocation.articles.includes(article.content_id) | ||
); | ||
setLocationArticles(newLocationArticles); | ||
setMostCommonTract(getMostCommonTract(newLocationArticles)); | ||
|
||
|
||
} | ||
|
||
|
||
},[selectedLocation, articleData2]); | ||
|
||
useEffect(() => { | ||
queryTractDataType("TRACT_DATA", { | ||
dateFrom: parseInt(minDate.format("YYYYMMDD")), | ||
dateTo: parseInt(maxDate.format("YYYYMMDD")), | ||
tract: mostCommonTract, | ||
}); | ||
}, [mostCommonTract]); | ||
|
||
|
||
|
||
|
||
return ( | ||
<> | ||
{isLoading ? ( | ||
<Stack | ||
sx={{ | ||
width: "100%", | ||
color: "grey.500", | ||
marginTop: "10px", | ||
}} | ||
spacing={2} | ||
> | ||
<LinearProgress color='secondary' /> | ||
</Stack> | ||
) : ( | ||
|
||
<div className="row justify-content-evenly"> | ||
<div className="col-md-5 col-sm-12"> | ||
<h1 className="titles">Locations page</h1> | ||
<LocationInfo location={selectedLocation} tract={mostCommonTract} /> | ||
</div> | ||
<div className="col-md-7 col-sm-12"> | ||
<h1 className="titles">Map</h1> | ||
<MapCard clickable={false} coordinates={selectedLocation?.coordinates as [number, number]} /> | ||
</div> | ||
|
||
{locationArticles.length > 0 && ( | ||
<div> | ||
<div className="row justify-content-evenly"> | ||
<div className="col-md-5 col-sm-12"> | ||
<h1 className="titles"> Topics Count</h1> | ||
<TopicCount articles={locationArticles} /> | ||
</div> | ||
<div className="col-md-7 col-sm-12"> | ||
<h1 className="titles">Demographics</h1> | ||
<NeighborhoodDemographicsBoard /> | ||
</div> | ||
</div> | ||
<h1 className="titles"> Articles</h1> | ||
<ArticleCard optionalArticles={locationArticles} /> | ||
</div> | ||
|
||
)} | ||
|
||
</div> | ||
|
||
)} | ||
</> | ||
|
||
); | ||
} | ||
|
||
export default LocationPage; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,7 +118,7 @@ const LocationsPage: React.FC = () => { | |
|
||
|
||
},[selectedLocation, articleData2]); | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters