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(xcontrols): add actions #1288

Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 2 additions & 0 deletions packages/x-components/src/wiring/events.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { TaggingXEvents } from '../x-modules/tagging/events.types';
import { UrlXEvents } from '../x-modules/url/events.types';
import { XModuleName } from '../x-modules/x-modules.types';
import { SemanticQueriesXEvents } from '../x-modules/semantic-queries/events.types';
import { ExperienceControlsXEvents } from '../x-modules/experience-controls/events.types';
import { WireMetadata } from './wiring.types';
/* eslint-disable max-len */
/**.
Expand Down Expand Up @@ -70,6 +71,7 @@ export interface XEventsTypes
SearchXEvents,
SemanticQueriesXEvents,
TaggingXEvents,
ExperienceControlsXEvents,
UrlXEvents {
/**
* The provided number of columns of a grid has changed.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createFetchAndSaveActions } from '../../../../store/utils/fetch-and-save-action.utils';
import { ExperienceControlsActionContext } from '../types';

const { fetchAndSave, cancelPrevious } = createFetchAndSaveActions<
ExperienceControlsActionContext,
//TODO: change types when the adapter is updated
any,
any
CachedaCodes marked this conversation as resolved.
Show resolved Hide resolved
>({
fetch({ dispatch }, request) {
return dispatch('fetchExperienceControlsResponse', request);
},
onSuccess({ commit }, controls) {
commit('setControls', controls);
CachedaCodes marked this conversation as resolved.
Show resolved Hide resolved
commit('setEvents', {});
}
});
Copy link
Contributor

Choose a reason for hiding this comment

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

This still doesn't take into account that the object returned by the success of the fetch will be similar to { controls, events }

Suggested change
onSuccess({ commit }, controls) {
commit('setControls', controls);
commit('setEvents', {});
}
});
onSuccess({ commit }, experienceControlsResponse) {
commit('setControls', experienceControlsResponse.controls);
commit('setEvents', experienceControlsResponse.events);
}
});


/**
* Default implementation for {@link ExperienceControlsActions.fetchAndSaveControls} action.
*
* @public
*/
export const fetchAndSaveExperienceControlsResponse = fetchAndSave;

/**
* Default implementation for {@link ExperienceControlsActions.cancelFetchAndSaveControls} action.
*
* @public
*/
export const cancelFetchAndSaveControls = cancelPrevious;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ExperienceControlsXStoreModule } from '../types';

/**
* Default implementation for the {@link ExperienceControlsActions.fetchControls}.
*
* @param _context - The {@link https://vuex.vuejs.org/guide/actions.html | context} of the actions,
* provided by Vuex.
* TODO: update when adapter is updateds.
* @param request
* @returns
*
* @public
*/

// eslint-disable-next-line max-len
export const fetchExperienceControlsResponse: ExperienceControlsXStoreModule['actions']['fetchExperienceControlsResponse'] =
async () => {
const response = await fetch(
// eslint-disable-next-line max-len
'https://config-service.internal.test.empathy.co/public/configs?service=xcontrols&instance=empathy'
);

if (response.ok) {
const controls = await response.json();

const aux = {
controls,
events: {}
};

return aux;
} else {
throw new Error('Failed to fetch data');
}

//return XPlugin.adapter.experienceControls(request).then(({ controls }) => controls);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createStoreEmitters } from '../../../store';
import { experienceControlsXStoreModule } from './module';

/**
* {@link StoreEmitters} For the experience-controls module.
* {@link StoreEmitters} For the {@link ExperienceControlsXModule}.
*
* @internal
*/
export const experienceControlsEmitters = {};
export const experienceControlsEmitters = createStoreEmitters(experienceControlsXStoreModule, {});
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fetchAndSaveExperienceControlsResponse } from './actions/fetch-and-save-controls.action';
import { fetchExperienceControlsResponse } from './actions/fetch-controls.action';
import { ExperienceControlsXStoreModule } from './types';

/**.
Expand All @@ -9,7 +11,8 @@ export const experienceControlsXStoreModule: ExperienceControlsXStoreModule = {
state: () => ({
controls: {},
events: {},
status: 'initial'
status: 'initial',
params: {}
}),
getters: {},
mutations: {
Expand All @@ -18,7 +21,13 @@ export const experienceControlsXStoreModule: ExperienceControlsXStoreModule = {
},
setEvents(state, events) {
Object.assign(state.events, events);
},
setParams(state, params) {
state.params = params;
}
},
actions: {}
actions: {
fetchExperienceControlsResponse,
fetchAndSaveExperienceControlsResponse
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ExperienceControlsState extends StatusState {
*/
controls: Dictionary<unknown>;
events: Partial<XEventsTypes>;
params: Dictionary<unknown>;
}

/**
Expand Down Expand Up @@ -41,9 +42,28 @@ export interface ExperienceControlsMutations {
* @param events - The new {@link ExperienceControlsState.events}.
*/
setEvents(events: Partial<XEventsTypes>): void;
/**
* Sets the extra params of the module.
*
* @param params - The new extra params.
*/
setParams(params: Dictionary<unknown>): void;
}

export interface ExperienceControlsActions {}
export interface ExperienceControlsActions {
/**
* Fetches the {@link ExperienceControlsState.controls} property.
*
* @param request - The request to fetch the {@link ExperienceControlsState.controls}.
* @returns A promise of the {@link ExperienceControlsState.controls}.
*/
fetchExperienceControlsResponse(request: any): Promise<{ [key: string]: unknown }>;

/**
* Fetches and saves the {@link ExperienceControlsState.controls} property.
*/
fetchAndSaveExperienceControlsResponse(request: any): void;
CachedaCodes marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Experience Controls type safe store module.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import { namespacedWireCommit } from '../../wiring';
import { createWiring } from '../../wiring/wiring.utils';

const moduleName = 'experienceControls';

/**
* WireCommit for {@link SemanticQueriesXModule}.
*
* @internal
*/
const wireCommit = namespacedWireCommit(moduleName);

/**
* WireCommit for {@link ExperienceControlsXModule}.
*
* @internal
*/
export const setParamsWire = wireCommit('setParams');

/**
* Wiring configuration for the {@link ExperienceControlsXModule | experience-controls module}.
*
* @internal
*/
export const experienceControlsWiring = createWiring({});
export const experienceControlsWiring = createWiring({
ExtraParamsChanged: {
setParamsWire
}
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { XModule } from '../x-modules.types';
import { experienceControlsEmitters } from './store/emitters';
import { experienceControlsXStoreModule } from './store/module';
import { ExperienceControlsXStoreModule } from './store/types';
import { experienceControlsWiring } from './wiring';

/**
* Search {@link XModule} alias.
*
* @public
*/
export type ExperienceControlsXModule = XModule<any>;
export type ExperienceControlsXModule = XModule<ExperienceControlsXStoreModule>;

/**
* Search {@link XModule} implementation. This module is auto-registered as soon as you
Expand All @@ -15,7 +19,7 @@ export type ExperienceControlsXModule = XModule<any>;
*/
export const experienceControlsXModule: ExperienceControlsXModule = {
name: 'experienceControls',
storeModule: {},
storeEmitters: {},
wiring: {}
storeModule: experienceControlsXStoreModule,
storeEmitters: experienceControlsEmitters,
wiring: experienceControlsWiring
};