-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: geocoding on address visiblity change (#1316)
<!--- Please provide a general summary of your changes in the title above --> # Pull Request type <!-- Please try to limit your pull request to one type; submit multiple pull requests if needed. --> Please check the type of change your PR introduces: - [x] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no API changes) - [ ] Build-related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Issue Number: N/A ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - - - ## Does this introduce a breaking change? - [ ] Yes - [ ] No <!-- If this does introduce a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR, such as screenshots of how the component looks before and after the change. --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added support for city coordinate queries via a new API endpoint. - Introduced React components `AutoCompleteItem` and `CountryItem` for improved UI interaction. - Added a `FormContext` for better form state management in the UI. - **Enhancements** - Improved address visibility handling and formatting across multiple components. - Refined Google geocoding and autocomplete functionality. - **Bug Fixes** - Corrected naming in API handlers to improve clarity and maintain consistency. - **Styling** - Added new styling rules for elements in the `AddressDrawer` component for a more polished UI. - **Refactor** - Consolidated schema definitions for Google API responses to reduce redundancy. - Updated import paths for better module organization and maintainability. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Joe Karow <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
89d952e
commit cb86839
Showing
18 changed files
with
608 additions
and
341 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
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,58 @@ | ||
import { PlaceAutocompleteType } from '@googlemaps/google-maps-services-js' | ||
import { TRPCError } from '@trpc/server' | ||
|
||
import { prisma } from '@weareinreach/db' | ||
import { googleMapsApi } from '~api/google' | ||
import { handleError } from '~api/lib/errorHandler' | ||
import { googleAPIResponseHandler } from '~api/lib/googleHandler' | ||
import { geocodeByStringResponse } from '~api/schemas/thirdParty/googleGeo' | ||
import { type TRPCHandlerParams } from '~api/types/handler' | ||
|
||
import { type TCityCoordsSchema } from './query.cityCoords.schema' | ||
|
||
const countryMap = new Map<string, string>() | ||
const govDistMap = new Map<string, string>() | ||
|
||
const cityCoords = async ({ input }: TRPCHandlerParams<TCityCoordsSchema>) => { | ||
try { | ||
const { city, govDist, country } = input | ||
if (countryMap.size === 0) { | ||
const countryData = await prisma.country.findMany({ | ||
where: { activeForOrgs: true }, | ||
select: { id: true, cca2: true }, | ||
}) | ||
countryData.forEach((x) => countryMap.set(x.id, x.cca2)) | ||
} | ||
if (govDist) { | ||
const govDistData = await prisma.govDist.findMany({ | ||
where: { countryId: country, active: true }, | ||
select: { id: true, name: true }, | ||
}) | ||
govDistData.forEach((x) => govDistMap.set(x.id, x.name)) | ||
} | ||
const searchCountry = country ? countryMap.get(country) : undefined | ||
if (!searchCountry) { | ||
throw new TRPCError({ | ||
code: 'NOT_FOUND', | ||
message: 'Country not found', | ||
}) | ||
} | ||
const searchGovDist = govDist ? govDistMap.get(govDist) : undefined | ||
const searchString = [city, searchGovDist, searchCountry].filter(Boolean).join(', ') | ||
const { data } = await googleMapsApi.geocode({ | ||
params: { | ||
// eslint-disable-next-line node/no-process-env | ||
key: process.env.GOOGLE_PLACES_API_KEY as string, | ||
address: searchString, | ||
components: PlaceAutocompleteType.cities, | ||
}, | ||
}) | ||
|
||
const parsedData = geocodeByStringResponse.parse(data) | ||
|
||
return googleAPIResponseHandler(parsedData, data) | ||
} catch (error) { | ||
return handleError(error) | ||
} | ||
} | ||
export default cityCoords |
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,10 @@ | ||
import { z } from 'zod' | ||
|
||
import { prefixedId } from '~api/schemas/idPrefix' | ||
|
||
export const ZCityCoordsSchema = z.object({ | ||
city: z.string(), | ||
govDist: prefixedId('govDist').nullish(), | ||
country: prefixedId('country'), | ||
}) | ||
export type TCityCoordsSchema = z.infer<typeof ZCityCoordsSchema> |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// codegen:start {preset: barrel, include: ./*.schema.ts} | ||
export * from './query.autocomplete.schema' | ||
export * from './query.cityCoords.schema' | ||
export * from './query.geoByPlaceId.schema' | ||
// codegen:end |
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
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
Oops, something went wrong.