Skip to content

Commit

Permalink
UI toc generation components, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Feb 19, 2021
1 parent 2883177 commit bcdb188
Show file tree
Hide file tree
Showing 38 changed files with 574 additions and 76 deletions.
5 changes: 2 additions & 3 deletions app/api/files/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ export const files = {
return toDeleteFiles;
},

async tocReviewed(_id: string) {
async tocReviewed(_id: string, language: string) {
const savedFile = await files.save({ _id, generatedToc: false });
const sameEntityFiles = await files.get({ entity: savedFile.entity }, { generatedToc: 1 });
const [entity] = await entities.get({
sharedId: savedFile.entity,
language: savedFile.language,
});

await entities.save(
Expand All @@ -50,7 +49,7 @@ export const files = {
false
),
},
{ user: {}, language: savedFile.language }
{ user: {}, language }
);

return savedFile;
Expand Down
2 changes: 1 addition & 1 deletion app/api/files/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default (app: Application) => {
}),
async (req, res, next) => {
try {
res.json(await files.tocReviewed(req.body.fileId));
res.json(await files.tocReviewed(req.body.fileId, req.language));
} catch (e) {
next(e);
}
Expand Down
2 changes: 0 additions & 2 deletions app/api/files/specs/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const fixtures: DBFixture = {
{
_id: uploadId,
entity: 'sharedId1',
language: 'es',
generatedToc: true,
originalname: 'upload1',
filename: fileName1,
Expand All @@ -22,7 +21,6 @@ const fixtures: DBFixture = {
{
_id: uploadId2,
generatedToc: true,
language: 'es',
entity: 'sharedId1',
filename: 'fileNotInDisk',
},
Expand Down
4 changes: 2 additions & 2 deletions app/api/search/deprecatedRoutes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Joi from 'joi';
import entities from 'api/entities';
import { searchSchema } from 'api/search/searchSchema';
import { searchParamsSchema } from 'shared/types/searchParams';
import { search } from './search';
import { validation, parseQuery } from '../utils';

Expand All @@ -25,7 +25,7 @@ export default app => {
app.get(
'/api/search',
parseQuery,
validation.validateRequest(searchSchema),
validation.validateRequest(searchParamsSchema),

(req, res, next) => {
const action = req.query.geolocation ? 'searchGeolocations' : 'search';
Expand Down
3 changes: 2 additions & 1 deletion app/api/search/elasticTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RequestParams } from '@elastic/elasticsearch';
import { RequestBody } from '@elastic/elasticsearch/lib/Transport';
import { Aggregations } from 'shared/types/Aggregations.d.ts';

interface ShardsResponse {
total: number;
Expand Down Expand Up @@ -42,7 +43,7 @@ export interface SearchResponse<T> {
sort?: string[];
}>;
};
aggregations?: any;
aggregations?: Aggregations;
}

export type IndicesDelete = Omit<RequestParams.IndicesDelete, 'index'>;
Expand Down
9 changes: 5 additions & 4 deletions app/api/search/specs/searchSchema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ValidationError } from 'ajv';
import { validation } from 'api/utils';
import { searchSchema } from '../searchSchema';
import { searchParamsSchema } from 'shared/types/searchParams';

describe('search schema', () => {
const validQuery = {
Expand All @@ -27,11 +27,12 @@ describe('search schema', () => {

it('should not have validation errors for valid search', async () => {
const validSearch = { validQuery };
await validation.validateRequest(searchSchema)(validSearch, null, expectValidSchema);
await validation.validateRequest(searchParamsSchema)(validSearch, null, expectValidSchema);
});

it('should support a number as a search term', async () => {
const validSearch = { query: { ...validQuery, searchTerm: 3 } };
await validation.validateRequest(searchSchema)(validSearch, null, expectValidSchema);
await validation.validateRequest(searchParamsSchema)(validSearch, null, expectValidSchema);
});
});

Expand All @@ -42,7 +43,7 @@ describe('search schema', () => {

async function testInvalidProperty(invalidProperty: any) {
const invalidSearch = { query: { ...validQuery, ...invalidProperty } };
await validation.validateRequest(searchSchema)(invalidSearch, null, expectInvalidSchema);
await validation.validateRequest(searchParamsSchema)(invalidSearch, null, expectInvalidSchema);
}

it('should be invalid if allAgregations is not a boolean value', async () => {
Expand Down
7 changes: 6 additions & 1 deletion app/react/App/scss/modules/_toc.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
.toc {
padding: 15px;
padding: 0px 15px 15px 15px;
}

div.tocHeader {
padding-left: 15px;
border-bottom: 1px solid #f4f4f4;
}

.toc-view{
Expand Down
11 changes: 9 additions & 2 deletions app/react/Attachments/components/File.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { APIURL } from 'app/config.js';
import { LocalForm, Control } from 'react-redux-form';
import { updateFile, deleteFile } from 'app/Attachments/actions/actions';
import { wrapDispatch } from 'app/Multireducer';
import { TocGeneratedLabel } from 'app/ToggledFeatures/tocGeneration';
import { NeedAuthorization } from 'app/Auth';
import { EntitySchema } from 'shared/types/entityType';
import { ViewDocumentLink } from './ViewDocumentLink';
Expand Down Expand Up @@ -102,7 +103,10 @@ export class File extends Component<FileProps, FileState> {
<div>
<div className="file-language">
<Translate>{language ? transformLanguage(language) || '' : ''}</Translate>
</div>{' '}
</div>
<TocGeneratedLabel file={this.props.file}>
<Translate>ML TOC</Translate>
</TocGeneratedLabel>
<a
href={`${APIURL}files/${filename}`}
target="_blank"
Expand Down Expand Up @@ -206,4 +210,7 @@ export class File extends Component<FileProps, FileState> {
const mapDispatchToProps = (dispatch: Dispatch<{}>, props: FileProps) =>
bindActionCreators({ updateFile, deleteFile }, wrapDispatch(dispatch, props.storeKey));

export const ConnectedFile = connect(null, mapDispatchToProps)(File);
export const ConnectedFile = connect(
null,
mapDispatchToProps
)(File);
3 changes: 2 additions & 1 deletion app/react/Attachments/components/specs/File.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import { LocalForm } from 'react-redux-form';
import { FileType } from 'shared/types/fileType';
import { File, FileProps } from '../File';
import { Translate } from 'app/I18N';
import { File, FileProps } from '../File';

describe('file', () => {
let component: ShallowWrapper<File>;
Expand Down Expand Up @@ -32,6 +32,7 @@ describe('file', () => {
});

const render = () => {
//eslint-disable-next-line react/jsx-props-no-spreading
component = shallow(<File {...props} />, { context });
};

Expand Down
15 changes: 14 additions & 1 deletion app/react/Documents/components/DocumentSidePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import ShowIf from 'app/App/ShowIf';
import SidePanel from 'app/Layout/SidePanel';
import DocumentSemanticSearchResults from 'app/SemanticSearch/components/DocumentResults';
import { CopyFromEntity } from 'app/Metadata/components/CopyFromEntity';
import { FeatureToggle } from 'app/components/Elements/FeatureToggle';
import { TocGeneratedLabel, ReviewTocButton } from 'app/ToggledFeatures/tocGeneration';
import { Icon } from 'UI';

import * as viewerModule from 'app/Viewer';
Expand Down Expand Up @@ -321,11 +323,14 @@ export class DocumentSidePanel extends Component {
<div className="sidepanel-footer">
<button
onClick={() => this.props.editToc(this.props.file.toc || [])}
className="edit-toc btn btn-success"
className="edit-toc btn btn-info"
>
<Icon icon="pencil-alt" />
<span className="btn-label">Edit</span>
</button>
<ReviewTocButton file={this.props.file}>
<Translate>Mark as Reviewed</Translate>
</ReviewTocButton>
</div>
</ShowIf>
</NeedAuthorization>
Expand All @@ -340,6 +345,14 @@ export class DocumentSidePanel extends Component {
/>
</TabContent>
<TabContent for="toc" className="toc">
<div className="tocHeader">
<h1>
<Translate>Table of contents</Translate>
</h1>
<TocGeneratedLabel file={this.props.file}>
<Translate>auto-created ⓘ </Translate>
</TocGeneratedLabel>
</div>
<ShowIf if={!this.props.tocBeingEdited}>
<ShowToc
toc={defaultDocumentToC}
Expand Down
7 changes: 5 additions & 2 deletions app/react/Documents/components/ShowToc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { connect } from 'react-redux';
import { scrollToToc } from 'app/Viewer/actions/uiActions';
import Immutable from 'immutable';
import ShowIf from 'app/App/ShowIf';
import { t } from 'app/I18N';
import { t, Translate} from 'app/I18N';
import { Icon } from 'UI';

export class ShowToc extends Component {
Expand Down Expand Up @@ -65,4 +65,7 @@ function mapDispatchToProps() {
return { scrollToToc };
}

export default connect(null, mapDispatchToProps)(ShowToc);
export default connect(
null,
mapDispatchToProps
)(ShowToc);
3 changes: 3 additions & 0 deletions app/react/Library/actions/libraryActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { toUrlParams } from 'shared/JSONRequest';
import { RequestParams } from 'app/utils/RequestParams';
import { store } from 'app/store';
import searchAPI from 'app/Search/SearchAPI';
import { tocGenerationUtils } from 'app/ToggledFeatures/tocGeneration';
import { selectedDocumentsChanged, maybeSaveQuickLabels } from './quickLabelActions';

export function enterLibrary() {
Expand Down Expand Up @@ -249,6 +250,8 @@ export function searchDocuments(
dispatch(actions.set(`${storeKey}.selectedSorting`, currentSearch));
}

searchParams.customFilters = currentSearch.customFilters;

setSearchInUrl(searchParams);
};
}
Expand Down
8 changes: 4 additions & 4 deletions app/react/Library/actions/specs/filterActions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ describe('filterActions', () => {
let filtersState;

beforeEach(() => {
libraryFilters = [
{ name: 'author', filter: true },
{ name: 'country', filter: true },
];
libraryFilters = [{ name: 'author', filter: true }, { name: 'country', filter: true }];
search = { searchTerm: '', filters: { author: 'RR Martin', country: '' } };
filtersState = {
documentTypes,
Expand All @@ -36,6 +33,9 @@ describe('filterActions', () => {
};

store = {
settings: {
collection: Immutable.Map({}),
},
library: {
filters: Immutable.fromJS(filtersState),
search,
Expand Down
43 changes: 31 additions & 12 deletions app/react/Library/actions/specs/libraryActions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ describe('libraryActions', () => {

beforeEach(() => {
dispatch = jasmine.createSpy('dispatch');
getState = jasmine
.createSpy('getState')
.and.returnValue({ library: { filters: Immutable.fromJS(filters), search: {} } });
getState = jasmine.createSpy('getState').and.returnValue({
settings: {
collection: Immutable.Map({}),
},
library: { filters: Immutable.fromJS(filters), search: {} },
});
});

it('should dispatch a SET_LIBRARY_TEMPLATES action ', () => {
Expand Down Expand Up @@ -178,15 +181,24 @@ describe('libraryActions', () => {
{
name: 'relationshipfilter',
type: 'relationshipfilter',
filters: [
{ name: 'status', type: 'select' },
{ name: 'empty', type: 'date' },
],
filters: [{ name: 'status', type: 'select' }, { name: 'empty', type: 'date' }],
},
],
documentTypes: ['decision'],
};
store = { library: { filters: Immutable.fromJS(state), search: { searchTerm: 'batman' } } };
store = {
settings: {
collection: Immutable.Map({}),
},
library: {
filters: Immutable.fromJS(state),
search: {
searchTerm: 'batman',
customFilters: { property: { values: ['value'] } },
filters: {},
},
},
};
spyOn(browserHistory, 'getCurrentLocation').and.returnValue({
pathname: '/library',
query: { view: 'chart' },
Expand Down Expand Up @@ -261,6 +273,16 @@ describe('libraryActions', () => {
);
});

it('should use customFilters from the current search on the store', () => {
const limit = 60;
spyOn(browserHistory, 'push');
actions.searchDocuments({}, storeKey, limit)(dispatch, getState);

expect(browserHistory.push).toHaveBeenCalledWith(
"/library/?view=chart&q=(customFilters:(property:(values:!(value))),filters:(),from:0,limit:60,searchTerm:'batman',sort:_score,types:!(decision))" //eslint-disable-line
);
});

it('should set the storeKey selectedSorting if user has selected a custom sorting', () => {
const expectedDispatch = {
type: 'library.selectedSorting/SET',
Expand Down Expand Up @@ -365,10 +387,7 @@ describe('libraryActions', () => {
},
{
type: types.UPDATE_DOCUMENTS,
docs: [
{ sharedId: '1', metadataResponse },
{ sharedId: '2', metadataResponse },
],
docs: [{ sharedId: '1', metadataResponse }, { sharedId: '2', metadataResponse }],
},
];
const store = mockStore({});
Expand Down
7 changes: 6 additions & 1 deletion app/react/Library/components/FiltersForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { t } from 'app/I18N';
import { wrapDispatch } from 'app/Multireducer';
import debounce from 'app/utils/debounce';
import libraryHelper from 'app/Library/helpers/libraryFilters';
import { FilterTocGeneration } from 'app/ToggledFeatures/tocGeneration';

import Filters from './FiltersFromProperties';

Expand Down Expand Up @@ -97,6 +98,7 @@ export class FiltersForm extends Component {
translationContext={translationContext}
storeKey={this.props.storeKey}
/>
<FilterTocGeneration onChange={this.activateAutoSearch} aggregations={aggregations} />
</Form>
</div>
);
Expand Down Expand Up @@ -126,4 +128,7 @@ function mapDispatchToProps(dispatch, props) {
return bindActionCreators({ searchDocuments }, wrapDispatch(dispatch, props.storeKey));
}

export default connect(mapStateToProps, mapDispatchToProps)(FiltersForm);
export default connect(
mapStateToProps,
mapDispatchToProps
)(FiltersForm);
12 changes: 11 additions & 1 deletion app/react/Library/components/ViewMetadataPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { actions } from 'app/Metadata';
import { deleteDocument, searchSnippets } from 'app/Library/actions/libraryActions';
import { deleteEntity } from 'app/Entities/actions/actions';
import { wrapDispatch } from 'app/Multireducer';
import { entityDefaultDocument } from 'shared/entityDefaultDocument';
import modals from 'app/Modals';

import {
Expand All @@ -22,9 +23,18 @@ const getTemplates = state => state.templates;

const mapStateToProps = (state, props) => {
const library = state[props.storeKey];
const doc = library.ui.get('selectedDocuments').first() || Immutable.fromJS({ documents: [] });
const defaultLanguage = state.settings.collection.get('languages').find(l => l.get('defautl'));
const file = entityDefaultDocument(
doc.get('documents').toJS(),
doc.get('language'),
defaultLanguage
);

return {
open: library.ui.get('selectedDocuments').size === 1,
doc: library.ui.get('selectedDocuments').first() || Immutable.fromJS({}),
doc,
file,
references: library.sidepanel.references,
tab: library.sidepanel.tab,
docBeingEdited: !!Object.keys(library.sidepanel.metadata).length,
Expand Down
Loading

0 comments on commit bcdb188

Please sign in to comment.