diff --git a/apiform.ts b/apiform.ts index 0b59703..ae4ac21 100644 --- a/apiform.ts +++ b/apiform.ts @@ -6,7 +6,8 @@ const api = axios.create({ baseURL: 'http://127.0.0.1:8000/api/', headers: { "Content-Type": "multipart/form-data", - "Authorization": "Token 043ac7dda62905f56ca9d0029ccf9dff922dd54d" + "Authorization": "Token 043ac7dda62905f56ca9d0029ccf9dff922dd54d", + "Accept": "application/json", }, }) api.interceptors.request.use((config) => { diff --git a/components/OpportunityForm.tsx b/components/OpportunityForm.tsx index 88224ed..114725d 100644 --- a/components/OpportunityForm.tsx +++ b/components/OpportunityForm.tsx @@ -12,22 +12,17 @@ function extractNumber(url: string): number { } export default function Form({ shelters }: { shelters: ShelterProp[] }) { - const [formData, setFormData] = useState({} as PetCardProps); - - const handleImageChange = (event: React.ChangeEvent) => { - if (event.target.files && event.target.files[0]) { - const reader = new FileReader(); - reader.onload = (e) => { - if (e.target && typeof e.target.result === "string") { - setFormData({ - ...formData, - image: e.target.result, - }); - } - }; - reader.readAsDataURL(event.target.files[0]); - } - }; + const [formData, setFormData] = useState({ + type: "", + name: "", + species: "", + breed: "", + age: "", + gender: "", + size: "", + description: "", + shelter: 0, + }); const handleChange = (event: any) => { setFormData({ @@ -35,6 +30,13 @@ export default function Form({ shelters }: { shelters: ShelterProp[] }) { [event.target.name]: event.target.value, }); }; + + const handleFileChange = (event: any) => { + setFormData({ + ...formData, + [event.target.name]: event.target.files[0], + }); + } // async function fetchData(username: string, password: string): Promise { // try { // const response = await api.post('api-token-auth/', { @@ -49,27 +51,30 @@ export default function Form({ shelters }: { shelters: ShelterProp[] }) { // } // } - const handleSubmit = async (event: React.FormEvent) => { + const handleSubmit = async (event: any) => { event.preventDefault(); + const config = { + headers: { + "Content-Type": "multipart/form-data", + "Authorization": "Token 043ac7dda62905f56ca9d0029ccf9dff922dd54d", + "Accept": "application/json", + }, + } + const form = event.target; console.log("event", form); - const formData = { - image: form.image.files[0], - type: form.type.value, - name: form.name.value, - species: form.species.value, - breed: form.breed.value, - age: form.age.value, - gender: form.gender.value, - size: form.size.value, - description: form.description.value, - shelter: Number(form.shelter.value), - // and so on for other fields + const data = new FormData(); + + // check if form is filled + if (!form.name.value || !form.species.value || !form.breed.value || !form.age.value || !form.gender.value || !form.size.value || !form.description.value || !form.shelter.value) { + return; } + console.log("formData", formData); - formapi.post("opportunities/", formData).then((response) => { + formData.shelter = Number(formData.shelter); + formapi.post("opportunities/", formData, config).then((response) => { console.log(response); }) }; @@ -82,7 +87,6 @@ export default function Form({ shelters }: { shelters: ShelterProp[] }) {
@@ -97,7 +101,7 @@ export default function Form({ shelters }: { shelters: ShelterProp[] }) { name="image" id="image" accept="image/*" - onChange={handleImageChange} + onChange={handleFileChange} className="w-full border border-gray-300 rounded p-2" />
@@ -107,10 +111,11 @@ export default function Form({ shelters }: { shelters: ShelterProp[] }) { @@ -177,10 +182,11 @@ export default function Form({ shelters }: { shelters: ShelterProp[] }) { @@ -192,10 +198,11 @@ export default function Form({ shelters }: { shelters: ShelterProp[] }) {