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

Is it impossible to update view when I change nested object? #16

Open
remigailard80 opened this issue Mar 21, 2022 · 19 comments
Open

Is it impossible to update view when I change nested object? #16

remigailard80 opened this issue Mar 21, 2022 · 19 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@remigailard80
Copy link

remigailard80 commented Mar 21, 2022

https://www.kickstartds.com/storybook/?path=/story/base-header--default

in this example, when I change

"items": [
      {
        "label": "Sanity",
        "href": "/sanity",
        "id": "sanity"
      },
      {
        "label": "Integrations",
        "href": "/integrations",
        "id": "integrations"
      },
      {
        "label": "Blog",
        "href": "/blog",
        "id": "blog"
      }
    ]

to

"items": [
      {
        "label": "ABC TEST",
        "href": "/sanity",
        "id": "sanity"
      },
      {
        "label": "Integrations",
        "href": "/integrations",
        "id": "integrations"
      },
      {
        "label": "Blog",
        "href": "/blog",
        "id": "blog"
      }
    ]

it looks like there's not any changes.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/AdvertisementDataOverviewType",
  "definitions": {
    "AdvertisementOverviewViewType.Data.AsObject": {
      "type": "object",
      "properties": {
        "title": {
          "type": "string"
        },
        "subTitle": {
          "type": "string"
        },
        "price": {
          "type": "number"
        },
        "countsList": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Count.AsObject"
          }
        },
        "image": {
          "type": "string"
        },
        "targetUri": {
          "type": "string"
        },
        "publishedAt": {
          "type": "number"
        },
        "categoryId": {
          "type": "number"
        },
        "quickReportQuestion": {
          "$ref": "#/definitions/QuickReportQuestion.AsObject"
        },
        "userId": {
          "type": "number"
        },
        "meta": {
          "$ref": "#/definitions/AdvertisementOverviewViewType.Meta.AsObject"
        }
      },
      "required": [
        "title",
        "subTitle",
        "price",
        "countsList",
        "image",
        "targetUri",
        "publishedAt",
        "categoryId",
        "userId"
      ],
      "additionalProperties": false
    },
    "Count.AsObject": {
      "type": "object",
      "properties": {
        "type": {
          "$ref": "#/definitions/CountType"
        },
        "value": {
          "type": "number"
        },
        "image": {
          "type": "string"
        }
      },
      "required": [
        "type",
        "value",
        "image"
      ],
      "additionalProperties": false
    },
    "CountType": {
      "type": "number",
      "enum": [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7
      ]
    },
    "QuickReportQuestion.AsObject": {
      "type": "object",
      "properties": {
        "content": {
          "type": "string"
        },
        "reportItemId": {
          "type": "number"
        }
      },
      "required": [
        "content",
        "reportItemId"
      ],
      "additionalProperties": false
    },
    "AdvertisementOverviewViewType.Meta.AsObject": {
      "type": "object",
      "properties": {
        "advertisement": {
          "$ref": "#/definitions/Advertisement.AsObject"
        }
      },
      "additionalProperties": false
    },
    "Advertisement.AsObject": {
      "type": "object",
      "properties": {
        "id": {
          "type": "number"
        }
      },
      "required": [
        "id"
      ],
      "additionalProperties": false
    }
  }
}

generated schema, and It looks like every ref objects read as anything.

스크린샷 2022-03-22 오전 2 20 25

@remigailard80
Copy link
Author

oh, sorry

Prerequesite: Your components need to have JSON Schema files associated with them. Additionally schemas need to be dereferenced already, as $ref-resolution is not (a tested) part of this addon (yet? ... let us know in the issues if you need this).

Guess i have to use tools like https://github.com/APIDevTools/json-schema-ref-parser

@julrich
Copy link
Member

julrich commented Mar 21, 2022

Hey @remigailard80, exactly! That's the one we're using, too!

Not sure if this is immediately helpful to you, but this is what we're doing for our components:

/* eslint-disable import/no-dynamic-require */
const $RefParser = require('json-schema-ref-parser');
const merge = require('json-schema-merge-allof');
const { root } = require('./utils');

const { schemaResolver } = require(`${root}/scripts/schemaResolver`);

const schemaLoader = async (refParser, schemaPath) =>
  refParser.dereference(schemaPath, {
    resolve: {
      rm: {
        order: 1,
        ...schemaResolver,
      },
    },
  });

const dereference = async (schemaPath) => {
  const schema = await schemaLoader(new $RefParser(), schemaPath);
  return merge(schema, { ignoreAdditionalProperties: true });
};

module.exports = {
  dereference,
};

Let me know if this solves it for you!
And if you have any other feedback, feel free to ping me (just create issues)! Always looking for ways to improve on the addon!

@julrich julrich self-assigned this Mar 21, 2022
@julrich julrich added the question Further information is requested label Mar 21, 2022
@julrich
Copy link
Member

julrich commented Mar 21, 2022

After having another look at our own example, I'm pretty sure there's something broken about the Array-handling, too! Will have a look at that! It's updating neither the component, nor the controls / args correctly in that case!

Works for activeNavId for example:
grafik

@julrich julrich added the bug Something isn't working label Mar 21, 2022
@remigailard80
Copy link
Author

thank you for this nice stuff!

@remigailard80
Copy link
Author

Guess pack of @kickstartds/core/lib/storybook/helpers caused errors.

스크린샷 2022-03-23 오전 11 18 02

When I change values on Json editor and saw it on console, the result look above.

But when i change the part

r = (e) =>
  Object.entries(e).reduce(
    (e, [t, n]) => (
      ((e) => "[object Object]" === Object.prototype.toString.call(e))(n) ||
      Array.isArray(n)
        ? Object.entries(r(n)).forEach(([n, r]) => {
            e[`${t}.${n}`] = r;
          })
        : (e[t] = n),
      e
    ),
    {}
  );

to

r = (e) => {
    return Object.entries(e).reduce((e, [t, n]) => {
          // e: reducer, t: key, n : value
        return (
          ((e) => "[object Object]" === Object.prototype.toString.call(e))(n) ||
          Array.isArray(n)
            ? (e[t] = n)
            : (e[t] = n),
          e
        );
    }, {});
  };

it works fine.

@remigailard80
Copy link
Author

remigailard80 commented Mar 23, 2022

But strangely, when I reproduce same situation on
https://github.com/remigailard80/storybook-addon-jsonschema/blob/main/stories/test.stories.jsx
(in ~/storybook-addon-jsonschema/node_modules/@kickstartds/core/lib/storybook/helpers.js, manually edit helpers.js and run yarn install)

it didn't works fine, and on SchemaEditor.tsx, handleChange didn't works.

Although I'm not good at programming, I hope it helps a little bit.

@julrich
Copy link
Member

julrich commented Mar 23, 2022

Oh, it sure does! I wasn't actually aware that you were using the pack / unpack helpers, because these have some known gaps in them! The array not syncing back when being edited in the Monaco code editor is one such example, I think!

Do you use them for something specific? Because I think without those it probably should work!
If that doesn't solve the problem, let me know! I'll have a look at your repository then!

@remigailard80
Copy link
Author

remigailard80 commented Mar 23, 2022

I think I can organize the cases as follows.

On the above test case

adding test.stories.tsx to the cloned project of https://github.com/kickstartDS/storybook-addon-jsonschema

  1. not modify ~/storybook-addon-jsonschema/node_modules/@kickstartds/core/lib/storybook/helpers.js and
    wrapping the args by pack => changing in object reflected on view (works fine)
ex)
Test.args = pack({
    title: "맛있는 고기예요",
    subTitle: "역삼동",
    price: 39000,
    counts: [
      {
        type: 3,
        value: 10,
        image: "",
      },
      {
        type: 2,
        value: 0,
        image: "",
      },
      {
        type: 1,
        value: 6,
        image: "",
      },
    ],
    image:
      "https://d1rb42tpk2uaws.cloudfront.net/origin/article/202006/ED9D54F7744525B5B5463915CF10F143D3FC2F128CE78703CE9CB7820C0CEB0F.jpg?q=82&s=300x300&t=crop",
    targetUri: "~~~~~~",
    publishedAt: 156789222,
    categoryId: 44,
    quickReportQuestion: {
      content: "구인구직 게시글인가요?",
      reportItemId: 63,
    },
    userId: 33,
    meta: {
      advertisement: {
        id: 1,
      },
    },
  }); 

and other examples are wrapped by pack, too such as,

BoxHell.args = pack({
  height: "default",
  box: {
    enabled: true,
    horizontal: "left",
    vertical: "center",
    background: "light",
    headline: {
      level: "p",
      styleAs: "h2",
      align: "left",
      content: "Lorem Ipsum dolor",
      spaceAfter: "none",
      pageHeader: false
    },
    text: "Hic maxime sed eos non. Consequatur ut qui amet accusantium nesciunt.",
    link: {
      variant: "outline",
      fillAnimation: true,
    },
  },
  media: {
    mode: "image",
    image: {
      srcMobile: "https://picsum.photos/seed/kdsvisual/640/270",
      srcTablet: "https://picsum.photos/seed/kdsvisual/1280/540",
      srcDesktop: "https://picsum.photos/seed/kdsvisual/1920/810",
      indent: "none",
    },
  },
});
  1. not modify ~/storybook-addon-jsonschema/node_modules/@kickstartds/core/lib/storybook/helpers.js and
    not wrapping the args by pack => changing in object not reflected on view (not works)

  2. if modify ~/storybook-addon-jsonschema/node_modules/@kickstartds/core/lib/storybook/helpers.js to

r = (e) => {
    return Object.entries(e).reduce((e, [t, n]) => {
          // e: reducer, t: key, n : value
        return (
          ((e) => "[object Object]" === Object.prototype.toString.call(e))(n) ||
          Array.isArray(n)
            ? (e[t] = n)
            : (e[t] = n),
          e
        );
    }, {});
  };

not works neither wrapping case or not wrapping case.

In my project

my project means the project that uses @kickstartds/storybook-addon-jsonschema by yarn add @kickstartds/storybook-addon-jsonschema.

As you mentioned, wrapping the args by pack has no effect, so using pack method on my project is unnessessary, rather using pack caused bugs.

But changing ~/storybook-addon-jsonschema/node_modules/@kickstartds/core/lib/storybook/helpers.js to

r = (e) => {
    return Object.entries(e).reduce((e, [t, n]) => {
          // e: reducer, t: key, n : value
        return (
          ((e) => "[object Object]" === Object.prototype.toString.call(e))(n) ||
          Array.isArray(n)
            ? (e[t] = n)
            : (e[t] = n),
          e
        );
    }, {});
  };

has effects.

Conclusion : not wrapping the args by pack and modifying ~/storybook-addon-jsonschema/node_modules/@kickstartds/core/lib/storybook/helpers.js works.

@julrich
Copy link
Member

julrich commented Mar 23, 2022

Thanks a lot for the thorough analysis! Let me see if we can just change 3. That should work for you, then, right?

@remigailard80
Copy link
Author

Yes! sorry for late reply.

@julrich
Copy link
Member

julrich commented Mar 25, 2022

Okay, I'll see to make that fix :)

@kenisteward
Copy link

kenisteward commented Feb 21, 2024

@julrich WAs this ever fixed? I'm attempting to use this to format a storybook for a a complex dereferenced json schema based setup but whenever I try to change internal fields it doesn't end up working.

Doesn't work with simple objects or objects in arrays

@julrich
Copy link
Member

julrich commented Feb 21, 2024

Hey @kenisteward. I'll have to check in with a colleague of mine. But we did update this addon for Storybook 7 compatibility. Maybe we fixed something there!

@lmestel do you know? I'll have to re-read the issue convo myself, because it's been quite a while.

@julrich
Copy link
Member

julrich commented Feb 21, 2024

@kenisteward can you add which version you are currently on?

@kenisteward
Copy link

@julrich
image
Looks like version 2.0.1 was resolved as a part of the npm install I used to get the lib

This shows as the currently marked latest on npm registry as well.

@lmestel
Copy link
Contributor

lmestel commented Feb 27, 2024

Hi everyone 👋
we have decided to just remove the args transformation (pack and unpack) from the addon. This should solve your problems.
We are currently testing the changes with some of our storybooks. If you like, feel free to do the same with the canaray release commented in #24.
We have also updated the example storybook in this repo. The button story contains a minimal example without args transformation; the header story on the other hand shows the old behavior with pack and unpack.
We would appreciate your feedback.

@julrich
Copy link
Member

julrich commented Apr 10, 2024

Let us know if anything remains, here. Happy to help the transition, if needed! Will close this in the near future, otherwise!

@kenisteward
Copy link

kenisteward commented Apr 13, 2024 via email

@julrich
Copy link
Member

julrich commented Apr 13, 2024

@kenisteward no worries, and if there's something problematic... a new issue would probably be best anyways (could link this one, then).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants