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

[POC] Analytics context #1354

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 135 additions & 63 deletions packages/nuxt-ripple-analytics/lib/index.ts

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions packages/nuxt-ripple/components/TestContext.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script setup lang="ts">
import { useEventContext } from '@dpc-sdp/ripple-ui-core'

interface ITestContextProps {
context: any
}

const props = withDefaults(defineProps<ITestContextProps>(), {})

const { context } = useEventContext(props.context)
</script>

<template>
<div class="testing123">
{{ context }}
<slot />
</div>
</template>

<style scoped>
.testing123 {
padding: 20px;
margin: 10px 0;
border: 1px solid;
background-color: rgba(0,0,0,0.05);
}
</style>
27 changes: 27 additions & 0 deletions packages/nuxt-ripple/components/TestContextControl.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script setup lang="ts">
import { useEventContext } from '@dpc-sdp/ripple-ui-core'

interface ITestContextProps {
objKey: any
on: any
off: any
}

const props = withDefaults(defineProps<ITestContextProps>(), {})

const { context, updateContext } = useEventContext()

Check warning on line 12 in packages/nuxt-ripple/components/TestContextControl.vue

View workflow job for this annotation

GitHub Actions / Test

'context' is assigned a value but never used. Allowed unused vars must match /props/u
</script>

<template>
<div>
Change <b>{{ objKey }}</b>
<button class="rpl-button" @click="updateContext(objKey, on)">
{{ on }}
</button>
<button class="rpl-button" @click="updateContext(objKey, off)">
{{ off }}
</button>
</div>
</template>

<style scoped></style>
26 changes: 26 additions & 0 deletions packages/nuxt-ripple/components/TestContextViewer.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script setup lang="ts">
import { useEventContext } from '@dpc-sdp/ripple-ui-core'

interface ITestContextProps {
}

const props = withDefaults(defineProps<ITestContextProps>(), {})

const { context } = useEventContext()
</script>

<template>
<div class="testing123">
{{ context }}
</div>
</template>

<style scoped>
.testing123 {
padding: 20px;
margin: 10px 0;
border: 1px solid;
background-color: rgba(0,0,0,0.05);
font-size: 16px;
}
</style>
34 changes: 34 additions & 0 deletions packages/nuxt-ripple/components/TestFireEvent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script setup lang="ts">
import { useRippleEvent } from '@dpc-sdp/ripple-ui-core'
interface ITestContextProps {
name: any
}

const emit = defineEmits<{}>()

const props = withDefaults(defineProps<ITestContextProps>(), {})

const { emitRplEvent } = useRippleEvent('testing', emit)

const fire = () => {
emitRplEvent(
'testFire',
{
action: 'search'
},
{ global: true }
)

nextTick()
}
</script>

<template>
<div>
<button class="rpl-button" @click="fire">
Fire "testing/testFire" event
</button>
</div>
</template>

<style scoped></style>
38 changes: 38 additions & 0 deletions packages/nuxt-ripple/components/TideBaseLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,44 @@
</slot>
</template>
<template #body="{ hasSidebar }">
<div class="rpl-type-p">
<TestContext :context="{ wow: 'cool' }">
<TestFireEvent name="testFire" />

<TestContextControl objKey="wow" on="apple" off="orange" />
<TestContext :context="{ ooh: 'amazing' }">
<TestContext :context="{ ooh: 'something' }">
<TestContextControl objKey="ooh" on="cat" off="dog" />
<TestContextControl objKey="wow" on="nested" off="blah" />
<TestContextControl objKey="new" on="123" off="456" />
<TestFireEvent name="testFire" />
</TestContext>
</TestContext>
<TestContext :context="{ ooh: 'blah' }"
><TestContext :context="{ ahh: 'neat' }"><TestFireEvent name="testFire" /></TestContext


><TestContextControl objKey="ooh" on="dolphin" off="whale" /></TestContext>
</TestContext>
<TestContext :context="{ wow: 'cool' }">
<TestFireEvent name="testFire" />

<TestContextControl objKey="wow" on="apple" off="orange" />
<TestContext :context="{ ooh: 'amazing' }">
<TestContext :context="{ ooh: 'something' }">
<TestContextControl objKey="ooh" on="cat" off="dog" />
<TestContextControl objKey="wow" on="nested" off="blah" />
<TestContextControl objKey="new" on="123" off="456" />
<TestFireEvent name="testFire" />
</TestContext>
</TestContext>
<TestContext :context="{ ooh: 'blah' }"
><TestContext :context="{ ahh: 'neat' }"><TestFireEvent name="testFire" /></TestContext


><TestContextControl objKey="ooh" on="dolphin" off="whale" /></TestContext>
</TestContext>
</div>
<slot name="body" :hasSidebar="hasSidebar"></slot>
<TideTopicTags
v-if="!featureFlags?.disableTopicTags && topicTags.length"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script setup lang="ts">
import { useEventContext } from '@dpc-sdp/ripple-ui-core'

import { FormKitSchemaNode } from '@formkit/core'
import { computed, nextTick, ref, watch } from 'vue'
import type { MappedCaptchaConfig } from '@dpc-sdp/ripple-tide-webform/types'
Expand Down Expand Up @@ -87,9 +89,12 @@ customInputs.library = (node: any) => {
}
})
}

const { context } = useEventContext({form_name: props.title, form_id: props.formId })
</script>

<template>
{{ context }}
<div
:style="{
'--local-max-width': '595px'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import type {
import { useRippleEvent } from '@dpc-sdp/ripple-ui-core'
import type { rplEventPayload } from '@dpc-sdp/ripple-ui-core'
import { get } from 'lodash-es'
import { useEventContext } from '@dpc-sdp/ripple-ui-core'


interface Props {
id: string
Expand Down Expand Up @@ -548,10 +550,17 @@ const locationOrGeolocation = computed(() => {
? userGeolocation.value
: locationQuery.value
})

const { updateContext } = useEventContext({ mode: 'asdasd'})

watch(activeTab, (newActiveTab) => {
updateContext('mode', newActiveTab)
})
</script>

<template>
<div class="rpl-u-margin-t-8">
<TestContextViewer />
<div
v-if="!searchListingConfig?.hideSearchForm"
:class="{
Expand Down
48 changes: 48 additions & 0 deletions packages/ripple-ui-core/src/composables/useEventContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { computed, provide, inject, ref } from 'vue'

export default (addedContext?) => {
const { context: parentContext, updateContext: updateParentContext } =
inject<any>('rplEventContext', {})

if (!addedContext) {
return {
context: parentContext,
updateContext: updateParentContext
}
}

const context = ref(addedContext || {})

function updateContext(key, value) {


if (typeof updateParentContext === 'function') {
updateParentContext(key, value)
}
if (context.value?.hasOwnProperty(key)) {
context.value = {
...context.value,
[key]: value
}
}

}

const mergedContext = computed(() => {

return {
...(parentContext?.value || {}),
...(context.value || {})
}
})

provide('rplEventContext', {
context: mergedContext,
updateContext
})

return {
context: mergedContext,
updateContext
}
}
7 changes: 3 additions & 4 deletions packages/ripple-ui-core/src/composables/useRippleEvent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { inject } from 'vue'
import rplEventBus from './../lib/eventbus'
import useEventContext from '../composables/useEventContext'

export type rplEventPayload = {
id?: string
Expand All @@ -24,16 +25,14 @@ export type rplEventOptions = {

export function useRippleEvent(namespace: string, emit?: any) {
const $rplEvent: typeof rplEventBus | undefined = inject('$rplEvent')
const eventContext: rplEventPayload = inject('eventContext', {})
const { context } = useEventContext()

const emitRplEvent = (
event: string,
payload: rplEventPayload = {},
options: rplEventOptions = {}
) => {
if (eventContext) {
payload = { ...eventContext, ...payload }
}
payload = { ...(context?.value || {}), ...payload }
if (emit) {
emit(event, payload)
}
Expand Down
1 change: 1 addition & 0 deletions packages/ripple-ui-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export { isExternalUrl } from './utils/isExternalUrl'
export { formatDate } from './lib/helpers'
export { RplIconGroups } from './components/icon/constants'
export { useRippleEvent } from './composables/useRippleEvent'
export { default as useEventContext } from './composables/useEventContext'
export type { rplEventPayload } from './composables/useRippleEvent'
Loading