import wixUsers from 'wix-users';
import wixData from 'wix-data';
import {session} from 'wix-storage';
import wixLocation from 'wix-location';
$w.onReady(function () {
// check if user has a pricing Plan
let user = wixUsers.currentUser;
let planName;
user.getPricingPlans()
.then((pricingPlans) => {
let userPlan = pricingPlans[0];
planName = userPlan.name;
});
if (planName === undefined) {
// show at least 2-days-old results
var today = new Date().getTime(); // date in milliseconds
var twoDaysAgo = new Date(today - (2 * 24 * 60 * 60 * 1000));
$w("#dataset1").setFilter(wixData.filter()
.lt("_updatedDate", twoDaysAgo)); // remove latest results
//console.log("_createdDate is: " );
console.log("2 days ago is: " + twoDaysAgo);
console.log("user is: " + user.role);
console.log("user plan is: " + planName);
}
else if (planName === "Premium"){ // premium member, no filter required
$w("#textConnectedAsPremium").show();
console.log(planName);
}
// show only approved data
$w("#dataset1").setFilter(wixData.filter().eq("isApproved", true));
// show all cities in City-dropdown
wixData.query('oppositeSearch')
.ascending("city")
.eq("isApproved", true)
.find()
.then (res => {
let options = [{"value":'', 'label': 'כל הארץ'}];
const uniqueCities = getUniqueCities(res.items);
$w("#dropdownFilterCity").options = options.concat((buildOptions(uniqueCities)));
})
// actions for repeater
$w("#repeater1").onItemReady(($item, itemData, inx) => {
// show a specific container's phone number
$item("#buttonContact").onClick(event => {
$item("#textPhoneNumber").show();
$item("#imageWhatsAppGreen").show();
$item("#imagePhone").show();
})
// create whatsapp url for each item
let whatsAppURL = "https://api.whatsapp.com/send/?phone=972"
+ itemData.phoneNumber.replace("-", "")
+ "&text=היי, ראיתי את המודעה שלך באתר גרים ודרים ויש לי מה להציע לך. זה עדיין רלוונטי?";
$item("#imageWhatsAppGreen").link = whatsAppURL;
$item("#imageWhatsAppGreen").target = "_blank";
// create a phone number to call, for each item
let phoneCall = $item("#imagePhone").link = "tel:+972-" + itemData.phoneNumber;
// show relevant icons for each item
if(!itemData.furniture) {
$item("#lineFurniture").show();
}
if(!itemData.animals) {
$item("#lineAnimals").show();
}
if(!itemData.yard) {
$item("#lineYard").show();
}
});
// delete outdated data
get_deleteEventsDatabaseOldData();
});
// delete outdated posts from dataset
export async function get_deleteEventsDatabaseOldData() {
let itemsToDelete;
let oneMonthAgoDate = new Date(
new Date().getFullYear(),
new Date().getMonth() - 1, // Add the month here (Change)
new Date().getDate()
);
//console.log(oneMonthAgoDate);
let returnoptions = {
"headers": {
"Content-Type": "application/json"
}
};
let options = {
"suppressAuth": true,
"suppressHooks": true
};
return await wixData.query('oppositeSearch')
.lt("_updatedDate", oneMonthAgoDate)
.find(options)
.then((results) => {
if (results.items.length > 0) {
itemsToDelete = results.items;
for (let i in itemsToDelete)
{
wixData.remove("oppositeSearch", itemsToDelete[i]._id, options);
}
returnoptions.body = {
"Status": "Items Deleted = " + results.items.length
};
}
returnoptions.body = {
"Status": "No Data To Delete"
};
})
// something went wrong
.catch((error) => {
returnoptions.body = {
"Error": error
};
console.log("error in deleteing old data");
});
}
function getUniqueCities(items) {
const citiesOnly = items.map(item => item.city);
// Return an array with a list of unique cities
return [...new Set(citiesOnly)];
}
function buildOptions(uniqueList) {
return uniqueList.map(curr => {
return {value:curr, label:curr};
});
}
// filter results
export function buttonFilterResults_click(event) {
// city
const CurrentCity = $w('#dropdownFilterCity').value;
if (CurrentCity === '') // כל הארץ
{
wixData.query("oppositeSearch")
.find()
.then( (results) => {
$w("#repeater1").data = results.items;
})
.catch( (error) => {
console.log("error in buttonFilterResults_click");
console.log(error);
});
}
else
{
wixData.query("oppositeSearch")
.eq('city', CurrentCity)
.find()
.then( (results) => {
$w("#repeater1").data = results.items;
})
.catch( (error) => {
console.log("error in buttonFilterResults_click, else");
console.log(error);
});
}
}