Skip to content

Commit

Permalink
refactor: move uniqueCities to locations data
Browse files Browse the repository at this point in the history
  • Loading branch information
yosevu committed Dec 5, 2022
1 parent 466d3a0 commit aff7707
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
9 changes: 2 additions & 7 deletions src/components/HomeHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { Link } from 'react-router-dom';
import Subtitle from './Subtitle';
import LocationMenu from './LocationMenu';
import { Location } from '../types/sparkeats';

type HomeHeaderProps = {
locations: Location[];
};

function HomeHeader({ locations }: HomeHeaderProps) {
function HomeHeader({ uniqueCities }: { uniqueCities: string[] }) {
return (
<div className="home-header">
<Subtitle />

<LocationMenu locations={locations} />
<LocationMenu uniqueCities={uniqueCities} />

<div className="home-header__button">
<Link className="button__header-primary" to="/locations/new">
Expand Down
5 changes: 3 additions & 2 deletions src/components/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import HomeHeader from '../HomeHeader';
import { LocationCards } from './LocationCards';
import { useLocations } from '../../useLocations';

export function HomePage() {
export function HomePage({ uniqueCities }: { uniqueCities: string[] }) {

const locations = useLocations();

return (
<div className="homepage">
<HomeHeader locations={locations} />
<HomeHeader uniqueCities={uniqueCities} />
<LocationCards locations={locations} />
</div>
);
Expand Down
15 changes: 3 additions & 12 deletions src/components/LocationMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import type { Location } from '../types/sparkeats';

type LocationSelectProps = {
locations: Location[];
};

function LocationMenu({ locations }: LocationSelectProps) {
const allCities = locations.map((location) => location.city);
const uniqueCities = [...new Set(allCities)];

function LocationMenu({ uniqueCities }: { uniqueCities: string[] }) {
return (
<div className="location-select">
<h2 className="location-select__title">
<label htmlFor="location">Pick a Location</label>
<label htmlFor="location">Select a Location</label>
</h2>

<select className="location-select__menu" name="location" id="location">
<option className="location-select__option" value="all">
All Locations
All Cities
</option>
{uniqueCities.map((city: string) => (
<option key={city} value={city}>
Expand Down
7 changes: 6 additions & 1 deletion src/locations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ function transformLocations(legacyPlaces: LegacyPlace[]): Locations {
.reduce(mapLocation, {});
}

function getUniqueCities(locations: Location[]): string[] {
return Array.from(new Set(locations.map((location) => location.city)));
}

const locations = transformLocations(legacyPlaces);
const uniqueCities = getUniqueCities(locations);

export { locations };
export { locations, uniqueCities };
15 changes: 11 additions & 4 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { App } from './App';
import { HomePage } from './components/HomePage';
import { LocationPage } from './components/LocationPage';
import { locations, uniqueCities } from './locations';

window.__SPARKEATS_VERSION__ = import.meta.env['VITE_SPARKEATS_VERSION'];

Expand All @@ -13,11 +14,17 @@ ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<Routes>
<Route path="/" element={<App />}>
<Route path="/" element={<HomePage />} />
<Route
path="/"
element={
<HomePage locations={locations} uniqueCities={uniqueCities} />
}
/>
<Route path="/locations/:id" element={<LocationPage />} />
<Route path="/locations/new" element={<div>New Location Page</div>} />
<Route path="/reviews/new" element={<div>New Review Page</div>} />
</Route>
</Routes>
</BrowserRouter>
</React.StrictMode>
</Route >
</Routes >
</BrowserRouter >
</React.StrictMode >
);

0 comments on commit aff7707

Please sign in to comment.