Skip to content

Commit

Permalink
Search by image is working
Browse files Browse the repository at this point in the history
  • Loading branch information
robotconscience committed May 31, 2024
1 parent 4be4964 commit d4aaca1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ const App = () => {
onChange={handleSearch}
/>
<span>or</span>
<ImageInput />
<ImageInput handleNewActiveObject={handleNewActiveObject} />
</div>
{errorMessage ?
<div>
Expand Down
50 changes: 20 additions & 30 deletions src/components/image-input/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from 'react';
import App from '../../App'
import PropTypes from 'prop-types';

const ImageInput = () => {
const ImageInput = ({ handleNewActiveObject }) => {
const defaultButtonText = '📸';
const [imageInputText, setImageInputText] = useState(defaultButtonText);

Expand Down Expand Up @@ -47,32 +47,22 @@ const ImageInput = () => {
};

const postBlob = async blob => {
console.log("Posting")
// const formData = new FormData();
// formData.append('image', blob, 'photo.png');

const request = await fetch(AZURE_API_IMAGE, {
method: 'POST',
body: blob,
headers: {
"Content-type": "application/octet-stream"
console.log("Uploading file...");
const request = new XMLHttpRequest();
const formData = new FormData();

request.open("POST", AZURE_API_IMAGE, true);
request.onreadystatechange = () => {
if (request.readyState === 4 && request.status === 200) {
const similarImages = JSON.parse(request.response)?.similarImages;
console.log(similarImages[0]);
// handleSearch(similarImages[0]);
setImageInputText('📸')
handleNewActiveObject(similarImages[0].objectId);
}
})
console.log(request)
const response = await request.json();
if (response.objectIDs) {
App.setErrorMessage(null);
const newObject = response.objectIDs[0];
App.handleNewActiveObject(newObject);
// new Azure version
} else if (response.similarImages) {
App.setErrorMessage(null);
// could randomize?
const index = Math.floor(Math.random()*response.similarImages.length)
const newObject = response.similarImages[index].objectId;
App.handleNewActiveObject(newObject);
}

};
formData.append("file", blob);
request.send(formData)
}

const handleOnChange = e => {
Expand Down Expand Up @@ -102,7 +92,7 @@ const ImageInput = () => {
);
};

// ImageInput.propTypes = {
// searchObjects: PropTypes.func
// };
ImageInput.propTypes = {
handleNewActiveObject: PropTypes.func
};
export default ImageInput;

0 comments on commit d4aaca1

Please sign in to comment.