top of page
Writer's pictureUniversal Web

COMO AGREGAR SISTEMA DE FAVORITOS | GRATIS | WIX

Hola Gente! Les traigo un super vídeo, yo sé que les va a servir mucho! Cómo agregar un sistema de favoritos a sus sitios en Wix, totalmente gratis. Suscríbanse y dejen su like





CODE

Books Page

// Velo API Reference: https://www.wix.com/velo/reference/api-overview/introduction
import { currentMember } from "wix-members-frontend";
import wixData from "wix-data";

$w.onReady(async function () {

    const fullMember = await getMemberId();
    const id = fullMember._id;
    console.log(fullMember);
    const response = await searchId(id);

    console.log("Response", response);

    $w("#repeater8").onItemReady(async ($item, itemData, index) => {

        if (response) {
            const referenced = await isBookReferenced(response._id, itemData._id)
            if (referenced) {
                $item("#added").show();
                $item("#addFavorite").hide();
            }
        }

        $item("#addFavorite").onClick(async () => {
            if (!response) {
                await saveFavorite(id, itemData._id);
            } else {
                const inserted = await insertReferenceBook(response._id, itemData._id)
                console.log("Inserted ", inserted);
				$item("#added").show();
                $item("#addFavorite").hide();
            }
        })
        $item("#added").onClick(async () => {
            const removed = await removeReferenceBook(response._id, itemData._id);
            console.log("Removed ", removed);
            $item("#added").hide();
            $item("#addFavorite").show();
        })
    });

});

async function isBookReferenced(idRow, idBook) {
    return await wixData
        .isReferenced("favorites", "books", idRow, idBook)
        .then((result) => {
            let isReferenced = result; // true
            return isReferenced;
        })
        .catch((err) => {
            let errorMsg = err;
            return false;
        });
}
async function insertReferenceBook(idRow, idBook) {
    return await wixData
        .insertReference("favorites", "books", idRow, idBook)
        .then(() => {
            console.log("Reference inserted", idBook);
            return true;
        })
        .catch((error) => {
            console.log(error);
            return false;
        });
}
async function removeReferenceBook(idRow, idBook) {
    return await wixData
        .removeReference("favorites", "books", idRow, idBook)
        .then(() => {
            console.log("Reference removed");
        })
        .catch((error) => {
            console.log(error);
        });
}
async function saveFavorite(idUser, idBook) {
    let toSave = {
        idUser: idUser
    };

    return await wixData
        .save("favorites", toSave)
        .then(async (results) => {
            console.log("Saved", results); //see item below
            const inserted = await insertReferenceBook(results._id, idBook);
            console.log("Inserted ? ", inserted);
        })
        .catch((err) => {
            console.log(err);
        });
}
async function searchId(id) {
    return await wixData
        .query("favorites")
        .eq("idUser", id)
        .find()
        .then((results) => {
            if (results.items.length > 0) {
                return results.items[0]
            } else {
                return false;
            }
        })
        .catch((err) => {
            console.log(err);
            return false;
        });
}
const getMemberId = async () => {
    const options = {
        fieldsets: ['FULL']
    }

    return await currentMember
        .getMember(options)
        .then((member) => {
            return member;
        })
        .catch((error) => {
            console.error(error);
            return null;
        });
}

Favorites Page Member

// Velo API Reference: https://www.wix.com/velo/reference/api-overview/introduction
import wixData from "wix-data";
import { currentMember } from "wix-members-frontend";

$w.onReady(async function () {
	const fullMember = await getMemberId();
	const id = fullMember._id;
	const favoriteInfo = await getBooks(id);

	console.log(favoriteInfo);

	$w("#repeater8").data = favoriteInfo.books;
	$w("#repeater8").onItemReady(($item, itemData, index)=>{
		$item("#text13").text = itemData.title;
		$item("#text14").text = itemData.autor;
	})
});

async function getBooks(id) {
    return await wixData
        .query("favorites")
        .eq("idUser", id)
        .include("books")
        .find()
        .then((results) => {
            let firstItem = results.items[0];
            return firstItem;
        });
}

const getMemberId = async () => {
    const options = {
        fieldsets: ['FULL']
    }

    return await currentMember
        .getMember(options)
        .then((member) => {
            return member;
        })
        .catch((error) => {
            console.error(error);
            return null;
        });
}

Gracias por ver el vídeo!



 

Comments


bottom of page