-
Notifications
You must be signed in to change notification settings - Fork 0
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 #77 from helsingborg-stad/feature/edit-zone
Feature/edit-zone
- Loading branch information
Showing
34 changed files
with
11,512 additions
and
4,461 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ones/components/MapSnippet/MapSnippet.tsx → src/components/MapSnippet/MapSnippet.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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
2 changes: 2 additions & 0 deletions
2
src/modules/Account/CreateZones/components/CreateZonesForm/hooks/index.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,2 @@ | ||
export * from './useCreateZonesForm'; | ||
export * from '../../../../../../components/MapSnippet/useRenderMap'; |
19 changes: 19 additions & 0 deletions
19
...CreateZones/components/CreateZonesForm/hooks/useCreateZonesForm/createZones.validation.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,19 @@ | ||
import { z } from 'zod'; | ||
|
||
export const featureCollectionValidation = z.object({ | ||
type: z.literal('FeatureCollection'), | ||
features: z.array(z.object({ | ||
type: z.literal('Feature'), | ||
geometry: z.object({ | ||
type: z.literal('Polygon'), | ||
coordinates: z.array(z.array(z.array(z.number()))), | ||
}), | ||
properties: z.object({ | ||
name: z.string().min(1, { message: 'Namn är obligatoriskt' }), | ||
address: z.string().min(1, { message: 'Adress är obligatoriskt' }), | ||
area: z.string().min(1, { message: 'Område är obligatoriskt' }), | ||
type: z.enum(['distribution', 'delivery']), | ||
gln: z.string().min(1, { message: 'GLN är obligatoriskt' }), | ||
}), | ||
})), | ||
}); |
File renamed without changes.
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,3 +1 @@ | ||
export * from './CreateZonesForm'; | ||
export * from './ClickOutsideCloser'; | ||
export * from './MapSnippet'; |
This file was deleted.
Oops, something went wrong.
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,38 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { useLoadScript } from '@react-google-maps/api'; | ||
import { useAuth } from 'hooks/useAuth'; | ||
import { SideBar } from 'components'; | ||
import { EditZoneForm } from './components'; | ||
import * as Styled from './styled'; | ||
|
||
const { VITE_GOOGLE_MAPS_API_KEY } = import.meta.env; | ||
|
||
export const EditZone = () => { | ||
const { hasToken } = useAuth(); | ||
const libraries = useRef<any>(['places']); | ||
const isAuthenticated = hasToken(); | ||
const { isLoaded } = useLoadScript({ | ||
googleMapsApiKey: VITE_GOOGLE_MAPS_API_KEY, | ||
libraries: libraries.current, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!isAuthenticated) { | ||
window.location.href = '/auth/login'; | ||
} | ||
}, [isAuthenticated]); | ||
|
||
if (!isAuthenticated) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Styled.ContentContainer> | ||
<SideBar /> | ||
<Styled.FormContainer> | ||
<Styled.Header>Lägg till zoner</Styled.Header> | ||
{isLoaded && <EditZoneForm />} | ||
</Styled.FormContainer> | ||
</Styled.ContentContainer> | ||
); | ||
}; |
124 changes: 124 additions & 0 deletions
124
src/modules/Account/EditZone/components/EditZoneForm/EditZoneForm.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,124 @@ | ||
import { | ||
Input, Button, Select, ClickOutsideCloser, | ||
Loading, | ||
} from 'components'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { useEditZoneForm } from './hooks/useEditZoneForm'; | ||
import { MapSnippet } from '../../../../../components/MapSnippet'; | ||
import * as Styled from './styled'; | ||
|
||
export const EditZoneForm = () => { | ||
const navigate = useNavigate(); | ||
const { | ||
featureCollection, | ||
setFieldValue, | ||
submitForm, | ||
errors, | ||
apiErrorText, | ||
addressStatus, | ||
clearSuggestions, | ||
ready, | ||
addressData, | ||
handleSelectAddress, | ||
activeAddress, | ||
isLoadingData, | ||
} = useEditZoneForm(); | ||
|
||
const renderSuggestions = (index: number) => addressData.map((suggestion) => { | ||
const { | ||
place_id: placeId, | ||
structured_formatting: { main_text: mainText, secondary_text: secondaryText }, | ||
} = suggestion; | ||
|
||
return ( | ||
<Styled.ListItem | ||
key={placeId} | ||
onClick={handleSelectAddress(index, suggestion)} | ||
title={mainText} | ||
> | ||
<strong>{mainText}</strong> | ||
{' '} | ||
<small>{secondaryText}</small> | ||
</Styled.ListItem> | ||
); | ||
}); | ||
|
||
if (isLoadingData) { | ||
return ( | ||
<Loading /> | ||
); | ||
} | ||
|
||
return ( | ||
<Styled.ContentContainer> | ||
<form onSubmit={submitForm}> | ||
{featureCollection.features | ||
.map((zone, index) => ( | ||
<Styled.SplitContainer key={zone.properties.id}> | ||
<Styled.InputContainer> | ||
<Input | ||
label="Namn" | ||
type="text" | ||
value={zone.properties.name} | ||
onChange={setFieldValue(index, 'name')} | ||
name="name" | ||
placeholder="Namn på zon" | ||
error={errors[index]?.name} | ||
/> | ||
<Input | ||
label="GLN" | ||
type="text" | ||
value={zone.properties.gln} | ||
onChange={setFieldValue(index, 'gln')} | ||
name="gln" | ||
placeholder="GLN (Lokaliseringsnummer)" | ||
error={errors[index]?.gln} | ||
/> | ||
<ClickOutsideCloser | ||
onClick={() => { | ||
if (activeAddress === zone.properties.id) { clearSuggestions(); } | ||
}} | ||
key={zone.properties.id} | ||
> | ||
<> | ||
<Input | ||
label="Adress" | ||
type="text" | ||
value={zone.properties.address} | ||
onChange={setFieldValue(index, 'address')} | ||
placeholder="Adress till zon" | ||
name="address" | ||
error={errors[index]?.address} | ||
disabled={!ready} | ||
/> | ||
{addressStatus === 'OK' && activeAddress === zone.properties.id && <Styled.List>{renderSuggestions(index)}</Styled.List>} | ||
</> | ||
</ClickOutsideCloser> | ||
<Input | ||
label="Område" | ||
type="text" | ||
value={zone.properties.area} | ||
onChange={setFieldValue(index, 'area')} | ||
name="area" | ||
placeholder="Område" | ||
error={errors[index]?.area} | ||
/> | ||
<Select label="Typ" value={zone.properties.type} name="type" onChange={setFieldValue(index, 'type')}> | ||
<option value="delivery">Leverans zon</option> | ||
<option value="distribution">Distributions zon</option> | ||
</Select> | ||
</Styled.InputContainer> | ||
<Styled.MapContainer> | ||
<MapSnippet zone={zone} /> | ||
</Styled.MapContainer> | ||
</Styled.SplitContainer> | ||
))} | ||
{apiErrorText && <Styled.ErrorText>{apiErrorText}</Styled.ErrorText>} | ||
<Styled.ButtonContainer> | ||
<Button type="submit" onClick={() => submitForm} primary>Spara</Button> | ||
<Button type="submit" onClick={() => navigate('/account/zones')} secondary>Avbryt</Button> | ||
</Styled.ButtonContainer> | ||
</form> | ||
</Styled.ContentContainer> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
src/modules/Account/EditZone/components/EditZoneForm/hooks/index.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 @@ | ||
export * from './useEditZoneForm'; |
File renamed without changes.
1 change: 1 addition & 0 deletions
1
src/modules/Account/EditZone/components/EditZoneForm/hooks/useEditZoneForm/index.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 @@ | ||
export * from './useEditZoneForm'; |
Oops, something went wrong.