Skip to content

Commit

Permalink
use an address formatting library
Browse files Browse the repository at this point in the history
  • Loading branch information
lyoshenka committed Mar 19, 2024
1 parent 4f2e162 commit fa83d73
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 12 deletions.
39 changes: 28 additions & 11 deletions lib/address.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
import addressFormatter from '@fragaria/address-formatter'
import { EMPTY } from '@/utils/display-utils'

export interface FormatShortAddressParams {
locality?: string | null | undefined
admininstrativeAreaLevel1Short?: string | null | undefined
country?: string | null | undefined
countryShort?: string | null | undefined
}

export const formatShortAddress = (
address: FormatShortAddressParams | null | undefined
): string => {
if (!address) return EMPTY

if (!address.locality && !address.admininstrativeAreaLevel1Short) {
return EMPTY
} else if (!address.locality) {
return `${address.admininstrativeAreaLevel1Short ?? EMPTY} | ${
address.country ?? EMPTY
}`
} else {
return `${address.locality ?? EMPTY}, ${
address.admininstrativeAreaLevel1Short ?? EMPTY
} | ${address.country ?? EMPTY}`
}
const formatted = addressFormatter.format(
{
city: address.locality || undefined,
state: address.admininstrativeAreaLevel1Short || undefined,
},
{
abbreviate: true,
appendCountry: false,
countryCode: address.countryShort || undefined,
fallbackCountryCode: 'US',
}
)

return formatted + (address.country ? ` | ${address.country}` : '')

// if (!address.locality && !address.admininstrativeAreaLevel1Short) {
// return EMPTY
// } else if (!address.locality) {
// return `${address.admininstrativeAreaLevel1Short ?? EMPTY} | ${
// address.country ?? EMPTY
// }`
// } else {
// return `${address.locality ?? EMPTY}, ${
// address.admininstrativeAreaLevel1Short ?? EMPTY
// } | ${address.country ?? EMPTY}`
// }
}
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@apollo/client": "^3.7.5",
"@fragaria/address-formatter": "^5.3.0",
"@next/font": "^14.1.3",
"@pinata/sdk": "^2.1.0",
"@prisma/client": "^5.7.0",
Expand Down
3 changes: 3 additions & 0 deletions pages/api/v2/activity/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const toFragments = (
activity.location.address?.admininstrativeAreaLevel1Short ||
'',
country: activity.location.address?.country || '',
countryShort: activity.location.address?.countryShort || '',
},
voteCount: 0, // TODO: implement
offerCount: 0, // TODO: implement
Expand Down Expand Up @@ -160,6 +161,8 @@ const toFragments = (
activity.offer.location.address
.admininstrativeAreaLevel1Short || '',
country: activity.offer.location.address.country || '',
countryShort:
activity.offer.location.address.countryShort || '',
}
: null,
caretaker: {
Expand Down
1 change: 1 addition & 0 deletions pages/api/v2/offer/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export const offerToFragment = (offer: OfferWithRelations): OfferFragment => {
admininstrativeAreaLevel1Short:
offer.location.address.admininstrativeAreaLevel1Short,
country: offer.location.address.country,
countryShort: offer.location.address.countryShort,
}
: null,
caretaker: {
Expand Down
1 change: 1 addition & 0 deletions pages/api/v2/profile/[externId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ const profileToFragment = (profile: ProfileWithRelations): ProfileFragment => {
admininstrativeAreaLevel1Short:
profile.address.admininstrativeAreaLevel1Short,
country: profile.address.country,
countryShort: profile.address.countryShort,
}
: undefined,
citizenshipStatus: profile.citizenshipStatus as CitizenshipStatus,
Expand Down
1 change: 1 addition & 0 deletions pages/api/v2/profile/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ const profilesToFragments = (
admininstrativeAreaLevel1Short:
profile.address.admininstrativeAreaLevel1Short,
country: profile.address.country,
countryShort: profile.address.countryShort,
}
: null,
avatar: profile.avatar
Expand Down
4 changes: 4 additions & 0 deletions utils/types/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export type ActivityWithRelations = Prisma.ActivityGetPayload<{
locality: true
admininstrativeAreaLevel1Short: true
country: true
countryShort: true
}
}
}
Expand Down Expand Up @@ -198,6 +199,7 @@ export type ActivityWithRelations = Prisma.ActivityGetPayload<{
locality: true
admininstrativeAreaLevel1Short: true
country: true
countryShort: true
}
}
caretaker: {
Expand Down Expand Up @@ -270,6 +272,7 @@ export const ActivityQueryInclude = {
locality: true,
admininstrativeAreaLevel1Short: true,
country: true,
countryShort: true,
},
},
},
Expand Down Expand Up @@ -297,6 +300,7 @@ export const ActivityQueryInclude = {
locality: true,
admininstrativeAreaLevel1Short: true,
country: true,
countryShort: true,
},
},
caretaker: {
Expand Down
2 changes: 1 addition & 1 deletion utils/types/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type AddressFragment = {

export type ShortAddressFragment = Pick<
AddressFragment,
'locality' | 'admininstrativeAreaLevel1Short' | 'country'
'locality' | 'admininstrativeAreaLevel1Short' | 'country' | 'countryShort'
>

export type RecentVoterFragment = {
Expand Down

0 comments on commit fa83d73

Please sign in to comment.