Skip to content

Commit

Permalink
[HOTFIX] - Page datasets update (#7309)
Browse files Browse the repository at this point in the history
* increase version

* expose function

* types for datasets

* simple updater function

* e2e test

* revert change to config
  • Loading branch information
Zasa-san authored Oct 7, 2024
1 parent c458d34 commit 0f7ee18
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/react/Pages/PageView.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';

import { isClient } from 'app/utils';
import { actions } from 'app/BasicReducer';
import { unselectAllDocuments } from 'app/Library/actions/libraryActions';
import { wrapDispatch } from 'app/Multireducer';
Expand All @@ -11,6 +11,7 @@ import { trackPage } from 'app/App/GoogleAnalytics';
import { ErrorBoundary } from 'app/V2/Components/ErrorHandling';
import { PageViewer } from './components/PageViewer';
import { getPageAssets } from './utils/getPageAssets';
import { updatePageDatasets } from './utils/updatePageDatasets';

class PageViewComponent extends RouteHandler {
static async requestState(requestParams) {
Expand All @@ -32,6 +33,9 @@ class PageViewComponent extends RouteHandler {

componentDidMount() {
this.closeSidePanel();
if (isClient) {
window.updatePageDatasets = updatePageDatasets;
}
}

componentWillUnmount() {
Expand Down
6 changes: 6 additions & 0 deletions app/react/Pages/specs/PageView.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,10 @@ describe('PageView', () => {
});
});
});

describe('dataset functions', () => {
it('should expose a function to update page datasets', () => {
expect(window.updatePageDatasets).not.toBeUndefined();
});
});
});
43 changes: 43 additions & 0 deletions app/react/Pages/utils/specs/updatePageDatasets.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @jest-environment jsdom
*/
import { fromJS } from 'immutable';
import { Store } from 'redux';
import create from 'app/store';
import { IStore } from 'app/istore';
import { updatePageDatasets } from '../updatePageDatasets';

describe('Update page datasets', () => {
let ReduxStore: Store<IStore>;

beforeEach(() => {
ReduxStore = create({
page: {
datasets: fromJS({
default: { rows: [], totalRows: 10, aggregations: {} },
dataset1: { rows: [], totalRows: 10, aggregations: {} },
}),
},
});

spyOn(ReduxStore, 'dispatch');
});

it('should update the selected dataset', () => {
updatePageDatasets('dataset1', { rows: [], totalRows: 7, aggregations: { new: 'info' } });

expect(ReduxStore.dispatch).toHaveBeenCalledWith({
type: 'page/datasets/SET',
value: {
default: { rows: [], totalRows: 10, aggregations: {} },
dataset1: { rows: [], totalRows: 7, aggregations: { new: 'info' } },
},
});
});

it('should not dispatch if there is no dataset', () => {
updatePageDatasets('dataset2', { rows: [], totalRows: 7, aggregations: { new: 'info' } });

expect(ReduxStore.dispatch).not.toHaveBeenCalled();
});
});
23 changes: 23 additions & 0 deletions app/react/Pages/utils/updatePageDatasets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { actions } from 'app/BasicReducer';
import { store } from 'app/store';

declare global {
interface Window {
updatePageDatasets: (name: string, data: any) => void;
}
}

const updatePageDatasets = (name: string, data: any) => {
if (!store) {
return;
}

const currentDatasets = store.getState().page.datasets?.toJS();

if (currentDatasets && currentDatasets[name]) {
currentDatasets[name] = data;
store.dispatch(actions.set('page/datasets', currentDatasets));
}
};

export { updatePageDatasets };
3 changes: 3 additions & 0 deletions app/react/istore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ export interface IStore {
data: PageType;
uiState: IImmutable<{ savingPage: boolean }>;
formState: any;
datasets?: IImmutable<{
[name: string]: { rows: any[]; totalRows: number; aggregations: any };
}>;
};
pages: IImmutable<PageType>;
relationTypes: IImmutable<RelationshipTypesType[]>;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions cypress/e2e/pages/graphs.cy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { clearCookiesAndLogin } from '../helpers/login';

const updateDatasetScript = `window.updatePageDatasets("default",{rows:[],totalRows:292,relation:"eq",aggregations:
{all:{tipo:{meta:{},doc_count:915,buckets:[{key:"57d8a9c4-f0cd-4290-b15e-ddf2b3c6ef91",doc_count:6,filtered:
{meta:{},doc_count:2},label:"De asunto"},{key:"d79ab686-a987-4a1e-a26a-0a604a5f3aae",doc_count:6,filtered:
{meta:{},doc_count:2},label:"En casos"}],count:11}}}});`;

const graphs = {
barChart: '<BarChart property="tipo" context="58ada34c299e8267485450fb" />',
pieChart: '<PieChart property="tipo" context="58ada34c299e8267485450fb" />',
Expand Down Expand Up @@ -81,4 +86,27 @@ describe('Graphs in Page ', () => {
it('should insert List chart with nested graph in created page', () => {
testChart(graphs.listChartScatter, 'List chart with nested graph');
});

describe('dataset updates', () => {
it('should update a graph via the page script using the updated function', () => {
cy.contains('a', 'Settings').click();
cy.contains('a', 'Pages').click();
cy.contains('tr', 'Bar chart graph').contains('button', 'Edit').click();
cy.contains('Javascript').click();
// delete extra closing keys added by the code editor when writing this text
// eslint-disable-next-line cypress/unsafe-to-chain-command
cy.get('div[data-mode-id="javascript"]')
.type(updateDatasetScript, {
parseSpecialCharSequences: false,
delay: 0,
})
.type('{backspace}{backspace}');
// wait for editor to update
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(501);
savePage();
visitPage();
takeSnapshot();
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uwazi",
"version": "1.187.2",
"version": "1.187.3",
"description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.",
"keywords": [
"react"
Expand Down

0 comments on commit 0f7ee18

Please sign in to comment.