-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@dpc-sdp/ripple-tide-landing-page): moved data driven components…
… feature into core
- Loading branch information
1 parent
1cbf987
commit 39f0cc3
Showing
13 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
examples/nuxt-app/layers/example-data-driven-component/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## About this layer | ||
|
||
An example of setting up a data driven component for testing purposes | ||
|
||
Data driven components can be registered in the Nuxt `app.config` file: | ||
|
||
```ts | ||
export default defineAppConfig({ | ||
ripple: { | ||
dataDrivenComponents: { | ||
// add key of field_data_driven_component and value of component name to render | ||
// eg: find_a_council_map: 'VicCouncilLookup' | ||
example_ddc: 'ExampleDDC' | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
Note that the vue component (e.g. ExampleDDC) must be declared globally for this to work, see https://nuxt.com/docs/guide/directory-structure/components. |
12 changes: 12 additions & 0 deletions
12
examples/nuxt-app/layers/example-data-driven-component/app.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default defineAppConfig({ | ||
ripple: { | ||
featureFlags: { | ||
contentCollectionSearchConnector: 'elasticsearch' | ||
}, | ||
dataDrivenComponents: { | ||
// add key of field_data_driven_component and value of component name to render | ||
// eg: find_a_council_map: 'VicCouncilLookup' | ||
example_ddc: 'ExampleDDC' | ||
} | ||
} | ||
}) |
15 changes: 15 additions & 0 deletions
15
examples/nuxt-app/layers/example-data-driven-component/components/global/ExampleDDC.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<template> | ||
<h3>{{ title }}</h3> | ||
<div v-html="description" /> | ||
<p>{{ testCustomProp }}</p> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
interface Props { | ||
title: string | ||
description: string | ||
testCustomProp: string | ||
} | ||
defineProps<Props>() | ||
</script> |
3 changes: 3 additions & 0 deletions
3
examples/nuxt-app/layers/example-data-driven-component/nuxt.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { defineNuxtConfig } from 'nuxt/config' | ||
|
||
export default defineNuxtConfig({}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/ripple-test-utils/step_definitions/components/data-driven-component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Then, DataTable } from '@badeball/cypress-cucumber-preprocessor' | ||
|
||
Then( | ||
'a custom data driven component with ID {string} should exist with title {string} and have the properties', | ||
(id: string, title: string, dataTable: DataTable) => { | ||
const data = dataTable.hashes()[0] | ||
|
||
cy.get(`[data-component-id="${id}"]`).as('component') | ||
cy.get('@component').should('exist') | ||
cy.get(`@component`).should( | ||
'have.attr', | ||
'data-component-type', | ||
'ExampleDDC' | ||
) | ||
cy.get('@component').within(() => { | ||
cy.get(`h3`).should('have.text', title) | ||
}) | ||
|
||
cy.get('@component').should('contain', data.description) | ||
cy.get('@component').should('contain', data.testCustomProp) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/ripple-tide-landing-page/components/global/TideLandingPage/DataDrivenCmpError.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<template> | ||
<DevOnly> | ||
<div class="rpl-callout callout-error"> | ||
<h3>Data driven component {{ component }} not found</h3> | ||
<p>Check the component mapping for {{ field }} in app.config</p> | ||
</div> | ||
</DevOnly> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
interface Props { | ||
component: string | ||
field: string | ||
} | ||
defineProps<Props>() | ||
</script> | ||
|
||
<style> | ||
.callout-error { | ||
--rpl-clr-accent: var(--rpl-clr-error); | ||
--rpl-clr-accent-alt: var(--rpl-clr-error-light); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...de-landing-page/mapping/components/data-driven-component/data-driven-component-mapping.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* eslint-disable no-prototype-builtins */ | ||
import type { TideDynamicPageComponent } from '@dpc-sdp/ripple-tide-api/types' | ||
import { logger } from '@dpc-sdp/ripple-tide-api' | ||
import { useAppConfig } from '#imports' | ||
|
||
export interface ITideDataDrivenComponent { | ||
component?: string | ||
description: string | ||
title: string | ||
field?: string | ||
data?: unknown | ||
} | ||
|
||
interface IDDCAppConfig { | ||
ripple?: { | ||
dataDrivenComponents?: Record<string, string> | ||
} | ||
} | ||
|
||
export const dataDrivenComponentMapping = ( | ||
field: any | ||
): TideDynamicPageComponent<ITideDataDrivenComponent> => { | ||
const getComponent = (field: any) => { | ||
const appConfig = useAppConfig() as IDDCAppConfig | ||
if ( | ||
appConfig?.ripple?.dataDrivenComponents?.hasOwnProperty( | ||
field.field_data_driven_component | ||
) | ||
) { | ||
return appConfig.ripple.dataDrivenComponents[ | ||
field.field_data_driven_component | ||
] | ||
} | ||
return 'TideLandingPageDataDrivenCmpError' | ||
} | ||
let dataProps = {} | ||
try { | ||
dataProps = JSON.parse(field.field_configuration) | ||
} catch (error) { | ||
logger.error(`Error parsing data driven component extra data`, error) | ||
} | ||
|
||
return { | ||
component: getComponent(field), | ||
id: `${field.drupal_internal__id}`, | ||
props: { | ||
title: field.field_paragraph_title, | ||
description: field.field_paragraph_body?.processed, | ||
field: field.field_data_driven_component, | ||
component: getComponent(field), | ||
...dataProps | ||
} | ||
} | ||
} | ||
|
||
export default { | ||
includes: [], | ||
mapping: dataDrivenComponentMapping, | ||
contentTypes: ['landing_page'] | ||
} |