Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 49 additions & 15 deletions api/frontend_dic/src/components/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ const GridBasicExample=()=> {


const [isLoggedIn, setIsLoggedIn] = useState(false);

function getMinDiff(startTimeStamp, endTimeStamp) {
const msInMinute = 60 * 1000;

return Math.floor(
Math.abs(new Date(endTimeStamp) - new Date(startTimeStamp)) / msInMinute
)
}


const Go = (e) => {
Expand All @@ -27,20 +35,51 @@ const GridBasicExample=()=> {
'X-RapidAPI-Host': 'wordsapiv1.p.rapidapi.com'
}
};

fetch('https://wordsapiv1.p.rapidapi.com/words/'+words+'/definitions', options)

const fetchWordMeaning = () => {
fetch('https://wordsapiv1.p.rapidapi.com/words/'+words+'/definitions', options)
.then(response => response.json())
.then(response => {
setdefinitions([...response.definitions])
// if(localStorage.getItem('searched')) {
// let searched = JSON.parse(localStorage.getItem('searched'));
// } else {

// }

})
.catch(err => console.error(err));
console.log(definitionss);
}

if(localStorage.getItem('searched')) {
let searched = JSON.parse(localStorage.getItem('searched'));
if(searched.length > 1) {
console.log('greater than one')
let last_entry = searched[searched.length -1];
if(getMinDiff(last_entry, Number(new Date())) < 2) {
console.log('not pass two mins')
alert(`Please you must wait for extra ${2 - getMinDiff(last_entry, Number(new Date()))}minute${(2 - getMinDiff(last_entry, Number(new Date())) > 1 ? 's' : '')} before making another search`);
return;
} else {
// send request
fetchWordMeaning();
console.log('pass two mins')
let searched = [
Number(new Date())
]
localStorage.setItem('searched', JSON.stringify(searched))
}
console.log(last_entry)
} else {
fetchWordMeaning();
console.log('just on item')
let searched = JSON.parse(localStorage.getItem('searched'));
searched.push(Number(new Date()))
localStorage.setItem('searched', JSON.stringify(searched))
}
} else {
fetchWordMeaning();
let searched = [
Number(new Date())
]
localStorage.setItem('searched', JSON.stringify(searched))
console.log(searched);
}

}
// add to favorite DB
const Fav = (e) => {
Expand All @@ -61,8 +100,6 @@ const GridBasicExample=()=> {
}).then((res) =>{
return res.json()
}).then(data => {

console.log(data)
alert(data.message)
})

Expand All @@ -82,7 +119,6 @@ const GridBasicExample=()=> {
return res.json()
}).then(data => {
if(data.id){
console.log(data.id)

addFavourites('http://127.0.0.1:8000/api/add', data.id)
}
Expand All @@ -104,11 +140,10 @@ const GridBasicExample=()=> {
}).then((res) =>{
return res.json()
}).then(data => {
console.log(data)
// console.log(data)
})
}
const tokenId=localStorage.getItem('token')
console.log(tokenId);
if(tokenId){
fetch('http://127.0.0.1:8000/api/user', {
method: "GET",
Expand All @@ -120,7 +155,6 @@ const GridBasicExample=()=> {
}).then((res) =>{
return res.json()
}).then(data => {
console.log(data.id)
getFavourites('http://127.0.0.1:8000/api/data', data.id)
})
}
Expand Down