Unofficial - User Group Status Retrieval in Softr.io

Hello, softr.io community!

Following my initial post regarding the retrieval of a user’s group status using a custom script, I’ve come up with a creative workaround that I thought might benefit others. Here’s a brief overview:

Instead of trying to directly retrieve user group information via the Softr Studio GUI (which doesn’t seem to offer a direct way to view this on the User Group page), I decided to implement a workaround using ‘Simple text’ blocks. Each block is assigned a unique name, prefixed by ‘grouptracker-’ followed by a number corresponding to my user group. All my user groups were initially organized by a number followed by the group name.

The script then checks if a block with an ID starting with ‘grouptracker-’ is displayed. It then consults a correspondence table and sends the relevant data to a webhook, which in turn updates my user data in Airtable. The script only triggers an update if there’s a change in the user’s group.

<script>
    document.addEventListener('DOMContentLoaded', (event) => {
        // Check if the user is logged in
        if (window.logged_in_user && window.logged_in_user['record_id']) {
            
            // Improved correspondence table
            const correspondenceTable = {
                1: 'Group_A',
                4: 'Group_B',
                7: 'Group_C',
                10: 'Group_D',
                13: 'Group_E',
                16: 'Group_F',
                19: 'Group_G',
                22: 'Group_H',
                25: 'Group_I',
                28: 'Group_J'
            };

            // Use an attribute selector to get all elements whose ID starts with "grouptracker-"
            const matchedElements = document.querySelectorAll('[id^="grouptracker-"]');

            // Use a regular expression to extract the number
            const regex = /grouptracker-(\d+)/;

            // Loop through the matching elements and compare with the correspondence table
            matchedElements.forEach(matchedElement => {
                const match = matchedElement.id.match(regex);
                if (match) {
                    const number = match[1];
                    const group = correspondenceTable[number] || "Unknown";
                    const numero_groupe = `${number}_${group}`;
                    
                    // Check if window.logged_in_user['User_group'] is different from numero_groupe
                    if (window.logged_in_user['User_group'] !== numero_groupe) {
                        const dataToSend = {
                            numero: number,
                            groupe: group,
                            numero_groupe: numero_groupe,
                            user_record_id: window.logged_in_user['record_id']
                        };
                        
                        // Send the data to the webhook
                        fetch('https://webhook-url-placeholder.com', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            body: JSON.stringify(dataToSend)
                        });
                    }
                }
            });
        }
    });
</script>

I hope this approach helps others looking for a similar solution. Please note that the provided script is a generic representation, and you might need to modify it according to your specific use case.

Thank you and happy building!