Skip to content

Commit

Permalink
[HOTFIX] Custom page error fix (#6402)
Browse files Browse the repository at this point in the history
* Fixed custom landing page error

* Version bump
  • Loading branch information
Kevin Nderitu authored Jan 30, 2024
1 parent 07c8375 commit 87d0f00
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
11 changes: 8 additions & 3 deletions app/react/entry-server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from 'fs';
import { AgnosticDataRouteObject, createStaticHandler } from '@remix-run/router';
import api from 'app/utils/api';
import { RequestParams } from 'app/utils/RequestParams';
import { omit } from 'lodash';
import { omit, isEmpty } from 'lodash';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { Helmet } from 'react-helmet';
Expand Down Expand Up @@ -188,11 +188,15 @@ const setReduxState = async (
Cookie: `connect.sid=${req.cookies['connect.sid']}`,
tenant: req.get('tenant'),
};
const requestParams = new RequestParams(
const requestParams = new RequestParams<{ q?: string }>(
{ ...req.query, ...omit(routeParams, 'lang') },
headers
);

if (requestParams.data && !isEmpty(requestParams.data) && requestParams.data.q) {
requestParams.data.q = decodeURI(requestParams.data.q);
}

try {
await Promise.all(
dataLoaders.map(async loader => {
Expand Down Expand Up @@ -255,6 +259,7 @@ const EntryServer = async (req: ExpressRequest, res: Response) => {
const routes = getRoutes(settings, req.user && req.user._id, headers);
const matched = matchRoutes(routes, req.path);
const language = matched ? matched[0].params.lang : req.language;
const isCatchAll = matched ? matched[matched.length - 1].route.path === '*' : true;

const { reduxState, staticHandleContext, router } = await getSSRProperties(
req,
Expand Down Expand Up @@ -298,7 +303,7 @@ const EntryServer = async (req: ExpressRequest, res: Response) => {
/>
);

res.send(`<!DOCTYPE html>${html}`);
res.status(isCatchAll ? 404 : 200).send(`<!DOCTYPE html>${html}`);
};

export { EntryServer };
11 changes: 10 additions & 1 deletion app/react/getIndexElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ import { LibraryCards } from './Library/Library';
import { Login } from './Users/Login';
import { ViewerRoute } from './Viewer/ViewerRoute';

const deconstructSearchQuery = (query?: string) => {
if (!query) return '';
if (query.startsWith('?q=')) {
return query.substring(1).split('=')[1];
}
return '(' + query.substring(1) + ')';
};

const getCustomLibraryPage = (customHomePage: string[]) => {
const [query] = customHomePage.filter(path => path.startsWith('?'));
const queryString = `(${query.substring(1)})`;
let searchQuery = deconstructSearchQuery(query);
let queryString = query ? searchQuery : '';

if (customHomePage.includes('map')) {
return <LibraryMap params={{ q: queryString }} />;
Expand Down
26 changes: 16 additions & 10 deletions cypress/e2e/entity.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,28 @@ describe('Entities', () => {
clearCookiesAndLogin();
});

const saveEntity = (message = 'Entity created') => {
cy.contains('button', 'Save').click();
cy.contains(message).as('successMessage');
cy.get('@successMessage').should('not.exist');
};

it('Should create new entity', () => {
clickOnCreateEntity();
cy.get('[name="library.sidepanel.metadata.title"]').click();
cy.get('[name="library.sidepanel.metadata.title"]').type('Test title', { force: true });
cy.contains('button', 'Save').click();
saveEntity();
});

describe('Rich text fields', () => {
it('should create an entity with HTML on a rich text field', () => {
clickOnCreateEntity();
cy.get('[name="library.sidepanel.metadata.title"]').click();
cy.get('[name="library.sidepanel.metadata.title"]').type('Entity with HTML', { force: true });
cy.get('#metadataForm').find('select').select('Reporte', { force: true });
cy.get('#metadataForm').find('select').select('Reporte', { timeout: 100 });

cy.get('#tabpanel-edit > textarea').type(textWithHtml);
cy.contains('button', 'Save').click();
saveEntity();
});

it('should check that the HTML is show as expected', () => {
Expand Down Expand Up @@ -98,7 +104,7 @@ describe('Entities', () => {
cy.get('input[aria-label=fileInput]').first().selectFile('./cypress/test_files/batman.jpg', {
force: true,
});
cy.contains('button', 'Save').click();
saveEntity();
});

it('should edit the entity to add a video on a metadata field', () => {
Expand All @@ -112,7 +118,7 @@ describe('Entities', () => {
.selectFile('./cypress/test_files/short-video.webm', {
force: true,
});
cy.contains('button', 'Save').click();
saveEntity('Entity updated');
});

it('should check the entity', () => {
Expand Down Expand Up @@ -199,7 +205,6 @@ describe('Entities', () => {
const expectedRenamedFiles = ['batman.jpg', 'My PDF.pdf', 'Resource from web'];
cy.get('.attachment-name span:not(.attachment-size)').each((element, index) => {
const content = element.text();
console.log(content);
cy.wrap(content).should('eq', expectedRenamedFiles[index]);
});
});
Expand Down Expand Up @@ -237,7 +242,7 @@ describe('Entities', () => {
.selectFile('./cypress/test_files/valid.pdf', {
force: true,
});
cy.contains('button', 'Save').click();
saveEntity();
});

it('should edit the entity and the documents', () => {
Expand All @@ -255,7 +260,7 @@ describe('Entities', () => {
.selectFile('./cypress/test_files/invalid.pdf', {
force: true,
});
cy.contains('button', 'Save').click();
saveEntity('Entity updated');
cy.contains('.item-document', 'Entity with main documents').click();
cy.contains('.file-originalname', 'Renamed file.pdf').should('exist');
cy.contains('.file-originalname', 'invalid.pdf').should('exist');
Expand Down Expand Up @@ -337,8 +342,9 @@ describe('Entities', () => {
'add value'
).click();
cy.get('input[name=value]#newThesauriValue').click();
cy.get('input[name=value]#newThesauriValue').clear();
cy.get('input[name=value]#newThesauriValue').type('New Value', {
force: true,
timeout: 100,
});
cy.contains('button.confirm-button', 'Save').click();
cy.contains(
Expand All @@ -360,7 +366,7 @@ describe('Entities', () => {
});
});

it('should add a thesauti value on a single select field and select it', () => {
it('should add a thesauri value on a single select field and select it', () => {
cy.contains(
'#metadataForm > div:nth-child(3) > .form-group.select > ul > .wide > div > div > button > span',
'add value'
Expand Down
33 changes: 33 additions & 0 deletions e2e/suite1/pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,37 @@ describe('Custom home page and styles', () => {
const rowSelector = '.tableview-wrapper > table > tbody > tr.template-58ada34c299e82674854504b';
expect((await page.$$(rowSelector)).length).toBe(2);
});

it('should allow using a complicated library query as a landing page', async () => {
await expect(page).toClick('a', { text: 'Settings' });
await expect(page).toClick('a', { text: 'Collection' });
await expect(page).toFill(
'input[name="home_page"]',
'/en/library/?q=(allAggregations:!f,filters:(),from:0,includeUnpublished:!t,limit:30,order:desc,sort:creationDate,treatAs:number,types:!(%2758ada34c299e82674854504b%27),unpublished:!f)'
);
await expect(page).toClick('button', { text: 'Save' });
await expect(page).toClick('div.alert-success');
});
it('should check that the landing page is the defined library query', async () => {
await page.goto(`${host}`);
await page.reload();
await disableTransitions();
await page.waitForSelector('.item-group');
const rowSelector = '.item-document';
await expect((await page.$$(rowSelector)).length).toBe(2);
await expect(page).toMatchElement('.item-group .item-document:nth-child(1) .item-name', {
text: 'Corte Interamericana de Derechos Humanos',
});
await expect(page).toMatchElement('.item-group .item-document:nth-child(2) .item-name', {
text: 'Comisión Interamericana de Derechos Humanos',
});
});

it('should allow using a default library url with language as a landing page', async () => {
await expect(page).toClick('a', { text: 'Settings' });
await expect(page).toClick('a', { text: 'Collection' });
await expect(page).toFill('input[name="home_page"]', '/en/library/');
await expect(page).toClick('button', { text: 'Save' });
await expect(page).toClick('div.alert-success');
});
});
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.151.0",
"version": "1.151.1",
"description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.",
"keywords": [
"react"
Expand Down

0 comments on commit 87d0f00

Please sign in to comment.