Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs]: Is it possible to create a custom build; with custom defaults? #604

Closed
ceesvanegmond opened this issue Dec 1, 2023 · 2 comments
Closed
Labels
documentation Improvements or additions to documentation

Comments

@ceesvanegmond
Copy link

What is the improvement/update you wish to see?

Hi all,

I first have to say! What a great library! It's so diverse and usable. Thank you!
I do have one question. At the moment; we can use the run() method. The run method expects an config json and this also inits the consent.

My question; is it possible to set defaults before calling the run() method? Maybe already in the build (custom)?
We create lots of website and for the cookiebar it's 80% the same except the type of cookies and colors; so I want to see if we can somehow create a build with defaults that match our config; and override the config that's specific to the client.

Is this possible?

Link the related docs page, if it exists.

No response

@ceesvanegmond ceesvanegmond added the documentation Improvements or additions to documentation label Dec 1, 2023
@ceesvanegmond ceesvanegmond changed the title [Docs]: Is it possible to create a custom build; with other defaults? [Docs]: Is it possible to create a custom build; with custom defaults? Dec 1, 2023
@github-actions github-actions bot added the triage yet to be reviewed label Dec 1, 2023
@orestbida
Copy link
Owner

@ceesvanegmond you can do it, but you need to create your own wrapper and a proper merge function to merge the 2 configs. Here is a proof of concept using v3:

index.js

import * as CookieConsent from "vanilla-cookieconsent"
import defaultConfig from "./defaultConfig";

const mergeConfigs = (defaultConfig, userConfig) => {
    const mergedConfig = { ...defaultConfig };

    for (const key in userConfig) {
        if (userConfig.hasOwnProperty(key)) {
            if (typeof userConfig[key] === 'object' && userConfig[key] !== null && defaultConfig[key]) {
                mergedConfig[key] = mergeConfigs(defaultConfig[key], userConfig[key]);
            } else {
                mergedConfig[key] = userConfig[key];
            }
        }
    }

    return mergedConfig;
}

// Export your custom run method
export const run = async (userConfig = {}) => {
    await CookieConsent.run(mergeConfigs(defaultConfig, userConfig));
};

export const acceptService = CookieConsent.acceptCategory;
export const acceptedCategory = CookieConsent.acceptedCategory;
export const acceptedService = CookieConsent.acceptedService;
// TODO: export the other methods

@orestbida orestbida removed the triage yet to be reviewed label Dec 2, 2023
@ceesvanegmond
Copy link
Author

@orestbida Thanks! Works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants