Working form and selecting photos
parent
724b047eaa
commit
2082dc1866
@ -0,0 +1,60 @@
|
|||||||
|
import { Address } from "./location";
|
||||||
|
interface FormState {
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
plate: string;
|
||||||
|
offenses: string[];
|
||||||
|
comment: string;
|
||||||
|
my_address: string;
|
||||||
|
address: Address;
|
||||||
|
}
|
||||||
|
interface FormAction {
|
||||||
|
type: string;
|
||||||
|
payload: {
|
||||||
|
field: string;
|
||||||
|
value: string | Address;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
export const fromInitialState = {
|
||||||
|
name: "",
|
||||||
|
email: "",
|
||||||
|
plate: "",
|
||||||
|
my_address: "",
|
||||||
|
offenses: [],
|
||||||
|
comment: "",
|
||||||
|
address: {
|
||||||
|
house_number: "",
|
||||||
|
road: "",
|
||||||
|
suburb: "",
|
||||||
|
neighbourhood: "",
|
||||||
|
hamlet: "",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
export function formReducer(state: FormState, action: FormAction) {
|
||||||
|
switch (action.type) {
|
||||||
|
case "CHANGE_FIELD":
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
//action.payload.field is equal to name of the imput, example: e.target.name = plate
|
||||||
|
[action.payload.field]: action.payload.value,
|
||||||
|
};
|
||||||
|
case "ADD_OFFENCE":
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
offenses: [...state.offenses, action.payload.value],
|
||||||
|
};
|
||||||
|
case "DELETE_OFFENCE":
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
offenses: state.offenses.filter(
|
||||||
|
(offence: string) => offence !== action.payload.value
|
||||||
|
),
|
||||||
|
};
|
||||||
|
case "CHANGE_ADDRESS":
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
import request from "request";
|
||||||
|
|
||||||
|
export async function getJSON(uri: string): Promise<Object> {
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
let data = "";
|
||||||
|
const reply = request({
|
||||||
|
uri,
|
||||||
|
headers: {
|
||||||
|
"User-Agent": "Poznanski Parkingowy Patrol - telegram bot",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
reply.on("data", (chunk) => {
|
||||||
|
data += chunk;
|
||||||
|
});
|
||||||
|
reply.on("error", reject);
|
||||||
|
reply.on("end", () => resolve(JSON.parse(data)));
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
import { getJSON } from "./http";
|
||||||
|
|
||||||
|
export type Location = {
|
||||||
|
lat: number;
|
||||||
|
lon: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Address = {
|
||||||
|
house_number: string;
|
||||||
|
road: string;
|
||||||
|
suburb: string;
|
||||||
|
neighbourhood: string;
|
||||||
|
hamlet: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function getAddress(location: Location): Promise<Address> {
|
||||||
|
const nominatim_url = `https://nominatim.openstreetmap.org/reverse?lat=${location.lat}&lon=${location.lon}&format=jsonv2`;
|
||||||
|
const nominatim_data = (await getJSON(nominatim_url)) as {
|
||||||
|
address: Address;
|
||||||
|
};
|
||||||
|
return nominatim_data.address;
|
||||||
|
}
|
Loading…
Reference in New Issue