Subtract a number and store in the base and then direct it to the link button

Hello friends

I am trying that when the user clicks on an employee (button532) it will subtract 1 credit and then direct them to the user’s link.

The problem is that my code is not working properly.
Thanks a lot!

$w.onReady(function () {
    $w('#button532').onClick(() => {

        let creditosItem = $w("#dynamicDataset").getCurrentItem();
        let creditos = creditosItem.creditos - 1;
        $w("#dynamicDataset").setFieldValue("creditos", creditos);
        $w("#dynamicDataset").save();

        $w("#repeater2").onItemReady(($item, itemData, index) => {
            wixLocation.to(`${itemData.linkempleado}`);

        });

    });
});

Where do you store the CREDITS ?

  1. In your own created DATABASE ?
  2. Somewhere else?

How do you imagine your CREDIT-SYSTEM to work?

  1. User can bye CREDITS?
  2. User uses SERVICE or do a DOWNLOAD and loose CREDITS?

Elaborate a little bit more.

There are surely a lot of different possibilities!

NOTE:
Never use element-IDs like ----> “#button532”!!!

What is the better elemet-ID ???
For example:

$w("#btnSubmit")...
$w("#btnGo")...

//or in your language -->

$w("#btnVer")...

!!! Always try to keep your code LOGICAL/SYSTEMATICAL and CLEAN !!!

  • btn → Button

  • rep —> Repeater

  • box -->Container

  • and so on…

  • The first 3-letters should always represent the ELEMENT, following by name-definition.

  • Why 3-letter best?

  • short shortcut

  • but also enough letters for clean definition to find back the ID in any SEARCH-system.

Also try to use more CONSOLE-LOGs!!!

let creditosItem = $w("#dynamicDataset").getCurrentItem(); 
console.log(creditosItem);

Hello Ninja! thanks for teaching me that! I’m going to improve!

When a company generates a search ad, they are also assigned credits within the same base (empleos) in the “creditos” column.

When the company wants to see an applicant, when clicking on the employee, 1 is subtracted from that “creditos” column.

That is, the user consumes credits every time he sees an employee.

When the count reaches 0 elsewhere in the code,
we show him a sign that he has run out of credits and we remind him to buy more.

Yes i understand.
I just wanted to know, if your CREDIT-SYSTEM is maden by your own (CUSTOM-MODE), or if your Credit-System is connected to an APP or any other predefined stuff out of the Wix-Box? → (Which could make things complicated)!

But as i can see, you create this system by your own → GOOD!

By the way, here one of many situations —> WHY YOU ALWAYS SHOULD KEEP YOUR CODE → LOGICAL + CLEAN + SHORT…

Read this post and you will understand!

Hi ! We perform this code, it works slowly but it works




export function btnver_click(event) {

    $w('#repeater2').hide();

    let creditosItem = $w("#dynamicDataset").getCurrentItem();
    console.log("Current-Item: ", creditosItem);
    let creditos = creditosItem.creditos - 1;

    let currentItem = 0

    let $item = $w.at(event.context);
    let itemData = $item("#dataset2").getCurrentItem();

    $w("#dynamicDataset").setFieldValue("creditos", creditos);
    $w("#dynamicDataset").setFieldValue("notificaciones", currentItem);
    $w("#dynamicDataset").save();

    console.log(itemData);
    wixLocation.to(itemData['linkempleado']);
    

}

I took a look onto your CODE…

This is how i would generate your futured functions…

$w.onReady(() => {console.log("Page is ready...");
    $w("#dynamicDataset").onReady(()=> {
        $w("#dataset2").onReady(()=> {console.log("DATASET-2 ready...");
            //data-filtration...
            let filter = wixData.filter();
                //filter = filter.isNotEmpty("creditos")
            $w("#dataset2").setFilter(filter)
            .then(()=> {console.log("Dataset is now filtered");})
            .catch( (err) => {console.log(err);});

            $w("#btnver").onClick((event) => {console.log("Ver Clicked");  
                //$w('#repeater2').hide();

                let $item = $w.at(event.context);
                let clickedItemData = $item("#dataset2").getCurrentItem(); console.log(clickedItemData);
                let index =  $item("#dataset2").getCurrentItemIndex(); console.log("Item-Index: ", index);

                //----Dynamic-Data---
                
                let dynamicEmpleosItem = $w("#dynamicDataset").getCurrentItem(); console.log("Dynamic-Empleos-Item: ", dynamicEmpleosItem);
                let title = dynamicEmpleosItem.title; console.log("Title: ", title);
                let empleoID = dynamicEmpleosItem.empleoID; console.log("Empleo-ID: ", empleoID);
                let subareaDeEmpleo = dynamicEmpleosItem.subareaDeEmpleo; console.log("Subarea-Empleo: ", subareaDeEmpleo);
                
                //---------description----
                
                let shortDescription = dynamicEmpleosItem.detalles.substr(0, 20); console.log("Empleos-Short-Description: ", shortDescription);
                $item("#text364").text = shortDescription + "  ";

                // ----Creditos Substraction-Process-
                let creditos = dynamicEmpleosItem.creditos; console.log("CREDITOS: ", creditos);
                creditos--;
                $w("#dynamicDataset").setFieldValue("creditos", creditos);
                $w("#dynamicDataset").save();   

                setTimeout(() => {wixLocation.to(dynamicEmpleosItem['linkempleado']);}, 1000);                  
            });
        });
    });
});
  1. Never use more then —> 1-$w.onReady()-command <----
  2. Always when working with datasets - → DATASET first must be ready!
  3. Using a lot of console-logs → will help you to understand your own code!
  4. Making good variable-definitions, keeps your code readable and clear.

Not everything is completely ready and working.
You will have to complete your coding.

EDIT:
Forgot to mention, that you need at least a → 0 ← VALUE inside your CREDITS-DB-FIELD, else you will get errors.

You can also solve this problem by code, generating a function, which checks first if DB-FIELD already has a value or not → if not → insert a → 0 → then start CALCULATION, or something like that.

Hey @CODE-NINJA <3

I’m doing a credit system too, and I know how to do it with my own database and code for USING the credits.

But!

I have trouble finding the right way to set up PAYING to get credits.

Like how do I make a system where the user can choose how many credits they want – let’s say 1 credit costs 1 USD.

How do I make it such that they choose the number of credits → Paying portal or checkout must SHOW that too right? → THEN (after payment) it is added to my custom CMS.

How can I make payments involved like this?

Wix checkout seems a little complicated.

I think maybe the new Wix Forms is the best solution?

But how do I make sure the credits are ONLY added after the payment.

All the best to you, dear ninja.

(You still never sent me a link to be able to donate to you!)

X

Hello my old fried. At moment i am out of Wix → working on other platforms and also with other coding environments (for example PYTHON).

But for you, the following could be interessting → On Payment Update | Velo

Read the following aswell…

  1. About Wix Payments | Help Center | Wix.com
  2. Create Payment | Velo
  3. Wix Payments API Retuning "Undefined" As Payment Status

Best regards and maybe i will get more ACTIVE again on WIX in future. See you soon again (maybe). :slight_smile:

1 Like