leaflet#LeafletMouseEvent TypeScript Examples
The following examples show how to use
leaflet#LeafletMouseEvent.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CoordinatesControl.ts From LiveAtlas with Apache License 2.0 | 6 votes |
_onMouseMove(event: LeafletMouseEvent) {
if (!this._map || !store.state.currentMap) {
return;
}
this._location = store.state.currentMap.latLngToLocation(event.latlng, store.state.currentWorld!.seaLevel + 1);
if(!this._locationChanged) {
this._locationChanged = true;
requestAnimationFrame(() => this._update());
}
}
Example #2
Source File: points.ts From LiveAtlas with Apache License 2.0 | 6 votes |
createPointLayer = (options: LiveAtlasPointMarker, converter: Function): GenericMarker => {
const marker = new GenericMarker(converter(options.location), options);
marker.on('click', (e: LeafletMouseEvent) => {
if(!e.target.getPopup() || e.target.isPopupOpen()) {
e.target._map.panTo(e.target.getLatLng());
}
});
if(options.popup) {
marker.bindPopup(() => createPopup(options));
}
return marker;
}
Example #3
Source File: map.component.ts From dayz-server-manager with MIT License | 5 votes |
public onMapReady(map: LeafletMap): void {
console.log('Map Ready');
this.map = map;
this.map.on('mousemove', (event: LeafletMouseEvent) => {
this.curCoordinates = this.project(event.latlng);
});
this.map.on('zoomend', () => this.zoomChange());
this.createBaseLayers();
Object.keys(this.baseLayers!).forEach((x) => {
this.map!.addLayer(this.baseLayers![x]);
});
this.map.setView(this.unproject(this.info!.center ?? [0, 0]));
for (const x of this.layers) {
this.map.addLayer(x[1].layer);
}
this.createLocations();
this.updateLayersControl();
this.zoomChange();
this.appCommon.getApiFetcher('INGAME_PLAYERS').latestData
.pipe(
takeUntil(this.onDestroy),
)
.subscribe(
(data) => {
if (data) {
const players = (data as any).value;
this.updatePlayers(players);
}
},
);
this.appCommon.getApiFetcher('INGAME_VEHICLES').latestData
.pipe(
takeUntil(this.onDestroy),
)
.subscribe(
(data) => {
if (data) {
const vehicles = (data as any).value;
this.updateVehicles(vehicles);
}
},
);
}
Example #4
Source File: index.tsx From NLW-1.0 with MIT License | 4 votes |
CreatePoint: React.FC = () => {
const [items, setItems] = useState<Item[]>([]);
const [ufs, setUfs] = useState<IBGEUFResponse[]>([]);
const [cities, setCities] = useState<IBGECityResponse[]>([]);
const [formData, setFormData] = useState({
name: '',
email: '',
whatsapp: '',
});
const [selectedFile, setSelectedFile] = useState<File>();
const [selectedItems, setSelectedItems] = useState<number[]>([]);
const [selectedUf, setSelectedUf] = useState<string>('0');
const [selectedCity, setSelectedCity] = useState<string>('0');
const [selectedPosition, setSelectedPosition] = useState<[number, number]>([
0,
0,
]);
const [initialPosition, setInitialPosition] = useState<[number, number]>([
0,
0,
]);
const history = useHistory();
// Get Current Position
useEffect(() => {
navigator.geolocation.getCurrentPosition(
position => {
const { latitude, longitude } = position.coords;
setInitialPosition([latitude, longitude]);
},
() => {
toast.error('❌ Oops! Algo deu errado =/', toastOptions);
},
{
timeout: 30000,
enableHighAccuracy: true,
},
);
}, []);
// Load items
useEffect(() => {
async function loadItems() {
const response = await api.get('/items');
setItems(response.data);
}
loadItems();
}, []);
// Load UFs
useEffect(() => {
async function loadUfs() {
const response = await ibge.get<IBGEUFResponse[]>(
'localidades/estados?orderBy=nome',
);
const ufInitials = response.data.map(uf => {
return {
sigla: uf.sigla,
nome: uf.nome,
};
});
setUfs(ufInitials);
}
loadUfs();
}, []);
// Load Cities
useEffect(() => {
async function loadCities() {
if (selectedUf === '0') return;
const response = await ibge.get<IBGECityResponse[]>(
`localidades/estados/${selectedUf}/municipios`,
);
const cityNames = response.data.map(city => {
return { nome: city.nome };
});
setCities(cityNames);
}
loadCities();
}, [selectedUf]);
function handleSelectUf(event: ChangeEvent<HTMLSelectElement>) {
setSelectedUf(event.target.value);
}
function handleSelectCity(event: ChangeEvent<HTMLSelectElement>) {
setSelectedCity(event.target.value);
}
function handleMapClick(event: LeafletMouseEvent) {
setSelectedPosition([event.latlng.lat, event.latlng.lng]);
}
function handleInputChange(event: ChangeEvent<HTMLInputElement>) {
const { name, value } = event.target;
setFormData({ ...formData, [name]: value });
}
function handleSelectItem(id: number) {
const alreadySelected = selectedItems.findIndex(item => item === id);
if (alreadySelected >= 0) {
const filteredItems = selectedItems.filter(item => item !== id);
setSelectedItems(filteredItems);
} else {
setSelectedItems([...selectedItems, id]);
}
}
// Toastify configurations
const toastOptions = {
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
};
const handleSubmit = useCallback(
async (event: FormEvent) => {
event.preventDefault();
try {
const { name, email, whatsapp } = formData;
const [latitude, longitude] = selectedPosition;
const data = new FormData();
data.append('name', name);
data.append('email', email);
data.append('whatsapp', whatsapp);
data.append('latitude', String(latitude));
data.append('longitude', String(longitude));
data.append('uf', selectedUf);
data.append('city', selectedCity);
data.append('items', selectedItems.join(','));
if (selectedFile) {
data.append('image', selectedFile);
}
await api.post('points', data);
toast('✅ Criado com sucesso!', toastOptions);
history.push('/');
} catch (err) {
toast.error('❌ Erro!', toastOptions);
}
},
[
formData,
selectedCity,
selectedItems,
selectedPosition,
selectedUf,
history,
selectedFile,
],
);
return (
<div id="page-create-point">
<header>
<img src={logo} alt="Ecoleta" />
<Link to="/">
<FiArrowLeft />
Voltar para home
</Link>
</header>
<form onSubmit={handleSubmit}>
<h1>
Cadastro do
<br /> ponto de coleta
</h1>
<Dropzone onFileUploaded={setSelectedFile} />
<fieldset>
<legend>
<h2>Dados</h2>
</legend>
<div className="field">
<label htmlFor="name">Nome da entidade</label>
<input
onChange={handleInputChange}
type="text"
name="name"
id="name"
/>
</div>
<div className="field-group">
<div className="field">
<label htmlFor="email">E-mail</label>
<input
onChange={handleInputChange}
type="text"
name="email"
id="email"
/>
</div>
<div className="field">
<label htmlFor="whatsapp">Whatsapp</label>
<input
onChange={handleInputChange}
type="text"
name="whatsapp"
id="whatsapp"
/>
</div>
</div>
</fieldset>
<fieldset>
<legend>
<h2>Endereço</h2>
<span>Selecione o endereço no mapa</span>
</legend>
<Map center={initialPosition} zoom={15} onClick={handleMapClick}>
<TileLayer
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={selectedPosition} />
</Map>
<div className="field-group">
<div className="field">
<label htmlFor="uf">Estado (UF)</label>
<select onChange={handleSelectUf} name="uf" id="uf">
<option value="0">Selecione uma UF</option>
{ufs?.map(uf => (
<option key={uf.nome} value={uf.sigla}>
{uf.sigla}
</option>
))}
</select>
</div>
<div className="field">
<label htmlFor="city">Cidade</label>
<select onChange={handleSelectCity} name="city" id="city">
<option value="0">Selecione uma cidade</option>
{cities.map(city => (
<option key={city.nome} value={city.nome}>
{city.nome}
</option>
))}
</select>
</div>
</div>
</fieldset>
<fieldset>
<legend>
<h2>Itens de coleta</h2>
<span>Selecione um ou mais itens abaixo</span>
</legend>
<ul className="items-grid">
{items.map(item => (
<li
key={item.id}
onClick={() => handleSelectItem(item.id)}
className={selectedItems.includes(item.id) ? 'selected' : ''}
>
<img src={item.image_url} alt={item.title} />
<span>{item.title}</span>
</li>
))}
</ul>
</fieldset>
<button type="submit">Cadastrar novo ponto de coleta</button>
</form>
</div>
);
}
Example #5
Source File: CreateOrphanage.tsx From happy with MIT License | 4 votes |
export default function OrphanagesMap() {
const history = useHistory();
const [position, setPosition] = useState({ latitude: 0, longitude: 0})
const [name, setName] = useState('');
const [about, setAbout] = useState('');
const [instructions, setInstructions] = useState('');
const [opening_hours, setOpeningHours] = useState('');
const [open_on_weekends, setOpenOnWeekends] = useState(true);
const [images, setImages] = useState<File[]>([]);
const [previewImages, setPreviewImages] = useState<string[]>([]);
function handleMapClick(event: LeafletMouseEvent) {
const { lat, lng } = event.latlng;
setPosition({
latitude: lat,
longitude: lng,
});
}
function handleSelectImages(event: ChangeEvent<HTMLInputElement>) {
if (!event.target.files) {
return;
}
const selectedImages = Array.from(event.target.files)
setImages(selectedImages);
const selectedImagesPreview = selectedImages.map(image => {
return URL.createObjectURL(image);
});
setPreviewImages(selectedImagesPreview);
}
async function handleSubmit(event: FormEvent) {
event.preventDefault();
const { latitude, longitude } = position;
const data = new FormData();
data.append('name', name);
data.append('about', about);
data.append('latitude', String(latitude));
data.append('longitude', String(longitude));
data.append('instructions', instructions);
data.append('opening_hours', opening_hours);
data.append('open_on_weekends', String(open_on_weekends));
images.forEach(image => {
data.append('images', image);
});
await api.post('orphanages', data);
alert('Cadastro realizado com sucesso!');
history.push('/app');
}
return (
<div id="page-create-orphanage">
<Sidebar />
<main>
<form onSubmit={handleSubmit} className="create-orphanage-form">
<fieldset>
<legend>Dados</legend>
<Map
center={[-27.2092052,-49.6401092]}
style={{ width: '100%', height: 280 }}
zoom={15}
onClick={handleMapClick}
>
<TileLayer
url={`https://api.mapbox.com/styles/v1/mapbox/light-v10/tiles/256/{z}/{x}/{y}@2x?access_token=${process.env.REACT_APP_MAPBOX_TOKEN}`}
/>
{ position.latitude !== 0 && (
<Marker
interactive={false}
icon={mapIcon}
position={[position.latitude, position.longitude]}
/>
)}
</Map>
<div className="input-block">
<label htmlFor="name">Nome</label>
<input
id="name"
value={name}
onChange={event => setName(event.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="about">Sobre <span>Máximo de 300 caracteres</span></label>
<textarea
id="name"
maxLength={300}
value={about}
onChange={event => setAbout(event.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="images">Fotos</label>
<div className="images-container">
{previewImages.map(image => (
<img key={image} src={image} alt={name} />
))}
<label htmlFor="image[]" className="new-image">
<FiPlus size={24} color="#15b6d6" />
</label>
</div>
<input multiple onChange={handleSelectImages} type="file" id="image[]"/>
</div>
</fieldset>
<fieldset>
<legend>Visitação</legend>
<div className="input-block">
<label htmlFor="instructions">Instruções</label>
<textarea
id="instructions"
value={instructions}
onChange={event => setInstructions(event.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="opening_hours">Horário de funcinamento</label>
<input
id="opening_hours"
value={opening_hours}
onChange={event => setOpeningHours(event.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="open_on_weekends">Atende fim de semana</label>
<div className="button-select">
<button
type="button"
className={open_on_weekends ? 'active' : ''}
onClick={() => setOpenOnWeekends(true)}
>
Sim
</button>
<button
type="button"
className={!open_on_weekends ? 'active' : ''}
onClick={() => setOpenOnWeekends(false)}
>
Não
</button>
</div>
</div>
</fieldset>
<button className="confirm-button" type="submit">
Confirmar
</button>
</form>
</main>
</div>
);
}
Example #6
Source File: index.tsx From ecoleta with MIT License | 4 votes |
CreatePoint: React.FC = () => {
const history = useHistory();
const { id } = useParams();
const { addToast } = useToast();
const { title } = useContext(ThemeContext);
const { toggleTheme } = useTheme();
const [items, setItems] = useState<IItem[]>([]);
const [ufs, setUfs] = useState<string[]>([]);
const [cities, setCities] = useState<string[]>([]);
const [selectedPosition, setSelectedPosition] = useState<[number, number]>([
0,
0,
]);
const [initialPosition, setInitialPosition] = useState<[number, number]>([
0,
0,
]);
const [formData, setFormData] = useState({
name: '',
email: '',
whatsapp: '',
});
const [selectedUf, setSelectedUf] = useState('0');
const [selectedCity, setSelectedCity] = useState('0');
const [selectedItems, setSelectedItems] = useState<string[]>([]);
const [selectedFile, setSelectedFile] = useState<File>();
const [preview, setPreview] = useState('');
const loadPoint = useCallback(async () => {
try {
const { data } = await api.get(`points/${id}`);
setSelectedCity(data.point.city);
setSelectedUf(data.point.uf);
setSelectedPosition([data.point.latitude, data.point.longitude]);
setFormData({
name: data.point.name,
email: data.point.email,
whatsapp: data.point.whatsapp,
});
setSelectedItems(
data.point.point_items.map(
(point_item: IPointItem) => point_item.item.id,
),
);
setPreview(data.point.image_url);
} catch (err) {
console.log(err);
}
}, [id]);
useEffect(() => {
if (id) {
loadPoint();
}
}, [id, loadPoint]);
useEffect(() => {
navigator.geolocation.getCurrentPosition(position => {
const { latitude, longitude } = position.coords;
setInitialPosition([-21.6235108, -55.1598438]);
});
}, []);
useEffect(() => {
api.get('items').then(response => setItems(response.data));
}, []);
useEffect(() => {
if (selectedUf === '0') return;
axios
.get<IIBGECityResponse[]>(
`https://servicodados.ibge.gov.br/api/v1/localidades/estados/${selectedUf}/municipios`,
)
.then(response => {
const cityNames = response.data.map(city => city.nome);
setCities(cityNames);
});
}, [selectedUf]);
useEffect(() => {
axios
.get<IIBGEResponse[]>(
'https://servicodados.ibge.gov.br/api/v1/localidades/estados',
)
.then(response => {
const ufInitials = response.data.map(uf => uf.sigla);
setUfs(ufInitials);
});
}, []);
const handleSelectUf = useCallback(
(event: ChangeEvent<HTMLSelectElement>) => {
setSelectedUf(event.target.value);
},
[],
);
const handleSelectCity = useCallback(
(event: ChangeEvent<HTMLSelectElement>) => {
setSelectedCity(event.target.value);
},
[],
);
const handleMapClick = useCallback((event: LeafletMouseEvent) => {
setSelectedPosition([event.latlng.lat, event.latlng.lng]);
}, []);
const handleInputChange = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
const { name, value } = event.target;
setFormData(oldData => ({
...oldData,
[name]: value,
}));
},
[],
);
const handleSelectItem = useCallback(
(itemId: string) => {
const alreadySelected = selectedItems.findIndex(item => item === itemId);
alreadySelected >= 0
? setSelectedItems(oldItems => oldItems.filter(item => item !== itemId))
: setSelectedItems(oldItems => [...oldItems, itemId]);
},
[selectedItems],
);
const handleSubmit = useCallback(
async (event: FormEvent) => {
event.preventDefault();
try {
const { name, email, whatsapp } = formData;
const [latitude, longitude] = selectedPosition;
const data = new FormData();
data.append('name', name);
data.append('email', email);
data.append('whatsapp', whatsapp);
data.append('latitude', String(latitude));
data.append('longitude', String(longitude));
data.append('uf', selectedUf);
data.append('city', selectedCity);
data.append('items', selectedItems.join(','));
if (selectedFile) {
data.append('image', selectedFile);
}
if (id) {
await api.put(`points/${id}`, data);
addToast({
type: 'success',
title: 'Atualizado',
description: 'Ponto de coleta atualizado com sucesso.',
});
} else {
await api.post('points', data);
addToast({
type: 'success',
title: 'Cadastro realizado',
description: 'Ponto de coleta cadastrado com sucesso.',
});
}
history.push('/list-points');
} catch (err) {
addToast({
type: 'error',
title: 'Erro',
description:
',Ocorreu um erro de comunicação com o servidor, tente novamente.',
});
}
},
[
formData,
selectedCity,
selectedItems,
selectedPosition,
selectedUf,
history,
id,
selectedFile,
addToast,
],
);
return (
<Container>
<header>
{title === 'light' ? (
<img src={logo} alt="Ecoleta" />
) : (
<img src={logoDark} alt="Ecoleta" />
)}
<Toggle
checked={title === 'dark'}
onChange={toggleTheme}
className="toggle"
icons={{
checked: <FaMoon color="yellow" size={12} />,
unchecked: <FaSun color="yellow" size={12} />,
}}
/>
<Link to="/list-points">
<FiArrowLeft />
Voltar para Dashboard
</Link>
</header>
<Form onSubmit={handleSubmit}>
<h1>Cadastro do ponto de coleta</h1>
<Dropzone preview={preview} onFileUploaded={setSelectedFile} />
<fieldset>
<legend>
<h2>Dados</h2>
</legend>
<div className="field">
<label htmlFor="name">Nome da entidade</label>
<input
value={formData.name}
type="text"
name="name"
id="name"
onChange={handleInputChange}
/>
</div>
<FieldGroup>
<div className="field">
<label htmlFor="email">E-mail</label>
<input
value={formData.email}
type="email"
name="email"
id="email"
onChange={handleInputChange}
/>
</div>
</FieldGroup>
<FieldGroup>
<div className="field">
<label htmlFor="whatsapp">Whatsapp</label>
<input
value={formData.whatsapp}
type="text"
name="whatsapp"
id="whatsapp"
onChange={handleInputChange}
/>
</div>
</FieldGroup>
</fieldset>
<fieldset>
<legend>
<h2>Endereço</h2>
<span>Selecione o endereço no mapa</span>
</legend>
<Map center={initialPosition} zoom={15} onClick={handleMapClick}>
<TileLayer
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={selectedPosition} />
</Map>
<FieldGroup>
<div className="field">
<label htmlFor="uf">Estado (UF)</label>
<select
value={selectedUf}
name="uf"
id="uf"
onChange={handleSelectUf}
>
<option value="0">Selecione um UF</option>
{ufs?.map(uf => (
<option key={uf} value={uf}>
{uf}
</option>
))}
</select>
</div>
<div className="field">
<label htmlFor="city">Cidade</label>
<select
value={selectedCity}
name="city"
id="city"
onChange={handleSelectCity}
>
<option value="0">Selecione um cidade</option>
{cities?.map(city => (
<option key={city} value={city}>
{city}
</option>
))}
</select>
</div>
</FieldGroup>
</fieldset>
<fieldset>
<legend>
<h2>Ítens de coleta</h2>
<span>Selecione um ou mais ítens abaixo</span>
</legend>
<ItemsList>
{items?.map((item: IItem) => (
<li key={item.id}>
<button
className={selectedItems.includes(item.id) ? 'selected' : ''}
type="button"
onClick={() => handleSelectItem(item.id)}
>
<img src={item.image_url} alt={item.title} />
<span>{item.title}</span>
</button>
</li>
))}
</ItemsList>
</fieldset>
<button type="submit">Cadastrar ponto de coleta</button>
</Form>
</Container>
);
}
Example #7
Source File: index.tsx From NextLevelWeek with MIT License | 4 votes |
CreatePoint = () => {
const [items, setItems] = useState<Item[]>([]);
const [ufs, setUfs] = useState<string[]>([]);
const [cities, setCities] = useState<string[]>([]);
const [selectedItems, setSelectedItems] = useState<number[]>([]);
const [initialPosition, setInitialPosition] = useState<[number, number]>([
0,
0,
]);
const [selectedPosition, setSelectedPosition] = useState<[number, number]>([
0,
0,
]);
const [formData, setFormData] = useState({
name: "",
email: "",
whatsapp: "",
});
const [selectedUf, setCity] = useState("0");
const [selectedCity, setSelectedCity] = useState("0");
const [selectedFile, setSelectFile] = useState<File>();
// Permite navegar de um componente para outro sem ter um botão.
const history = useHistory();
/**
* UseEffect para pegar a localização atual do usuário
*/
useEffect(() => {
navigator.geolocation.getCurrentPosition((position) => {
// console.log(position);
const { latitude, longitude } = position.coords;
setInitialPosition([latitude, longitude]);
});
}, []);
/**
* Evitar que toda vez que o componente mude, chame a API local.
*/
useEffect(() => {
api.get("/items").then((res) => {
// console.log(res);
setItems(res.data);
});
}, []);
/**
* Chamanda da API do IBGE para UFs.
*/
useEffect(() => {
axios
.get<IBGEUF[]>(
"https://servicodados.ibge.gov.br/api/v1/localidades/estados?orderBy=nome"
)
.then((res) => {
// console.log(res);
const ufInitials = res.data.map((uf) => uf.sigla);
// console.log(ufInitials);
setUfs(ufInitials);
});
}, []);
/**
* Chamada da API do IBGE por municípios.
*/
useEffect(() => {
// Carregar as cidades sempre que alterar as UF.
// console.log("Funfou", selectedUf);
if (selectedUf === "0") {
return;
}
axios
.get<IBGECity[]>(
`https://servicodados.ibge.gov.br/api/v1/localidades/estados/${selectedUf}/municipios`
)
.then((res) => {
// console.log(res);
const cityName = res.data.map((city) => city.nome);
setCities(cityName);
});
}, [selectedUf]);
/**
* Toda vez que o usuário altera a UF chama esta função.
*/
function handleSelectUf(e: ChangeEvent<HTMLSelectElement>) {
// console.log(e.target.value);
const uf = e.target.value;
setCity(uf);
}
/**
* Armazenando a cidade.
*/
function handleSelectCity(e: ChangeEvent<HTMLSelectElement>) {
const city = e.target.value;
// console.log(city);
setSelectedCity(city);
}
/**
* Função para colocar o pin no mapa.
*/
function handleMapClick(e: LeafletMouseEvent) {
// console.log(e.latlng);
setSelectedPosition([e.latlng.lat, e.latlng.lng]);
}
/**
* Função para armazenar os inputs.
*/
function handleInputChange(e: ChangeEvent<HTMLInputElement>) {
// console.log(e.target.name, e.target.value);
const { name, value } = e.target;
setFormData({
...formData,
[name]: value,
});
}
/**
* Função para armazenar os itens selecionados.
*/
function handleSelectItem(id: number) {
// console.log("Foi", id);
const alreadySelected = selectedItems.findIndex((item) => item === id);
if (alreadySelected >= 0) {
const filteredItems = selectedItems.filter((item) => item !== id);
setSelectedItems(filteredItems);
} else {
setSelectedItems([...selectedItems, id]);
}
}
/**
* Função do submit.
*/
async function handleSubmit(e: FormEvent) {
// console.log("E aí ?");
// Evitando que a tela seja recarregada no submit.
e.preventDefault();
// console.log(selectedFile);
const { name, email, whatsapp } = formData;
const [latitude, longitude] = selectedPosition;
const uf = selectedUf;
const city = selectedCity;
const items = selectedItems;
const data = new FormData();
data.append("name", name);
data.append("email", email);
data.append("whatsapp", whatsapp);
data.append("latitude", String(latitude));
data.append("longitude", String(longitude));
data.append("uf", uf);
data.append("city", city);
data.append("items", items.join(","));
if (selectedFile) {
data.append("image", selectedFile);
}
/* const data = {
name,
email,
whatsapp,
latitude,
longitude,
uf,
city,
items,
}; */
// console.log(data);
await api.post("/points", data);
alert("Ponto de coleta criado!");
history.push("/");
}
return (
<div id="page-create-point">
<header>
<img src={logo} alt="Ecoleta" />
<Link to="/">
<FiArrowLeft />
Voltar para home
</Link>
</header>
<form onSubmit={handleSubmit}>
<h1>
Cadastro do <br /> ponto de coleta
</h1>
<Dropzone onFileUploaded={setSelectFile} />
<fieldset>
<legend>
<h2>Dados</h2>
</legend>
<div className="field">
<label htmlFor="name">Nome da entidade</label>
<input
type="text"
name="name"
id="name"
onChange={handleInputChange}
/>
</div>
<div className="field-group">
<div className="field">
<label htmlFor="email">E-mail</label>
<input
type="text"
name="email"
id="email"
onChange={handleInputChange}
/>
</div>
<div className="field">
<label htmlFor="whatsapp">WhatsApp</label>
<input
type="text"
name="whatsapp"
id="whatsapp"
onChange={handleInputChange}
/>
</div>
</div>
</fieldset>
<fieldset>
<legend>
<h2>Endereço</h2>
<span>Selecione o endereço no mapa.</span>
</legend>
<Map
center={initialPosition}
zoom={15}
onClick={handleMapClick}
>
<TileLayer
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={selectedPosition} />
</Map>
<div className="field-group">
<div className="field">
<label htmlFor="uf">Estado</label>
<select
name="uf"
id="uf"
value={selectedUf}
onChange={handleSelectUf}
>
<option value="0">Selecione um estado</option>
{ufs.map((uf) => (
<option key={uf} value={uf}>
{uf}
</option>
))}
</select>
</div>
<div className="field">
<label htmlFor="city">Cidade</label>
<select
name="city"
id="city"
value={selectedCity}
onChange={handleSelectCity}
>
<option value="0">Selecione uma cidade</option>
{cities.map((city) => (
<option key={city} value={city}>
{city}
</option>
))}
</select>
</div>
</div>
</fieldset>
<fieldset>
<legend>
<h2>Itens de coleta</h2>
<span>Selecione um ou mais itens a baixo.</span>
</legend>
<ul className="items-grid">
{items.map((item) => (
<li
key={item.id}
onClick={() => handleSelectItem(item.id)}
className={
selectedItems.includes(item.id)
? "selected"
: ""
}
>
<img src={item.image_url} alt={item.title} />
<span>{item.title}</span>
</li>
))}
</ul>
</fieldset>
<button type="submit">Cadastrar ponto de coleta</button>
</form>
</div>
);
}
Example #8
Source File: index.tsx From NextLevelWeek with MIT License | 4 votes |
OrphanageCreate: React.FC = () => {
const history = useHistory();
const [position, setPosition] = useState({ latitude: 0, longitude: 0 });
// Função para quando clicar no mapa
function handleMapClick(e: LeafletMouseEvent) {
// console.log(e);
const { lat, lng } = e.latlng;
setPosition({
latitude: lat,
longitude: lng,
});
}
// Campos do formulário
const [name, setName] = useState('');
const [about, setAbout] = useState('');
const [instructions, setInstructions] = useState('');
const [opening_hours, setOpeningHours] = useState('');
const [open_on_weekends, setOpenOnWeekends] = useState(true);
// Função para o envio do fomulário
async function handleSubmit(e: FormEvent) {
e.preventDefault();
const { latitude, longitude } = position;
const data = new FormData();
data.append('name', name);
data.append('about', about);
data.append('latitude', String(latitude));
data.append('longitude', String(longitude));
data.append('instructions', instructions);
data.append('opening_hours', opening_hours);
data.append('open_on_weekends', String(open_on_weekends));
images.forEach(image => {
data.append('images', image);
});
await api.post('/orphanages', data);
history.push('/app');
}
// Função quando seleciona a imagem
const [images, setImages] = useState<File[]>([]);
const [previewImages, setPreviewImages] = useState<string[]>([]);
function handleSelectImages(e: ChangeEvent<HTMLInputElement>) {
// console.log(e.target.files);
if (!e.target.files) {
return;
}
// Array de imagens
const selectedImages = Array.from(e.target.files)
setImages(selectedImages);
// Preview das imagens
const selectedImagesPreview = selectedImages.map(image => {
return URL.createObjectURL(image);
});
setPreviewImages(selectedImagesPreview);
}
return (
<Container>
<Sidebar />
<Content>
<Form onSubmit={handleSubmit}>
<fieldset>
<legend>Dados</legend>
<Map
center={[-5.8044209, -35.263095]}
style={{ width: '100%', height: 280 }}
zoom={15}
onclick={handleMapClick}
>
<TileLayer
url={`https://api.mapbox.com/styles/v1/mapbox/light-v10/tiles/256/{z}/{x}/{y}@2x?access_token=${process.env.REACT_APP_MAPBOX_TOKEN}`}
/>
{
position.latitude !== 0 &&
<Marker
interactive={false}
icon={happyMapIcon}
position={[position.latitude, position.longitude]}
/>
}
</Map>
<InputContainer>
<label htmlFor="name">Nome</label>
<input
id="name"
value={name}
onChange={e => setName(e.target.value)}
/>
</InputContainer>
<InputContainer>
<label htmlFor="about">Sobre <span>Máximo de 300 caracteres</span></label>
<textarea
id="about"
maxLength={300}
value={about}
onChange={e => setAbout(e.target.value)}
/>
</InputContainer>
<InputContainer>
<label htmlFor="images">Fotos</label>
<ImagesContainer>
{previewImages.map(image => {
return (
<img
key={image}
src={image}
alt={name}
/>
)
})}
<label htmlFor="image[]">
<FiPlus size={24} color="#15b6d6" />
</label>
</ImagesContainer>
<input
type="file"
id="image[]"
multiple
onChange={handleSelectImages}
/>
</InputContainer>
</fieldset>
<fieldset>
<legend>Visitação</legend>
<InputContainer>
<label htmlFor="instructions">Instruções</label>
<textarea
id="instructions"
value={instructions}
onChange={e => setInstructions(e.target.value)}
/>
</InputContainer>
<InputContainer>
<label htmlFor="opening_hours">Horário de funcionamento</label>
<input
id="opening_hours"
value={opening_hours}
onChange={e => setOpeningHours(e.target.value)}
/>
</InputContainer>
<InputContainer>
<label htmlFor="open_on_weekends">Atende no fim de semana</label>
<OpenOnWeekendContainer>
<SelectButton
type="button"
active={open_on_weekends}
onClick={() => setOpenOnWeekends(true)}
>Sim</SelectButton>
<SelectButton
type="button"
active={!open_on_weekends}
onClick={() => setOpenOnWeekends(false)}
>Não</SelectButton>
</OpenOnWeekendContainer>
</InputContainer>
</fieldset>
<button type="submit">Confirmar</button>
</Form>
</Content>
</Container>
);
}
Example #9
Source File: CreateOrphanage.tsx From happy with MIT License | 4 votes |
export default function CreateOrphanage() {
const history = useHistory();
const [position, setPosition] = useState({ latitude: 0, longitude: 0 });
const [name, setName] = useState('');
const [about, setAbout] = useState('');
const [instructions, setInstructions] = useState('');
const [opening_hours, setOpeningHours] = useState('');
const [open_on_weekends, setOpenOnWeekends] = useState(true);
const [images, setImages] = useState<File[]>([]);
const [previewImages, setPreviewImages] = useState<PreviewImage[]>([]);
function handleMapClick(event: LeafletMouseEvent) {
const { lat, lng } = event.latlng;
setPosition({
latitude: lat,
longitude: lng
});
}
async function handleSubmit(event: FormEvent) {
event.preventDefault();
const { latitude, longitude } = position;
const data = new FormData();
data.append('name', name);
data.append('about', about);
data.append('latitude', String(latitude));
data.append('longitude', String(longitude));
data.append('instructions', instructions);
data.append('opening_hours', opening_hours);
data.append('open_on_weekends', String(open_on_weekends));
images.forEach(image => {
data.append('images', image);
});
await api.post('/orphanages', data);
alert('Orfanato cadastrado com sucesso!');
history.push('/app');
}
function handleSelectImages(event: ChangeEvent<HTMLInputElement>) {
if (!event.target.files) {
return;
}
const selectedImages = Array.from(event.target.files);
event.target.value = "";
setImages(selectedImages);
const selectedImagesPreview = selectedImages.map(image => {
return { name: image.name, url: URL.createObjectURL(image) };
});
setPreviewImages(selectedImagesPreview);
}
function handleRemoveImage(image: PreviewImage) {
setPreviewImages(
previewImages.map((image) => image).filter((img) => img.url !== image.url)
);
setImages(
images.map((image) => image).filter((img) => img.name !== image.name)
);
}
return (
<div id="page-create-orphanage">
<Sidebar />
<main>
<form onSubmit={handleSubmit} className="create-orphanage-form">
<fieldset>
<legend>Dados</legend>
<Map
center={[-27.2092052, -49.6401092]}
style={{ width: '100%', height: 280 }}
zoom={15}
onclick={handleMapClick}
>
<TileLayer
url={`https://api.mapbox.com/styles/v1/mapbox/light-v10/tiles/256/{z}/{x}/{y}@2x?access_token=${process.env.REACT_APP_MAPBOX_TOKEN}`}
/>
{position.latitude !== 0 && (
<Marker
interactive={false}
icon={mapIcon}
position={[
position.latitude,
position.longitude
]}
/>
)}
</Map>
<div className="input-block">
<label htmlFor="name">Nome</label>
<input
id="name"
value={name}
onChange={event => setName(event.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="about">Sobre <span>Máximo de 300 caracteres</span></label>
<textarea
id="name"
maxLength={300}
value={about}
onChange={event => setAbout(event.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="images">Fotos</label>
<div className="images-container">
{previewImages.map((image) => {
return (
<div key={image.url}>
<span
className="remove-image"
onClick={() => handleRemoveImage(image)}
>
<FiX size={18} color="#ff669d" />
</span>
<img src={image.url} alt={name} className="new-image" />
</div>
);
})}
<label htmlFor="image[]" className="new-image">
<FiPlus size={24} color="#15b6d6" />
</label>
</div>
<input
type="file"
multiple
accept=".png, .jpg, .jpeg"
onChange={handleSelectImages}
id="image[]"
/>
</div>
</fieldset>
<fieldset>
<legend>Visitação</legend>
<div className="input-block">
<label htmlFor="instructions">Instruções</label>
<textarea
id="instructions"
value={instructions}
onChange={event => setInstructions(event.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="opening_hours">Horário de Funcionamento</label>
<input
id="opening_hours"
value={opening_hours}
onChange={event => setOpeningHours(event.target.value)}
/>
</div>
<div className="input-block">
<label htmlFor="open_on_weekends">Atende fim de semana</label>
<div className="button-select">
<button
type="button"
className={open_on_weekends ? 'active' : ''}
onClick={() => setOpenOnWeekends(true)}
>
Sim
</button>
<button
type="button"
className={!open_on_weekends ? 'active' : ''}
onClick={() => setOpenOnWeekends(false)}
>
Não
</button>
</div>
</div>
</fieldset>
<button className="confirm-button" type="submit">
Confirmar
</button>
</form>
</main>
</div>
);
}
Example #10
Source File: index.tsx From nlw-ecoleta with MIT License | 4 votes |
export function CreatePoint() {
const [items, setItems] = useState<Item[]>([]);
const [ufs, setUfs] = useState<string[]>([]);
const [cities, setCities] = useState<string[]>([]);
const [selectedItems, setSelectedItems] = useState<number[]>([]);
const [initialPosition, setInitialPosition] = useState<[number, number]>([0, 0]);
const [selectedFile, setSelectedFile] = useState<File>();
const [formData, setFormData] = useState({
name: '',
email: '',
whatsapp: '',
});
const [selectedUf, setSelectedUf] = useState('0');
const [selectedCity, setSelectedCity] = useState('0');
const [selectedPosition, setSelectedPosition] = useState<[number, number]>([0, 0]);
const history = useNavigate();
useEffect (() => {
navigator.geolocation.getCurrentPosition(position => {
const { latitude, longitude } = position.coords;
setInitialPosition([latitude, longitude]);
});
}, []);
useEffect(() => {
api.get('items').then(response => {
setItems(response.data);
});
}, []);
useEffect(() => {
axios
.get<IBGEUFResponse[]>('https://servicodados.ibge.gov.br/api/v1/localidades/estados')
.then(response => {
const ufInitials = response.data.map(uf => uf.sigla);
setUfs(ufInitials);
});
}, []);
useEffect(() => {
if(selectedUf === '0') {
return;
}
axios
.get<IBGECityResponse[]>(`https://servicodados.ibge.gov.br/api/v1/localidades/estados/${selectedUf}/municipios`)
.then(response => {
const cityNames = response.data.map(city => city.nome);
setCities(cityNames);
});
} , [selectedUf]);
function handleSelectUf(event: ChangeEvent<HTMLSelectElement>) {
const uf = event.target.value;
setSelectedUf(uf);
}
function handleSelectCity(event: ChangeEvent<HTMLSelectElement>) {
const city = event.target.value;
setSelectedCity(city);
}
function handleMapClick(event: LeafletMouseEvent) {
setSelectedPosition([
event.latlng.lat,
event.latlng.lng,
]);
}
function handleInputChange(event: ChangeEvent<HTMLInputElement>){
const { name, value } = event.target;
setFormData({ ...formData, [name]: value });
}
function handleSelectItem(id: number) {
const alreadySelected = selectedItems.findIndex(item => item === id);
if (alreadySelected >= 0) {
const filteredItems = selectedItems.filter(item => item !== id);
setSelectedItems(filteredItems);
}
else {
setSelectedItems([ ...selectedItems, id ]);
}
}
async function handleSubmit(event: FormEvent) {
event.preventDefault();
const { name, email, whatsapp } = formData;
const uf = selectedUf;
const city = selectedCity;
const [ latitude, longitude ] = selectedPosition;
const items = selectedItems;
const data = new FormData();
data.append('name', name);
data.append('email', email);
data.append('whatsapp', whatsapp);
data.append('uf', uf);
data.append('city', city);
data.append('latitude', String(latitude));
data.append('longitude', String(longitude));
data.append('items', items.join(','));
if (selectedFile) {
data.append('image', selectedFile);
}
await api.post('points', data);
alert('Ponto de coleta criado.');
history('/');
}
// function LocationMarker() {
// const map = useMapEvents({
// click() {
// map.locate();
// },
// locationfound(e) {
// setSelectedPosition(e.latlng as any);
// map.flyTo(e.latlng, map.getZoom());
// },
// });
// return selectedPosition === null ? null : (
// <Marker position={selectedPosition} />
// );
// }
return (
<div id='page-create-point'>
<header>
<img src={ logo } alt="Ecoleta" />
<Link to='/'>
<FiArrowLeft />
Voltar para home
</Link>
</header>
<form onSubmit={handleSubmit}>
<h1>Cadastro do <br /> ponto de coleta</h1>
<Dropzone onFileUploaded={setSelectedFile} />
<fieldset>
<legend>
<h2> Dados </h2>
</legend>
<div className='field'>
<label htmlFor="name"> Nome da entidade</label>
<input
type="text"
name="name"
id="name"
onChange={handleInputChange}
/>
</div>
<div className="field-group">
<div className='field'>
<label htmlFor="email"> E-mail</label>
<input
type="email"
name="email"
id="email"
onChange={handleInputChange}
/>
</div>
<div className='field'>
<label htmlFor="name"> Whatsapp</label>
<input
type="text"
name="whatsapp"
id="whatsapp"
onChange={handleInputChange}
/>
</div>
</div>
</fieldset>
<fieldset>
<legend>
<h2> Endereço </h2>
<span>Selecione o endereço no mapa</span>
</legend>
<MapContainer center={initialPosition} zoom={5} onClick={handleMapClick}>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={selectedPosition}></Marker>
</MapContainer>
<div className="field-group">
<div className="field">
<label htmlFor="uf"> Estado (UF)</label>
<select
name="uf"
id="uf"
value={selectedUf}
onChange={handleSelectUf}
>
<option value="0"> Selecione um UF</option>
{ufs.map(uf => (
<option key={uf} value={uf}> {uf}</option>
))}
</select>
</div>
<div className="field">
<label htmlFor="city"> Cidade</label>
<select
name="city"
id="city"
value={selectedCity}
onChange={handleSelectCity}
>
<option value="0"> Selecione uma cidade</option>
{cities.map(city => (
<option key={city} value={city}> {city}</option>
))}
</select>
</div>
</div>
</fieldset>
<fieldset>
<legend>
<h2> Itens de coleta </h2>
<span>Selecione um ou mais itens abaixo</span>
</legend>
<ul className='items-grid'>
{items.map(item =>(
<li
key={item.id}
onClick={() => handleSelectItem(item.id)}
className={selectedItems.includes(item.id) ? 'selected' : ''}
>
<img src={item.image_url} alt={item.title}/>
<span>{item.title}</span>
</li>
))}
</ul>
</fieldset>
<button type="submit">Cadastrar ponto de coleta</button>
</form>
</div>
);
}
Example #11
Source File: index.tsx From nlw-01-omnistack with MIT License | 4 votes |
CreatePoint = () => {
const [items, setItems] = useState<Item[]>([]);
const [ufs, setUfs] = useState<string[]>([]);
const [cities, setCities] = useState<string[]>([]);
const [initialPosition, setInitialPosition] = useState<[number, number]>([0, 0]);
const [formData, setFormData] = useState({
name: '',
email: '',
whatsapp: '',
});
const [selectedUf, setSelectedUf] = useState('0');
const [selectedCity, setSelectedCity] = useState('0');
const [selectedItems, setSelectedItems] = useState<number[]>([]);
const [selectedPosition, setSelectedPosition] = useState<[number, number]>([0, 0]);
const [selectedFile, setSelectedFile] = useState<File>();
const history = useHistory();
useEffect(() => {
navigator.geolocation.getCurrentPosition(position => {
const { latitude, longitude } = position.coords;
setInitialPosition([latitude, longitude]);
});
}, []);
useEffect(() => {
api.get('items').then(response => {
setItems(response.data);
});
}, []);
useEffect(() => {
axios.get<IBGEUFResponse[]>('https://servicodados.ibge.gov.br/api/v1/localidades/estados').then(response => {
const ufInitials = response.data.map(uf => uf.sigla);
setUfs(ufInitials);
});
}, []);
useEffect(() => {
if (selectedUf === '0') {
return;
}
axios
.get<IBGECityResponse[]>(`https://servicodados.ibge.gov.br/api/v1/localidades/estados/${selectedUf}/municipios`)
.then(response => {
const cityNames = response.data.map(city => city.nome);
setCities(cityNames);
});
}, [selectedUf]);
function handleSelectUf(event: ChangeEvent<HTMLSelectElement>) {
const uf = event.target.value;
setSelectedUf(uf);
}
function handleSelectCity(event: ChangeEvent<HTMLSelectElement>) {
const city = event.target.value;
setSelectedCity(city);
}
function handleMapClick(event: LeafletMouseEvent) {
setSelectedPosition([
event.latlng.lat,
event.latlng.lng,
])
}
function handleInputChange(event: ChangeEvent<HTMLInputElement>) {
const { name, value } = event.target;
setFormData({ ...formData, [name]: value });
}
function handleSelectItem(id: number) {
const alreadySelected = selectedItems.findIndex(item => item === id);
if (alreadySelected >= 0) {
const filteredItems = selectedItems.filter(item => item !== id);
setSelectedItems(filteredItems);
} else {
setSelectedItems([ ...selectedItems, id ]);
}
}
async function handleSubmit(event: FormEvent) {
event.preventDefault();
const { name, email, whatsapp } = formData;
const uf = selectedUf;
const city = selectedCity;
const [latitude, longitude] = selectedPosition;
const items = selectedItems;
const data = new FormData();
data.append('name', name);
data.append('email', email);
data.append('whatsapp', whatsapp);
data.append('uf', uf);
data.append('city', city);
data.append('latitude', String(latitude));
data.append('longitude', String(longitude));
data.append('items', items.join(','));
if (selectedFile) {
data.append('image', selectedFile)
}
await api.post('points', data);
alert('Ponto de coleta criado!');
history.push('/');
}
return (
<div id="page-create-point">
<header>
<img src={logo} alt="Ecoleta" />
<Link to="/">
<FiArrowLeft />
Voltar para home
</Link>
</header>
<form onSubmit={handleSubmit}>
<h1>Cadastro do <br /> ponto de coleta</h1>
<Dropzone onFileUploaded={setSelectedFile} />
<fieldset>
<header role="legend">
<h2>Dados</h2>
</header>
<div className="field">
<label htmlFor="name">Nome da entidade</label>
<input
type="text"
name="name"
id="name"
onChange={handleInputChange}
/>
</div>
<div className="field-group">
<div className="field">
<label htmlFor="email">E-mail</label>
<input
type="email"
name="email"
id="email"
onChange={handleInputChange}
/>
</div>
<div className="field">
<label htmlFor="whatsapp">Whatsapp</label>
<input
type="text"
name="whatsapp"
id="whatsapp"
onChange={handleInputChange}
/>
</div>
</div>
</fieldset>
<fieldset>
<header role="legend">
<h2>Endereço</h2>
<span>Selecione o endereço no mapa</span>
</header>
<Map center={initialPosition} zoom={15} onClick={handleMapClick}>
<TileLayer
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={selectedPosition} />
</Map>
<div className="field-group">
<div className="field">
<label htmlFor="uf">Estado (UF)</label>
<select
name="uf"
id="uf"
value={selectedUf}
onChange={handleSelectUf}
>
<option value="0">Selecione uma UF</option>
{ufs.map(uf => (
<option key={uf} value={uf}>{uf}</option>
))}
</select>
</div>
<div className="field">
<label htmlFor="city">Cidade</label>
<select
name="city"
id="city"
value={selectedCity}
onChange={handleSelectCity}
>
<option value="0">Selecione uma cidade</option>
{cities.map(city => (
<option key={city} value={city}>{city}</option>
))}
</select>
</div>
</div>
</fieldset>
<fieldset>
<header role="legend">
<h2>Ítens de coleta</h2>
<span>Selecione um ou mais ítens abaixo</span>
</header>
<ul className="items-grid">
{items.map(item => (
<li
key={item.id}
onClick={() => handleSelectItem(item.id)}
className={selectedItems.includes(item.id) ? 'selected' : ''}
>
<img src={item.image_url} alt={item.title} />
<span>{item.title}</span>
</li>
))}
</ul>
</fieldset>
<button type="submit">
Cadastrar ponto de coleta
</button>
</form>
</div>
);
}
Example #12
Source File: index.tsx From ecoleta with MIT License | 4 votes |
CreatePoint = () => {
const [formData, setFormData] = useState({
name: '',
email: '',
whatsapp: '',
});
const [initialPosition, setInitialPosition] = useState<[number, number]>([
0,
0,
]);
const [ufs, setUfs] = useState<string[]>([]);
const [cities, setCities] = useState<string[]>([]);
const [items, setItems] = useState<Item[]>([]);
const [selectedUf, setSelectedUf] = useState('0');
const [selectedCity, setSelectedCity] = useState('0');
const [selectedItems, setSelectedItems] = useState<number[]>([]);
const [selectedPosition, setSelectedPosition] = useState<[number, number]>([
0,
0,
]);
const [selectedFile, setSelectedFile] = useState<File>();
const history = useHistory();
useEffect(() => {
navigator.geolocation.getCurrentPosition((position) => {
const { latitude, longitude } = position.coords;
setInitialPosition([latitude, longitude]);
});
}, []);
useEffect(() => {
api.get('/items').then((response) => {
setItems(response.data);
});
}, []);
useEffect(() => {
axios
.get<IBGEUFResponse[]>(
'https://servicodados.ibge.gov.br/api/v1/localidades/estados'
)
.then((response) => {
const ufInitials = response.data.map((uf) => uf.sigla);
setUfs(ufInitials);
});
}, []);
useEffect(() => {
if (selectedUf === '0') {
return;
}
axios
.get<IBGECityResponse[]>(
`https://servicodados.ibge.gov.br/api/v1/localidades/estados/${selectedUf}/municipios`
)
.then((response) => {
const cityNames = response.data.map((city) => city.nome);
setCities(cityNames);
});
}, [selectedUf]);
function handleSelectUf(event: ChangeEvent<HTMLSelectElement>) {
const uf = event.target.value;
setSelectedUf(uf);
}
function handleSelectCity(event: ChangeEvent<HTMLSelectElement>) {
const city = event.target.value;
setSelectedCity(city);
}
function handleMapClick(event: LeafletMouseEvent) {
setSelectedPosition([event.latlng.lat, event.latlng.lng]);
}
function handleInputChange(event: ChangeEvent<HTMLInputElement>) {
const { name, value } = event.target;
setFormData({ ...formData, [name]: value });
}
function handleSelectItem(id: number) {
const alreadySelected = selectedItems.findIndex((item) => item === id);
if (alreadySelected >= 0) {
const filteredItems = selectedItems.filter((item) => item !== id);
setSelectedItems(filteredItems);
} else {
setSelectedItems([...selectedItems, id]);
}
}
async function handleSubmit(event: FormEvent) {
event.preventDefault();
const { name, email, whatsapp } = formData;
const uf = selectedUf;
const city = selectedCity;
const [latitude, longitude] = selectedPosition;
const items = selectedItems;
const data = new FormData();
data.append('name', name);
data.append('email', email);
data.append('whatsapp', whatsapp);
data.append('uf', uf);
data.append('city', city);
data.append('latitude', String(latitude));
data.append('longitude', String(longitude));
data.append('items', items.join(','));
if (selectedFile) {
data.append('image', selectedFile);
}
await api.post('/points', data);
alert('Ponto de coleta criado');
history.push('/');
}
return (
<div id="page-create-point">
<header>
<img src={logo} alt="Ecoleta" />
<Link to="/">
<FiArrowLeft />
Voltar para home
</Link>
</header>
<form onSubmit={handleSubmit} autoComplete="off">
<h1>
Cadastro do <br />
ponto de coleta
</h1>
<Dropzone onFileUploaded={setSelectedFile} />
<fieldset>
<legend>
<h2>Dados</h2>
</legend>
<div className="field">
<label htmlFor="name">Nome da entidade</label>
<input
type="text"
name="name"
id="name"
onChange={handleInputChange}
/>
</div>
<div className="field-group">
<div className="field">
<label htmlFor="email">E-mail</label>
<input
type="email"
name="email"
id="email"
onChange={handleInputChange}
/>
</div>
<div className="field">
<label htmlFor="whatsapp">Whatsapp</label>
<input
type="text"
name="whatsapp"
id="whatsapp"
onChange={handleInputChange}
/>
</div>
</div>
</fieldset>
<fieldset>
<legend>
<h2>Endereço</h2>
<span>Selecione o endereço no mapa</span>
</legend>
<Map center={initialPosition} zoom={15} onClick={handleMapClick}>
<TileLayer
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={selectedPosition} />
</Map>
<div className="field-group">
<div className="field">
<label htmlFor="uf">Estado (UF)</label>
<select
name="uf"
id="uf"
value={selectedUf}
onChange={handleSelectUf}
>
<option value="0">Selecione uma UF</option>
{ufs.map((uf) => (
<option key={uf} value={uf}>
{uf}
</option>
))}
</select>
</div>
<div className="field">
<label htmlFor="city">Cidade</label>
<select
name="city"
id="city"
value={selectedCity}
onChange={handleSelectCity}
>
<option value="0">Selecione uma cidade</option>
{cities.map((city) => (
<option key={city} value={city}>
{city}
</option>
))}
</select>
</div>
</div>
</fieldset>
<fieldset>
<legend>
<h2>Itens de coleta</h2>
<span>Selecione um ou mais itens abaixo</span>
</legend>
<ul className="items-grid">
{items.map((item) => (
<li
key={item.id}
className={selectedItems.includes(item.id) ? 'selected' : ''}
onClick={() => handleSelectItem(item.id)}
>
<img src={item.image_url} alt={item.title} />
<span>{item.title}</span>
</li>
))}
</ul>
</fieldset>
<button type="submit">Cadastrar ponto de coleta</button>
</form>
</div>
);
}
Example #13
Source File: index.tsx From ecoleta with MIT License | 4 votes |
CreatePoint: React.FC = () => {
const [items, setItems] = useState<Item[]>([]);
const [selectedItems, setSelectedItems] = useState<number[]>([]);
const [ufs, setUfs] = useState<string[]>([]);
const [selectedUf, setSelectedUf] = useState('0');
const [cities, setCities] = useState<string[]>([]);
const [selectedCity, setSelectedCity] = useState('0');
const [selectedPosition, setSelectedPosition] = useState<[number, number]>([0, 0]);
const [initialMapPosition, setInitialMapPosition] = useState<[number, number]>([0, 0]);
const [selectedFile, setSelectedFile] = useState<File>();
const [formData, setFormData] = useState({
name: '',
email: '',
whatsapp: '',
});
const history = useHistory();
useEffect(() => {
navigator.geolocation.getCurrentPosition(position => {
const { latitude, longitude } = position.coords;
setInitialMapPosition([latitude, longitude]);
});
}, []);
useEffect(() => {
api.get('items').then(response => {
setItems(response.data);
});
}, []);
useEffect(() => {
axios
.get<IBGEUFResponse[]>('https://servicodados.ibge.gov.br/api/v1/localidades/estados')
.then(response => {
const ufInitials = response.data.map(uf => uf.sigla);
setUfs(ufInitials);
});
}, []);
useEffect(() => {
if (selectedUf === '0') {
return;
}
axios
.get<IBGECityResponse[]>(`https://servicodados.ibge.gov.br/api/v1/localidades/estados/${selectedUf}/municipios`)
.then(response => {
const cityNames = response.data.map(city => city.nome);
setCities(cityNames);
});
}, [selectedUf]);
function handleSelectUf(event: ChangeEvent<HTMLSelectElement>) {
const uf = event.target.value;
setSelectedUf(uf);
}
function handleSelectCity(event: ChangeEvent<HTMLSelectElement>) {
const city = event.target.value;
setSelectedCity(city);
}
function handleMapClick(event: LeafletMouseEvent) {
setSelectedPosition([
event.latlng.lat,
event.latlng.lng
])
}
function handleInputChange(event: ChangeEvent<HTMLInputElement>) {
const { name, value } = event.target;
setFormData({ ...formData, [name]: value });
}
function handleSelectItem(id: number) {
const alreadSelected = selectedItems.findIndex(item => item === id);
if (alreadSelected >= 0) {
const filteredItems = selectedItems.filter(item => item !== id);
setSelectedItems(filteredItems);
} else {
setSelectedItems([ ...selectedItems, id]);
}
}
async function handleSubmit(event: FormEvent) {
event.preventDefault();
const { name, email, whatsapp } = formData;
const uf = selectedUf;
const city = selectedCity;
const [latitude, longitude] = selectedPosition;
const items = selectedItems;
const data = new FormData();
data.append('name', name);
data.append('email', email);
data.append('whatsapp', whatsapp);
data.append('uf', uf);
data.append('city', city);
data.append('latitude', String(latitude));
data.append('longitude', String(longitude));
data.append('items', items.join(','));
if (selectedFile) {
data.append('image', selectedFile);
}
await api.post('points', data);
alert('Ponto de Coleta Criado!');
history.push('/');
}
return (
<div id="page-create-point">
<header>
<img src={logo} alt="Ecoleta Logo"/>
<Link to="/">
<FiArrowLeft />
Voltar para home
</Link>
</header>
<form onSubmit={handleSubmit}>
<h1>Cadastro do <br />ponto de coleta</h1>
<Dropzone onFileUploaded={setSelectedFile} />
<fieldset>
<legend>
<h2>Dados</h2>
</legend>
<div className="field">
<label htmlFor="name">Nome da entidade</label>
<input
type="text"
name="name"
id="name"
onChange={handleInputChange}
/>
</div>
<div className="field-group">
<div className="field">
<label htmlFor="email">E-mail</label>
<input
type="email"
name="email"
id="email"
onChange={handleInputChange}
/>
</div>
<div className="field">
<label htmlFor="whatsapp">Whatsapp</label>
<input
type="text"
name="whatsapp"
id="whatsapp"
onChange={handleInputChange}
/>
</div>
</div>
</fieldset>
<fieldset>
<legend>
<h2>Endereço</h2>
<span>Selecione o endereço no mapa</span>
</legend>
<Map center={initialMapPosition} zoom={15} onClick={handleMapClick}>
<TileLayer
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={selectedPosition}/>
</Map>
<div className="field-group">
<div className="field">
<label htmlFor="uf">Estado (UF)</label>
<select
name="uf"
id="uf"
value={selectedUf}
onChange={handleSelectUf}
>
<option value="0">Selecione uma UF</option>
{ufs.map(uf => (
<option key={uf} value={uf}>{uf}</option>
))}
</select>
</div>
<div className="field">
<label htmlFor="city">Cidade</label>
<select
name="city"
id="city"
value={selectedCity}
onChange={handleSelectCity}
>
<option value="0">Selecione uma cidade</option>
{cities.map(city => (
<option key={city} value={city}>{city}</option>
))}
</select>
</div>
</div>
</fieldset>
<fieldset>
<legend>
<h2>Ítens de coleta</h2>
<span>Selecione um ou mais ítens abaixo</span>
</legend>
<ul className="items-grid">
{items.map(item => (
<li
key={item.id}
onClick={() => handleSelectItem(item.id)}
className={selectedItems.includes(item.id) ? 'selected' : ''}
>
<img src={item.image_url} alt={item.title}/>
<span>{item.title}</span>
</li>
))}
</ul>
</fieldset>
<button type="submit">
Cadastrar ponto de coleta
</button>
</form>
</div>
);
}