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

Implement liveReload utils to simplify live reload implementations #168

Merged
merged 6 commits into from
Jun 19, 2024

Conversation

JiriLojda
Copy link
Member

Motivation

Which issue does this fix? Fixes #issue number

If no issue exists, what is the fix or new feature? Were there any reasons to fix/implement things that are not obvious?

Checklist

  • Code follows coding conventions held in this repo
  • Automated tests have been added
  • Tests are passing
  • Docs have been updated (if applicable)
  • Temporary settings (e.g. variables used during development and testing) have been reverted to defaults

How to test

If manual testing is required, what are the steps?

@JiriLojda JiriLojda requested a review from a team as a code owner June 6, 2024 17:56
const applyUpdateOnItemOptionallyAsync = <Elements extends IContentItemElements>(
item: IContentItem<Elements>,
update: InternalUpdateMessage,
resolveElementCodename: null | ((codename: string) => string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: you can use default value here, then you could use resolveElementCodename without || null on line 31 and line 49 won't be necessary.. if you find this harder to read, I understand

src/utils/liveReload.ts Show resolved Hide resolved
};
}
default:
throw new Error();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this error doesn't tell much :D

const shouldApplyOnThisItem =
item.system.codename === update.item.codename && item.system.language === update.variant.codename;

const resolveCodename = resolveElementCodename ?? ((c: string) => camelCasePropertyNameResolver('', c));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by default codenames are in snake_case, wouldn't it bebetter to have snakeCaseResolver as default?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SDK uses camelCase by default. When you don't configure it explicitly you will have camelCase codenames in item.elements keys in res.data of SDK responses.


return applyOnOptionallyAsync(
mergeOptionalAsyncs(
typedItemElement.linkedItems.map((i) => applyUpdateOnItemOptionallyAsync(i, update, resolveElementCodename))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for thinking if not already solved: what is the depth for linked items? what about the circular references?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very good point. I updated the code to handle item cycles properly coupled with a few tests.

IvanKiral
IvanKiral previously approved these changes Jun 17, 2024
README.md Outdated

```ts
import KontentSmartLink, { KontentSmartLinkEvent } from '@kontent-ai/smart-link';
import KontentSmartLink, { KontentSmartLinkEvent, applyUpdateOnItem, applyUpdateOnItemAndLoadLinkedItems } from '@kontent-ai/smart-link';

// Initialize the SDK
const sdk = KontentSmartLink.initialize({ ... });

// Listen for updates and apply them to your application
sdk.on(KontentSmartLink.Update, (data: IUpdateMessageData) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KontentSmartLinkEvent

// Use this data to update your application state or UI as needed e.g.:
setItems((items) => items.map(item => applyUpdateOnItem(item, data)));
// or
Promise.all(items.map(item => applyUpdateOnItemAndLoadLinkedItems(item, data, fetchItemsFromDeliveryApi)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we want to add below?
I know its up to anybody how he want to get the items, but this example would save me 2 or 3 minutes till i figured out how to do it :D

const fetchItemsFromDeliveryApi = (items) => client.items().inFilter(system.codename, items).toAllPromise().then(res => res.data.items)

@JiriLojda JiriLojda merged commit 8c682e9 into master Jun 19, 2024
5 checks passed
@JiriLojda JiriLojda deleted the live_reload_utils branch June 19, 2024 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants