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

feat(web): Generic List - Order by field in CMS #17319

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RunarVestmann
Copy link
Member

@RunarVestmann RunarVestmann commented Dec 20, 2024

Generic List - Order by field in CMS

What

  • Now we can change the default order of items in a generic list

Why

  • Some lists we want to order by title, others by date and so forth

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Formatting passes locally with my changes
  • I have rebased against main before asking for a review

Summary by CodeRabbit

  • New Features

    • Introduced a defaultOrder property for the GenericList component, allowing for customizable item ordering.
    • Enhanced the SliceMachine to support the new defaultOrder functionality in rendering slices.
    • Added sorting capabilities to the CmsElasticsearchService, enabling item retrieval based on various criteria.
  • Bug Fixes

    • Improved handling of creation dates in the GenericListItemSyncService by prioritizing the first published date.
  • Documentation

    • Updated fragments to include the new defaultOrder field in the GenericListFields.

Copy link
Contributor

coderabbitai bot commented Dec 20, 2024

Walkthrough

This pull request introduces a new feature for ordering generic list items across multiple components and services. The changes primarily focus on adding a defaultOrder property to the GenericListWrapperProps interface, updating GraphQL fragments, and implementing sorting logic in the Elasticsearch service. The modifications enable specifying a default ordering for list items, with support for sorting by date, title, or publish date. The changes are implemented consistently across frontend components, CMS models, and backend services.

Changes

File Change Summary
apps/web/components/GenericList/GenericList.tsx Added defaultOrder to GenericListWrapperProps, updated fetchListItems to include ordering
apps/web/components/Organization/Slice/SliceMachine.tsx Updated renderSlice to handle defaultOrder for GenericList
apps/web/screens/queries/fragments.ts Added defaultOrder field to GenericListFields fragment
apps/web/utils/richText.tsx Modified defaultRenderComponent to pass defaultOrder to GenericListWrapper
libs/cms/src/lib/cms.elasticsearch.service.ts Updated getListItems method to support sorting
libs/cms/src/lib/dto/getGenericListItems.input.ts Added GetGenericListItemsInputOrderBy enum
libs/cms/src/lib/models/genericList.model.ts Added defaultOrder field, created mapOrderBy function
libs/cms/src/lib/models/latestGenericListItems.model.ts Updated mapLatestGenericListItems to include orderBy

Sequence Diagram

sequenceDiagram
    participant Client
    participant GenericListWrapper
    participant GraphQLQuery
    participant ElasticsearchService

    Client->>GenericListWrapper: Render with defaultOrder
    GenericListWrapper->>GraphQLQuery: Fetch items with orderBy
    GraphQLQuery->>ElasticsearchService: Request sorted items
    ElasticsearchService-->>GraphQLQuery: Return sorted list items
    GraphQLQuery-->>GenericListWrapper: Provide ordered items
    GenericListWrapper->>Client: Render ordered list
Loading

Possibly related PRs

Suggested labels

automerge

Suggested reviewers

  • mannipje
  • Toti91
  • oddsson

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@RunarVestmann RunarVestmann added deploy-feature Deploys features to dev and removed deploy-feature Deploys features to dev labels Dec 20, 2024
Copy link
Contributor

Affected services are: air-discount-scheme-api,api,application-system-api,judicial-system-api,judicial-system-backend,services-search-indexer,services-user-notification,web,
Feature deployment of your services will begin shortly. Your feature will be accessible here:
https://web-generic-list-default-order-api-catalogue.dev01.devland.is/api
https://web-generic-list-default-order-application-callback-xrd.internal.dev01.devland.is/application-payment
https://web-generic-list-default-order-application-callback-xrd.internal.dev01.devland.is/applications
https://web-generic-list-default-order-application-payment-callback-xrd.internal.dev01.devland.is/application-payment
https://web-generic-list-default-order-application-payment-callback-xrd.internal.dev01.devland.is/applications
https://web-generic-list-default-order-beta.dev01.devland.is/
https://web-generic-list-default-order-beta.dev01.devland.is/api
https://web-generic-list-default-order-beta.dev01.devland.is/bff
https://web-generic-list-default-order-beta.dev01.devland.is/samradsgatt
https://web-generic-list-default-order-beta.dev01.devland.is/stjornbord/bff
https://web-generic-list-default-order-loftbru-cf.dev01.devland.is/
https://web-generic-list-default-order-loftbru-cf.dev01.devland.is/api/graphql
https://web-generic-list-default-order-loftbru.dev01.devland.is/
https://web-generic-list-default-order-loftbru.dev01.devland.is/api/graphql

Deployed services: application-system-api,api,web,air-discount-scheme-api,application-system-api-worker,consultation-portal,services-bff-portals-admin,services-bff-portals-my-pages,air-discount-scheme-web.
Excluded services: search-indexer-service,user-notification,user-notification-worker,user-notification-cleanup-worker

Copy link

codecov bot commented Dec 20, 2024

Codecov Report

Attention: Patch coverage is 25.53191% with 35 lines in your changes missing coverage. Please review.

Project coverage is 35.69%. Comparing base (9364323) to head (3e8a720).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...cms/src/lib/models/latestGenericListItems.model.ts 0.00% 15 Missing ⚠️
libs/cms/src/lib/cms.elasticsearch.service.ts 10.00% 9 Missing ⚠️
libs/cms/src/lib/models/genericList.model.ts 27.27% 8 Missing ⚠️
...rc/lib/search/importers/genericListItem.service.ts 0.00% 2 Missing ⚠️
libs/cms/src/lib/dto/getGenericListItems.input.ts 88.88% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #17319      +/-   ##
==========================================
- Coverage   35.69%   35.69%   -0.01%     
==========================================
  Files        6928     6928              
  Lines      148718   148767      +49     
  Branches    42495    42520      +25     
==========================================
+ Hits        53087    53098      +11     
- Misses      95631    95669      +38     
Flag Coverage Δ
api 3.33% <ø> (ø)
api-domains-auth-admin 48.49% <ø> (ø)
api-domains-communications 39.38% <25.53%> (-0.03%) ⬇️
application-system-api 38.73% <25.53%> (-0.02%) ⬇️
application-template-api-modules 27.67% <25.53%> (-0.02%) ⬇️
application-templates-inheritance-report 6.59% <ø> (ø)
application-ui-shell 22.28% <ø> (-0.04%) ⬇️
cms 0.39% <0.00%> (-0.01%) ⬇️
cms-translations 38.72% <25.53%> (-0.03%) ⬇️
judicial-system-api 20.20% <ø> (ø)
judicial-system-backend 55.93% <25.53%> (-0.04%) ⬇️
services-user-notification 46.49% <25.53%> (-0.04%) ⬇️
web 2.40% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
apps/web/components/GenericList/GenericList.tsx 0.00% <ø> (ø)
...web/components/Organization/Slice/SliceMachine.tsx 0.00% <ø> (ø)
apps/web/screens/queries/fragments.ts 0.00% <ø> (ø)
apps/web/utils/richText.tsx 0.00% <ø> (ø)
libs/cms/src/lib/dto/getGenericListItems.input.ts 70.37% <88.88%> (+7.21%) ⬆️
...rc/lib/search/importers/genericListItem.service.ts 14.63% <0.00%> (-0.76%) ⬇️
libs/cms/src/lib/models/genericList.model.ts 50.00% <27.27%> (-10.87%) ⬇️
libs/cms/src/lib/cms.elasticsearch.service.ts 3.18% <10.00%> (+0.29%) ⬆️
...cms/src/lib/models/latestGenericListItems.model.ts 31.91% <0.00%> (-2.97%) ⬇️

... and 4 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9364323...3e8a720. Read the comment docs.

@datadog-island-is
Copy link

datadog-island-is bot commented Dec 20, 2024

Datadog Report

All test runs b5ed288 🔗

10 Total Test Services: 0 Failed, 9 Passed
🔻 Test Sessions change in coverage: 4 decreased, 29 no change

Test Services
This report shows up to 10 services
Service Name Failed Known Flaky New Flaky Passed Skipped Total Time Code Coverage Change Test Service View
api 0 0 0 4 0 2.49s 1 no change Link
api-domains-auth-admin 0 0 0 18 0 10.16s 1 no change Link
api-domains-communications 0 0 0 5 0 30.62s 1 decreased (-0.02%) Link
api-domains-license-service 0 0 0 0 0 477.75ms 1 no change Link
application-system-api 0 0 0 46 0 2m 22.71s 1 no change Link
application-template-api-modules 0 0 0 118 0 2m 3.77s 1 no change Link
application-ui-shell 0 0 0 74 0 30.57s 1 no change Link
cms-translations 0 0 0 3 0 26.62s 1 decreased (-0.03%) Link
judicial-system-api 0 0 0 61 0 5.31s 1 no change Link
judicial-system-backend 0 0 0 21113 0 18m 17.22s 1 decreased (-0.03%) Link

🔻 Code Coverage Decreases vs Default Branch (4)

  • cms-translations - jest 48.15% (-0.03%) - Details
  • services-user-notification - jest 68.89% (-0.03%) - Details
  • judicial-system-backend - jest 59.59% (-0.03%) - Details
  • api-domains-communications - jest 48.51% (-0.02%) - Details

@RunarVestmann RunarVestmann added the deploy-feature Deploys features to dev label Dec 20, 2024
@RunarVestmann RunarVestmann marked this pull request as ready for review December 20, 2024 12:21
@RunarVestmann RunarVestmann requested review from a team as code owners December 20, 2024 12:21
@RunarVestmann RunarVestmann added the automerge Merge this PR as soon as all checks pass label Dec 20, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🔭 Outside diff range comments (1)
apps/web/components/Organization/Slice/SliceMachine.tsx (1)

Line range hint 1-300: Consider addressing @ts-ignore comments

The file contains multiple @ts-ignore comments that suppress TypeScript errors. These should be properly typed to maintain type safety.

Consider creating proper type definitions for:

  1. The slice parameter in the renderSlice function
  2. The slice.__typename checks in the SliceMachine component

Example approach:

type SliceWithTypename = Slice & {
  __typename: string;
};

export const renderSlice = (
  slice: SliceWithTypename,
  namespace: Record<string, string>,
  slug: string,
  params: Record<string, unknown>
) => {
  // ... rest of the code
}
🧹 Nitpick comments (2)
libs/cms/src/lib/dto/getGenericListItems.input.ts (1)

13-17: Enum addition for ordering.
Clearly enumerating sorting criteria as “Date”, “Title”, and “Publish Date” is beneficial for readability. Ensure your naming in the UI is intuitive to end-users.

apps/web/components/GenericList/GenericList.tsx (1)

582-582: Consider adding JSDoc documentation for the defaultOrder prop

To improve code maintainability and help other developers understand the purpose of this prop, consider adding JSDoc documentation.

 interface GenericListWrapperProps {
   id: string
   searchInputPlaceholder?: string | null
   itemType?: string | null
   filterTags?: GenericTag[] | null
+  /** Specifies the default ordering for the list items. When not provided, items will use the default backend ordering. */
   defaultOrder?: GetGenericListItemsInputOrderBy | null
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9364323 and 3e8a720.

⛔ Files ignored due to path filters (1)
  • libs/cms/src/lib/generated/contentfulTypes.d.ts is excluded by !**/generated/**
📒 Files selected for processing (9)
  • apps/web/components/GenericList/GenericList.tsx (3 hunks)
  • apps/web/components/Organization/Slice/SliceMachine.tsx (1 hunks)
  • apps/web/screens/queries/fragments.ts (1 hunks)
  • apps/web/utils/richText.tsx (1 hunks)
  • libs/cms/src/lib/cms.elasticsearch.service.ts (3 hunks)
  • libs/cms/src/lib/dto/getGenericListItems.input.ts (2 hunks)
  • libs/cms/src/lib/models/genericList.model.ts (3 hunks)
  • libs/cms/src/lib/models/latestGenericListItems.model.ts (1 hunks)
  • libs/cms/src/lib/search/importers/genericListItem.service.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (9)
libs/cms/src/lib/dto/getGenericListItems.input.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
apps/web/screens/queries/fragments.ts (1)

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/web/components/GenericList/GenericList.tsx (1)

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/web/utils/richText.tsx (1)

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
libs/cms/src/lib/search/importers/genericListItem.service.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/cms/src/lib/models/genericList.model.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/cms/src/lib/models/latestGenericListItems.model.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
apps/web/components/Organization/Slice/SliceMachine.tsx (1)

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
libs/cms/src/lib/cms.elasticsearch.service.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
🔇 Additional comments (20)
libs/cms/src/lib/cms.elasticsearch.service.ts (6)

42-45: Ensure maintainability of new imports.
It's great that the new enum and input type are imported here. For optimal discoverability, consider adding a brief doc comment or JSDoc remark explaining the purpose of these imports and how they enable sorting.


476-476: Good extension of method signature.
The optional “orderBy?” parameter reflects the new sorting logic. This approach is flexible and cleanly integrates the new feature with minimal code changes.


532-533: Sorting array initialization is sufficient.
Using an empty array to accumulate sorting rules is fine. No issues found here.


534-547: Robust default sort logic.
Your fallback condition uses descending release date and text-based sorting in a stable manner. Consider verifying that the default logic matches expectations in upstream usage (e.g., date-based sorting might cause confusion if users consider “publish date” different from “release date”).

Would you like a script to search the codebase for references to “releaseDate” or “publish date” to confirm consistent usage?


549-559: Title-based sort logic.
Sorting by title (ascending) is appropriate for alphabetical sorting. Looks correct and aligns well with the fallback to other fields if titles are identical.


561-571: Publish-date-based sort logic.
Sorting by dateCreated first, then fallback by title, then releaseDate provides a clear chronological structure. Ensure that calling code expects “publish date” to be captured by “dateCreated”.

Would you like a script to locate usage references of “publish date” or “dateCreated” to finalize consistency checks?

libs/cms/src/lib/dto/getGenericListItems.input.ts (4)

1-7: Proper usage of GraphQL decorators.
The imports from '@nestjs/graphql' are correct and well-structured. Good job keeping them consistent.


11-12: Caching configuration is appropriate.
The import of “CacheField” indicates an efficient approach to reduce repeated field lookups.


19-21: Enum registration with GraphQL.
Registering your enum helps keep your schema consistent. Good job making the name explicit.


55-56: New “orderBy” field is consistent.
Allowing it to be null or omitted is a flexible approach. This neatly maps onto your new sorting logic.

libs/cms/src/lib/models/genericList.model.ts (4)

6-6: New import aligns with sorting functionality.
Importing GetGenericListItemsInputOrderBy from the DTO is correct. This promotes cohesive usage of the enum across layers.


31-32: Optional “defaultOrder” field.
Defining “defaultOrder” at the model level and annotating it with CacheField is a good way to provide a default sorting at the data layer.


40-51: Mapping function is straightforward.
The map from a string value to your new enum is clear and easy to maintain. No issues detected.


62-62: Mapping “defaultOrder” usage.
You ensure that the model’s “defaultOrder” matches what is set in Contentful fields. This keeps future maintenance simple.

libs/cms/src/lib/models/latestGenericListItems.model.ts (2)

68-72: Concise variable extraction.
Extracting “genericList” before returning promotes readability. Nice improvement in code structure.


73-92: Propagating “defaultOrder” to “itemResponse”.
Including “orderBy: genericList?.defaultOrder” ensures alignment between the mapped data and the new sorting capabilities.

apps/web/components/Organization/Slice/SliceMachine.tsx (1)

206-206: LGTM! Proper prop passing for defaultOrder

The defaultOrder prop is correctly passed from the slice to the GenericListWrapper component.

apps/web/utils/richText.tsx (1)

278-278: LGTM! Consistent prop handling across components

The changes correctly:

  1. Add the defaultOrder prop to GenericList
  2. Explicitly type the variant prop as 'accordion' | 'card' in TeamList

These changes maintain consistency with the modifications in SliceMachine.tsx.

Also applies to: 283-286

apps/web/screens/queries/fragments.ts (1)

876-876: LGTM: Field addition to GraphQL fragment

The defaultOrder field is correctly added to the GenericListFields fragment.

apps/web/components/GenericList/GenericList.tsx (1)

33-33: LGTM: Implementation of defaultOrder

The changes correctly implement the default ordering functionality with proper TypeScript types and GraphQL integration.

Also applies to: 582-582, 590-590, 653-653

@RunarVestmann RunarVestmann added skip-nx-cache Skip NX's cache in a pull request and removed deploy-feature Deploys features to dev labels Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Merge this PR as soon as all checks pass skip-nx-cache Skip NX's cache in a pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants