Keep switch on until switched back by member

Hi. I want to have a switch that stays on when clicked until the member switches it back off, even if they reload or leave the page. Is there any way someone can help me with this if possible? Thanks

1 Like

Hello acumenpay,

you can store the switch-status in a database (boolean-value) TRUE/FALSE.
Then you just ask the status of the switch from the database, for each user.

Thanks, that makes more sense but I’m still having a little trouble. Could you just explain how I could do that?

Do you have 1-switch for all member?
Or do you wanna have 1-switch for each member?

I want each member to have an individual switch that is separate from other members’ switches

Look here…
https://russian-dima.wixsite.com/meinewebsite/switch-safe-function

In this example you have the whole ā€œREAD-OUTā€ process.

import wixData from 'wix-data';

$w.onReady(function () {
    checkStatus()
});

function checkStatus (parameter) {console.log("check started")
    wixData.query("Database4")
    .find()
    .then( (results) => {
 if(results.items.length > 0) {console.log("GO")

 let firstItem = results.items[0]; //see item below
 let currentItem = results.items[currentItemIndex]
 let currentItemIndex = $w('#dataset1').getCurrentItemIndex()
 let myBoolean=results.items[currentItemIndex].btnStatus

 if (myBoolean===true) {$w('#switch1').checked=true} else {$w('#switch1').checked=false}
        console.log(currentItemIndex)
        console.log(myBoolean)
        } else {
            console.log("Nothing found.")
        }
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );
}

export function button1_click(event) {checkStatus()}
export function button2_click(event) {checkStatus()}
export function switch1_dblClick(event) {checkStatus()}

You will need following elements to reconstruct it…

  1. Database (mine is called ā€œDatabase4ā€ wit 2 colums in my example [title & BTN-Status]
  2. Dataset called ā€œdataset1ā€ (read-only in this example, but should be read&write at least)
  3. 1x switchbutton (ā€œswitch1ā€)
  4. 2x normal buttons (ā€œbutton1ā€ & ā€œbutton2ā€)
  5. 3x text (one of them connected to database (ā€œtitleā€)


PART-1 is done!

Now it’s your turn, to complete the write-action into the database.:smirk:

Vielen Dank. Das hat viel geholfen.

If it was helpful, don’t forget to give a like :wink:

Thanks, but I’m still a little confused. I know you’re not here to write the code for me but any info at all helps a ton. Could I do something where it inserts the input into a database and then from there it can be saved to memorize which account has set the switch to the on or off position? Like this:

import wixData from ā€˜wix-data’ ;

export function button24_onChange(event, $w) {
let toInsert = {
ā€œLockā€ : ā€œYesā€ ,
};

wixData.insert( ā€œLockAccountā€ , toInsert)
.then( (results) => {
let item = results;
} )
. catch ( (err) => {
let errorMsg = err;
} );

(I’m trying to create a Lock Account feature btw)

You forgot to add the useId to the toInsert object.
also I’d prefer that you’ll use the userId as the value of the _id field and do something like:

import wixData from'wix-data';
import wixUsers from'wix-users';
let user = wixUsers.currentUser;
let userId = user.id;
$w.onReady(() => {
if(!userLoggedIn){
$w('#switch1').disable();
} else {
wixData.get('LockAccount', userId)
.then(r => {
if(!r){
$w('#switch1').checked = true;//the default if it's a new member.
//show the current status to the 
} else {
r.lock ? $w('#switch1').checked = false : $w('#switch1').checked = true;
}
$w('#switch1').enable();
$w('#switch1').onChange(event => {
let toUpdate = {
_id: userId,
lock: $w('#switch1').checked
}
wixData.save("LockAccount", toUpdate)
.catch(err => err );
})
}
})
}
})

I wrote it fast. can’t promise you I don’t have a syntax error somewhere but you got the point.

Ok, i do not know if you already understand the coding of J.D. (for beginner it could be very difficult to understand)
My solution in this case is a little bit another. I prefered to use the dataset.
Look again here… (UPDATED-Version)
https://russian-dima.wixsite.com/meinewebsite/switch-safe-function

import wixData from 'wix-data';

$w.onReady(function () {
    checkStatus()
});

function checkStatus (parameter) {console.log("check started")
    wixData.query("Database4")
    .find()
    .then( (results) => {
 if(results.items.length > 0) {console.log("GO")

 let firstItem = results.items[0]; //see item below
 let currentItemIndex = $w('#dataset1').getCurrentItemIndex()
 let currentItem = results.items[currentItemIndex]
 let myBoolean=results.items[currentItemIndex].btnStatus

 if (myBoolean===true) {$w('#switch1').checked=true} else {$w('#switch1').checked=false}
        console.log(currentItemIndex)
        console.log(myBoolean)
 
        } else {
            console.log("Nothing found.")
        }
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );
}

export function button1_click(event) {checkStatus()}
export function button2_click(event) {checkStatus()}
export function switch1_dblClick(event) {checkStatus()}
export function button3_click(event) {WriteData()}

function WriteData (parameter) {
    $w("#dataset1").onReady( () => {
 let currentItemIndex = $w('#dataset1').getCurrentItemIndex()
        console.log(currentItemIndex)
        console.log($w('#switch1').checked===true)
 if ($w('#switch1').checked===true){$w("#dataset1").setFieldValue("btnStatus", true);}
 else {$w("#dataset1").setFieldValue("btnStatus", false);}
        $w("#dataset1").save();
    })
}

Now the function —> SAVE is active.

I think, now you have enoug stuff, where you can work with and learn :grin: