Commit 88295a48 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov
Browse files

Merge branch '382-bug-vocabulary-details-loader' into 'master'

Resolve "BUG Vocabulary details: loader"

Closes #382

See merge request !306
parents ec6918c2 d57fdb4d
Pipeline #6897 passed with stages
in 5 minutes and 58 seconds
......@@ -3,7 +3,7 @@ import VocabularyService from 'service/VocabularyService';
import { log } from 'utilities/debug';
import {
CREATE_VOCABULARY, RECEIVE_VOCABULARY, RECEIVE_VOCABULARIES,
CREATE_VOCABULARY, RECEIVE_VOCABULARY, RECEIVE_VOCABULARIES, RECEIVE_VOCABULARY_TERMS, APPEND_VOCABULARY_TERMS,
} from 'vocabulary/constants';
import { IReducerAction, Page } from 'model/common.model';
......@@ -19,6 +19,14 @@ const receiveVocabulary = (vocabulary: Vocabulary): IReducerAction => ({
type: RECEIVE_VOCABULARY, payload: vocabulary,
});
const receiveVocabularyTerms = (terms): IReducerAction => ({
type: RECEIVE_VOCABULARY_TERMS, payload: terms,
});
const appendVocabularyTerms = (terms): IReducerAction => ({
type: APPEND_VOCABULARY_TERMS, payload: terms,
});
// Create a new record
export const createVocabulary = () => (dispatch) => {
log('Create new vocabulary');
......@@ -45,6 +53,13 @@ export const listVocabularyTerms = (uuid: string, page: number, results: number)
log('Loading vocabulary terms', uuid);
return VocabularyService.listTerms(uuid, { page, size: results })
.then((terms) => {
if (terms.number === 0) {
return dispatch(receiveVocabularyTerms(terms));
} else {
return dispatch(appendVocabularyTerms(terms));
}
})
.catch((error) => {
log(`No vocabulary with uuid ${uuid}`, error);
});
......
......@@ -7,5 +7,7 @@ export const RECEIVE_VOCABULARY = 'App/Vocabulary/RECEIVE_VOCABULARY';
export const VOCABULARY_FILTERFORM = 'Form/Vocabulary/VOCABULARY_FILTERFORM';
export const LIST_VOCABULARIES = 'App/Vocabulary/LIST_VOCABULARIES';
export const RECEIVE_VOCABULARIES = 'App/Vocabulary/RECEIVE_VOCABULARIES';
export const RECEIVE_VOCABULARY_TERMS = 'App/Vocabulary/RECEIVE_VOCABULARY_TERMS';
export const APPEND_VOCABULARY_TERMS = 'App/Vocabulary/APPEND_VOCABULARY_TERMS';
export const RECEIVE_MY_VOCABULARYLIST = 'App/Vocabulary/RECEIVE_MY_VOCABULARYLIST';
......@@ -3,10 +3,11 @@ import {log} from 'utilities/debug';
import { IReducerAction } from 'model/common.model';
import { CREATE_VOCABULARY, RECEIVE_VOCABULARY, RECEIVE_VOCABULARIES } from 'vocabulary/constants';
import { CREATE_VOCABULARY, RECEIVE_VOCABULARY, RECEIVE_VOCABULARIES, RECEIVE_VOCABULARY_TERMS, APPEND_VOCABULARY_TERMS } from 'vocabulary/constants';
const INITIAL_STATE = {
currentVocabulary: null,
terms: null,
paged: null,
};
......@@ -34,6 +35,27 @@ export default function publicVocabulary(state = INITIAL_STATE, action: IReducer
});
}
case RECEIVE_VOCABULARY_TERMS: {
log('RECEIVE_VOCABULARY_TERMS', action);
return update(state, {
terms: { $set: action.payload },
});
}
case APPEND_VOCABULARY_TERMS: {
log('APPEND_VOCABULARY_TERMS', action);
return !state.terms ? update(state, {
terms: { $set: action.payload },
}) :
update(state, {
terms: {
content: { $push: action.payload.content },
number: {$set: action.payload.number},
last: {$set: action.payload.last},
},
});
}
default:
return state;
}
......
......@@ -29,6 +29,7 @@ interface IDisplayPageProps extends React.ClassAttributes<any> {
classes: any;
uuid?: string;
vocabularyTerms: Page<VocabularyTerm>;
loadVocabulary: (uuid: string) => void;
listVocabularyTerms: any;
deleteVocabulary: (vocabulary: Vocabulary) => Promise<Vocabulary>;
......@@ -50,38 +51,21 @@ class DisplayPage extends React.Component<IDisplayPageProps, any> {
protected static needs = [
({params: {uuid}}) => loadVocabulary(uuid),
({params: {uuid}}) => listVocabularyTerms(uuid, 0 , 50),
];
constructor(props: IDisplayPageProps, context: any) {
super(props, context);
this.state = { paged: null };
}
public componentWillMount() {
const {vocabulary, loadVocabulary, uuid} = this.props;
const {vocabulary, loadVocabulary, uuid, listVocabularyTerms} = this.props;
if (uuid && (!vocabulary || vocabulary.uuid !== uuid)) {
loadVocabulary(uuid);
} else {
const paged = Page.from(vocabulary.terms, vocabulary.termCount, 0, 50);
this.setState({paged});
listVocabularyTerms(uuid, 0, 50);
}
}
public componentWillReceiveProps(nextProps) {
const {vocabulary} = nextProps;
const paged = Page.from(vocabulary.terms, vocabulary.termCount, 0, 50);
this.setState({paged});
}
protected onPaginationChange = (page, results, sortBy) => {
const {vocabulary, listVocabularyTerms} = this.props;
listVocabularyTerms(vocabulary.uuid, page, results).then((paged) => {
this.setState({ paged });
});
listVocabularyTerms(vocabulary.uuid, page, results);
}
private onDelete = (e) => {
......@@ -99,8 +83,7 @@ class DisplayPage extends React.Component<IDisplayPageProps, any> {
}
public render() {
const {classes, uuid, vocabulary, listVocabularyTerms} = this.props;
const {paged} = this.state;
const {classes, uuid, vocabulary, listVocabularyTerms, vocabularyTerms} = this.props;
const stillLoading: boolean = (!vocabulary || (uuid && vocabulary && vocabulary.uuid !== uuid));
......@@ -143,11 +126,11 @@ class DisplayPage extends React.Component<IDisplayPageProps, any> {
</Card>
</Grid>
{ paged && paged.content &&
{ vocabularyTerms && vocabularyTerms.content &&
<Grid item xs={ 12 }>
<Section title="Accepted vocabulary terms">
{ paged.totalElements > 50 &&
<PaginationComponent pageObj={ paged }
{ vocabularyTerms.totalElements > 50 &&
<PaginationComponent pageObj={ vocabularyTerms }
onChange={ this.onPaginationChange }
infinite
displayName="terms"
......@@ -161,7 +144,7 @@ class DisplayPage extends React.Component<IDisplayPageProps, any> {
</TableRow>
) }>
<PagedLoader
paged={ paged }
paged={ vocabularyTerms }
loadingIndicator={ <Loading /> }
loadPage={ loadVocabularyTermsPage }
colSpan={ 3 }
......@@ -181,6 +164,7 @@ class DisplayPage extends React.Component<IDisplayPageProps, any> {
const mapStateToProps = (state, ownProps) => ({
uuid: ownProps.match.params.uuid,
vocabulary: state.vocabulary.public.currentVocabulary,
vocabularyTerms: state.vocabulary.public.terms,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment