Skip to content

Commit

Permalink
feat: migrate extra params component
Browse files Browse the repository at this point in the history
  • Loading branch information
diegopf committed Jun 19, 2024
1 parent b4c5f7b commit 46c231f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ describe('testing extra params component', () => {

expect(extraParamsProvidedCallback).toHaveBeenCalledWith<[WirePayload<Dictionary<unknown>>]>({
eventPayload: { warehouse: 1234 },
metadata: { moduleName: 'extraParams', location: undefined, replaceable: true }
metadata: { moduleName: 'extraParams', location: 'none', replaceable: true }
});
expect(extraParamsProvidedCallback).toHaveBeenCalledTimes(1);

await wrapper.setProps({ values: { warehouse: 5678 } });

expect(extraParamsProvidedCallback).toHaveBeenCalledWith<[WirePayload<Dictionary<unknown>>]>({
eventPayload: { warehouse: 5678 },
metadata: { moduleName: 'extraParams', location: undefined, replaceable: true }
metadata: { moduleName: 'extraParams', location: 'none', replaceable: true }
});
expect(extraParamsProvidedCallback).toHaveBeenCalledTimes(2);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
<script lang="ts">
import { Dictionary } from '@empathyco/x-utils';
import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';
import { State, xComponentMixin, XEmit } from '../../../components';
import { defineComponent, watch } from 'vue';
import { extraParamsXModule } from '../x-module';
import { use$x } from '../../../composables/use-$x';
import { useState } from '../../../composables/use-state';
/**
* It emits a {@link ExtraParamsXEvents.ExtraParamsProvided} with the values
* received as a prop.
*
* @public
*/
@Component({
mixins: [xComponentMixin(extraParamsXModule)]
})
export default class ExtraParams extends Vue {
/**
* Emits the initial extra params, overriding with the state extra params, just in case, those
* values were already set by XComponents initialization (url, plugin config, etc.).
*/
created(): void {
this.$x.emit('ExtraParamsInitialized', { ...this.values });
this.$x.emit('ExtraParamsProvided', { ...this.values, ...this.storeExtraParams });
}
/**
* (Required) A Dictionary where the keys are the extra param names and its values.
*
* @remarks Emits the {@link ExtraParamsXEvents.ExtraParamsProvided} when the
* component is rendered or the values changed.
*
* @public
*/
@XEmit('ExtraParamsProvided', { immediate: false, deep: true })
@Prop({ required: true })
public values!: Dictionary<unknown>;
export default defineComponent({
name: 'ExtraParams',
xModule: extraParamsXModule.name,
props: {
values: {
type: Object as () => Dictionary<unknown>,
required: true
}
},
setup(props) {
const { params } = useState('extraParams', ['params']);
const $x = use$x();
/**
* State extra params. Used to override the initial extra params.
*/
@State('extraParams', 'params')
public storeExtraParams!: Dictionary<unknown>;
$x.emit('ExtraParamsInitialized', { ...props.values });
$x.emit('ExtraParamsProvided', { ...props.values, ...params.value });
// eslint-disable-next-line @typescript-eslint/no-empty-function
render(): void {}
}
watch(
() => props.values,
values => {
$x.emit('ExtraParamsProvided', { ...values });
},
{ deep: true }
);
}
});
</script>

<docs lang="mdx">
Expand Down

0 comments on commit 46c231f

Please sign in to comment.