Jira Cloud development and problem with Cookies

Hi everyone.

I am developing a plugin that acts as a Dashboard Item.
The plugin is built in React. I have a cookie need and I decided to use the library:

Unfortunately, after saving the cookie, the value (undefined) could not be read. Locally - when I test the content (from another iframe server) it works fine. I tried to set the cookie domain parameter to the JIRA Cloud server domain but it didn’t work.

Perhaps it was wise to use AP.cookie
https://developer.atlassian.com/cloud/jira/platform/jsapi/cookie/
but unfortunately AP.cookie.read uses callback (instead of promise) - which is problematic in my case.

Could someone advise something on this?

Regards
Mik

Hi @Mik ,

Are you able to wrap AP.cookie.read as follows:

  readCookie = (name) => {
    return new Promise((resolve, reject) => {
      AP.cookie.read(name, (value) => {
        resolve(value);
      });
    });
  }

Regards,
Dugald

3 Likes

Hi @dmorrow Thank you - I will try use it :slight_smile:
Regards
Mik

1 Like

Let me know how you go. :slight_smile:

Hi @dmorrow

My functions looks like this:

export const saveCookie = (name: string, value: string, daysUntilExpiry: number) => {
    if (!daysUntilExpiry) daysUntilExpiry = 365;
    AP.cookie.save(name, value, daysUntilExpiry);
}

export const readCookieAsPromise = (name: string) => {
    return new Promise((resolve, reject) => {
        AP.cookie.read(name, (value: string) => {
            resolve(value);
            reject(value);
        });
    });
}

but If I’m trying

useEffect(() => {

            saveCookie('TestCookieAsPromise', 'MikTesting4', 1)
            let promisedCookieValue = readCookieAsPromise('CookieAsPromiseVALUE')
            Promise.all([promisedCookieValue]).then((values) => {
                console.log(values);
            });
    },

Plugin will not start.

image

Strange…

Regards
Mik

Hi again @dmorrow

Please ignore last message. It was another reason.

I can confirm that your function working fine!

Thank you for help.

Best regards
Mik

1 Like

Thanks for the confirmation. :slight_smile:

1 Like