-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: complete project form migration (#2055)
* refactor: migrate BasicInfoForm * refactor: migrate LocationForm * refactor: migrate MetadataForm * fix: metadata typing issue * fix: handle review feedback
- Loading branch information
Showing
34 changed files
with
752 additions
and
473 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
web-components/src/components/inputs/new/LocationField/LocationField.styles.ts
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,11 @@ | ||
import { Theme } from '../../../../theme/muiTheme'; | ||
import { makeStyles } from 'tss-react/mui'; | ||
|
||
export const useLocationStyles = makeStyles()((theme: Theme) => ({ | ||
result: { | ||
border: `1px solid ${theme.palette.info.light}`, | ||
backgroundColor: theme.palette.primary.main, | ||
padding: theme.spacing(1.25), | ||
cursor: 'pointer', | ||
}, | ||
})); |
154 changes: 154 additions & 0 deletions
154
web-components/src/components/inputs/new/LocationField/LocationField.tsx
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,154 @@ | ||
import React, { forwardRef, useEffect, useState } from 'react'; | ||
import MapboxClient from '@mapbox/mapbox-sdk'; | ||
import mbxGeocoder, { | ||
GeocodeFeature, | ||
GeocodeQueryType, | ||
} from '@mapbox/mapbox-sdk/services/geocoding'; | ||
|
||
import FieldFormControl from '../FieldFormControl/FieldFormControl'; | ||
import Input from '../Input/Input'; | ||
import { useLocationStyles } from './LocationField.styles'; | ||
import { isGeocodingFeature } from './LocationField.types'; | ||
|
||
interface LocationFieldProps { | ||
className?: string; | ||
description?: string; | ||
label?: string; | ||
error?: boolean; | ||
helperText?: string; | ||
optional?: boolean | string; | ||
disabled?: boolean; | ||
placeholder?: string; | ||
types?: GeocodeQueryType[]; | ||
token: string; | ||
value: string | GeocodeFeature; | ||
handleChange: (value: string | GeocodeFeature) => void; | ||
onBlur: ( | ||
value: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement, Element>, | ||
) => void; | ||
} | ||
|
||
type Props = React.PropsWithChildren<LocationFieldProps>; | ||
|
||
const LocationField = forwardRef<HTMLInputElement, Props>( | ||
( | ||
{ | ||
className, | ||
label, | ||
description, | ||
optional, | ||
placeholder, | ||
disabled, | ||
// https://docs.mapbox.com/api/search/geocoding/#data-types | ||
types = [ | ||
'country', | ||
'region', | ||
'postcode', | ||
'district', | ||
'place', | ||
'locality', | ||
'neighborhood', | ||
'address', | ||
'poi', | ||
], | ||
token: accessToken, | ||
value, | ||
handleChange, | ||
onBlur, | ||
...props | ||
}, | ||
ref, | ||
) => { | ||
const baseClient = MapboxClient({ accessToken }); | ||
const geocoderService = mbxGeocoder(baseClient); | ||
const [features, setFeatures] = useState<GeocodeFeature[]>([]); | ||
const [showResults, setShowResults] = useState(true); | ||
|
||
const { classes } = useLocationStyles(); | ||
|
||
return ( | ||
<FieldFormControl | ||
className={className} | ||
label={label} | ||
description={description} | ||
disabled={disabled} | ||
optional={optional} | ||
> | ||
<> | ||
<Input | ||
{...props} | ||
placeholder={placeholder} | ||
value={isGeocodingFeature(value) ? value.place_name : value} | ||
onBlur={e => { | ||
onBlur(e); | ||
setTimeout(() => { | ||
setShowResults(false); | ||
}, 800); | ||
}} | ||
onChange={({ target: { value } }) => { | ||
handleChange(value); | ||
if (value.length >= 1) { | ||
const isCoordinates = | ||
/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/.test( | ||
value, | ||
); | ||
if (isCoordinates) { | ||
const [longitude, latitude] = value | ||
.split(',') | ||
.map(Number) as [number, number]; | ||
const coordinates: [number, number] = [longitude, latitude]; | ||
geocoderService | ||
.reverseGeocode({ | ||
mode: 'mapbox.places', | ||
query: coordinates, | ||
}) | ||
.send() | ||
.then(({ body }) => { | ||
setFeatures(body.features); | ||
setShowResults(true); | ||
}); | ||
} else { | ||
geocoderService | ||
.forwardGeocode({ | ||
types, | ||
mode: 'mapbox.places', | ||
query: value, | ||
}) | ||
.send() | ||
.then(res => { | ||
setFeatures(res.body.features); | ||
setShowResults(true); | ||
}); | ||
} | ||
} | ||
}} | ||
ref={ref} | ||
/> | ||
{showResults && | ||
features.map((item, index) => ( | ||
<div | ||
key={index} | ||
className={classes.result} | ||
onClick={() => { | ||
handleChange({ | ||
// @ts-ignore | ||
'@context': { | ||
'@vocab': 'https://purl.org/geojson/vocab#', | ||
type: '@type', | ||
coordinates: { '@container': '@list' }, | ||
}, | ||
...item, | ||
}); | ||
setShowResults(false); | ||
}} | ||
> | ||
{item.place_name} | ||
</div> | ||
))} | ||
</> | ||
</FieldFormControl> | ||
); | ||
}, | ||
); | ||
|
||
export default LocationField; |
7 changes: 7 additions & 0 deletions
7
web-components/src/components/inputs/new/LocationField/LocationField.types.ts
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,7 @@ | ||
import { GeocodeFeature } from '@mapbox/mapbox-sdk/services/geocoding'; | ||
|
||
export function isGeocodingFeature( | ||
input: string | GeocodeFeature, | ||
): input is GeocodeFeature { | ||
return typeof input !== 'string' && 'place_name' in input; | ||
} |
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
156 changes: 0 additions & 156 deletions
156
web-marketplace/src/components/organisms/BasicInfoForm.tsx
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
web-marketplace/src/components/organisms/BasicInfoForm/BasicInfoForm.constants.ts
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,5 @@ | ||
export const BASIC_INFO_NAME_LABEL = 'Project name'; | ||
export const BASIC_INFO_NAME_DESCRIPTION = | ||
'This is the name of the farm, ranch, property, or conservation project.'; | ||
export const BASIC_INFO_NAME_PLACEHOLDER = 'i.e. Sunnybrook Farms'; | ||
export const BASIC_INFO_SIZE_LABEL = 'Size in hectares or acres'; |
Oops, something went wrong.