From 9a42c85741aa7e72245e052df9ca2b2caf5ebe43 Mon Sep 17 00:00:00 2001 From: Matija Obreza Date: Tue, 2 Oct 2018 19:59:35 +0200 Subject: [PATCH] Using '1.0' as default version for descriptors, descriptor lists and datasets --- src/datasets/reducers/dashboard.ts | 310 +++++++++++----------- src/descriptorlists/reducers/dashboard.ts | 198 +++++++------- src/descriptors/reducers/dashboard.ts | 226 ++++++++-------- 3 files changed, 370 insertions(+), 364 deletions(-) diff --git a/src/datasets/reducers/dashboard.ts b/src/datasets/reducers/dashboard.ts index 503e3de..422c357 100644 --- a/src/datasets/reducers/dashboard.ts +++ b/src/datasets/reducers/dashboard.ts @@ -1,172 +1,174 @@ import update from 'immutability-helper'; -import {log} from 'utilities/debug'; +import { log } from 'utilities/debug'; import * as _ from 'lodash'; import { - CREATE_DATASET, - ADD_CREATOR_TO_DATASET, - REMOVE_CREATOR_FROM_DATASET, - UPDATE_DATASET_CREATOR, - ADD_LOCATION, - RECEIVE_LOCATION, - REMOVE_LOCATION, - DASHBOARD_REMOVE_DATASET, - DASHBOARD_RECEIVE_DATASET_PAGE, - RECEIVE_DATASET, - DASHBOARD_APPEND_DATASET_PAGE, + CREATE_DATASET, + ADD_CREATOR_TO_DATASET, + REMOVE_CREATOR_FROM_DATASET, + UPDATE_DATASET_CREATOR, + ADD_LOCATION, + RECEIVE_LOCATION, + REMOVE_LOCATION, + DASHBOARD_REMOVE_DATASET, + DASHBOARD_RECEIVE_DATASET_PAGE, + RECEIVE_DATASET, + DASHBOARD_APPEND_DATASET_PAGE, } from 'datasets/constants'; import { Dataset } from 'model/dataset.model'; -import {Page} from 'model/common.model'; +import { Page } from 'model/common.model'; const INITIAL_STATE: { - dataset: Dataset, - paged: Page, - pagedQuery: any, + dataset: Dataset, + paged: Page, + pagedQuery: any, } = { - dataset: null, - paged: null, - pagedQuery: null, + dataset: null, + paged: null, + pagedQuery: null, }; function datasetsDashboard(state = INITIAL_STATE, action: { type?: string, payload?: any } = { type: '', payload: {} }) { - switch (action.type) { - case CREATE_DATASET: { - return update(state, { - dataset: { $set: new Dataset() }, - }); - } - case RECEIVE_DATASET: { - const receivedIndex = state.paged ? _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid) : -1; - - if (receivedIndex !== -1) { - return update(state, { - dataset: { $set: action.payload }, - paged: { - content: { - [receivedIndex]: {$set: action.payload}, - }, - }, - }); - } else { - return update(state, { - dataset: { $set: action.payload }, - paged: {$set: null}, - }); - } - } - - case DASHBOARD_REMOVE_DATASET: { - if (state.paged) { - const removeIndex = _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid); - return removeIndex === -1 ? state - : update(state, { - paged: { - content: {$splice: [[removeIndex, 1]]}, - numberOfElements: {$set: state.paged.numberOfElements - 1}, - totalElements: {$set: state.paged.totalElements - 1}, - }, - }); - } - return state; - } - case DASHBOARD_RECEIVE_DATASET_PAGE: { - return update(state, { - paged: { $set: action.payload.paged }, - pagedQuery: { $set: action.payload.query }, - }); - } - case DASHBOARD_APPEND_DATASET_PAGE: { - const {paged, query} = action.payload; - - return !state.paged ? update(state, { - paged: { $set: paged }, - pagedQuery: { $set: query }, - }) : - update(state, { - paged: { - content: {$push: paged.content}, - number: {$set: paged.number}, - last: {$set: paged.last}, - }, - pagedQuery: { $set: query }, - }); - } - - // Creators step - case ADD_CREATOR_TO_DATASET: { - log('Payload ADD_CREATOR_TO_DATASET', action); - if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { - return update(state, { - dataset: { creators: { $push: [ action.payload.creator ] } }, - }); - } else { - return state; - } - } - - case REMOVE_CREATOR_FROM_DATASET: { - log('Payload REMOVE_CREATOR_FROM_DATASET', action); - if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { - return update(state, { - dataset: { creators: { $apply: (creators) => creators.filter((creator) => creator.uuid !== action.payload.creator.uuid) } }, - }); - } else { - return state; - } - } - - case UPDATE_DATASET_CREATOR: { - log('Payload UPDATE_DATASET_CREATOR', action); - if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { - const index = state.dataset.creators.findIndex((creator) => creator.uuid === action.payload.creator.uuid); - return update(state, { - dataset: { creators: { [index]: { $set: action.payload.creator } } }, - }); - } else { - return state; - } - } - - // Location step - case ADD_LOCATION: { - log('Payload ADD_LOCATION', action); - if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { - return update(state, { - dataset: { locations: { $push: [ action.payload.location ] } }, - }); - } else { - return state; - } - } - - case REMOVE_LOCATION: { - log('Payload REMOVE_LOCATION', action); - if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { - return update(state, { - dataset: { locations: { $apply: (locations) => locations.filter((location) => location.uuid !== action.payload.location.uuid) } }, - }); - } else { - return state; - } - } - - case RECEIVE_LOCATION: { - log('Payload RECEIVE_LOCATION', action); - if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { - const index = state.dataset.locations.findIndex((location) => location.uuid === action.payload.location.uuid); - return update(state, { - dataset: { locations: { [index]: { $set: action.payload.location } } }, - }); - } else { - return state; - } - } - - default: - return state; + switch (action.type) { + case CREATE_DATASET: { + const dataset: Dataset = new Dataset(); + dataset.versionTag = '1.0'; + return update(state, { + dataset: { $set: dataset }, + }); } + case RECEIVE_DATASET: { + const receivedIndex = state.paged ? _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid) : -1; + + if (receivedIndex !== -1) { + return update(state, { + dataset: { $set: action.payload }, + paged: { + content: { + [receivedIndex]: { $set: action.payload }, + }, + }, + }); + } else { + return update(state, { + dataset: { $set: action.payload }, + paged: { $set: null }, + }); + } + } + + case DASHBOARD_REMOVE_DATASET: { + if (state.paged) { + const removeIndex = _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid); + return removeIndex === -1 ? state + : update(state, { + paged: { + content: { $splice: [[removeIndex, 1]] }, + numberOfElements: { $set: state.paged.numberOfElements - 1 }, + totalElements: { $set: state.paged.totalElements - 1 }, + }, + }); + } + return state; + } + case DASHBOARD_RECEIVE_DATASET_PAGE: { + return update(state, { + paged: { $set: action.payload.paged }, + pagedQuery: { $set: action.payload.query }, + }); + } + case DASHBOARD_APPEND_DATASET_PAGE: { + const { paged, query } = action.payload; + + return !state.paged ? update(state, { + paged: { $set: paged }, + pagedQuery: { $set: query }, + }) : + update(state, { + paged: { + content: { $push: paged.content }, + number: { $set: paged.number }, + last: { $set: paged.last }, + }, + pagedQuery: { $set: query }, + }); + } + + // Creators step + case ADD_CREATOR_TO_DATASET: { + log('Payload ADD_CREATOR_TO_DATASET', action); + if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { + return update(state, { + dataset: { creators: { $push: [action.payload.creator] } }, + }); + } else { + return state; + } + } + + case REMOVE_CREATOR_FROM_DATASET: { + log('Payload REMOVE_CREATOR_FROM_DATASET', action); + if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { + return update(state, { + dataset: { creators: { $apply: (creators) => creators.filter((creator) => creator.uuid !== action.payload.creator.uuid) } }, + }); + } else { + return state; + } + } + + case UPDATE_DATASET_CREATOR: { + log('Payload UPDATE_DATASET_CREATOR', action); + if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { + const index = state.dataset.creators.findIndex((creator) => creator.uuid === action.payload.creator.uuid); + return update(state, { + dataset: { creators: { [index]: { $set: action.payload.creator } } }, + }); + } else { + return state; + } + } + + // Location step + case ADD_LOCATION: { + log('Payload ADD_LOCATION', action); + if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { + return update(state, { + dataset: { locations: { $push: [action.payload.location] } }, + }); + } else { + return state; + } + } + + case REMOVE_LOCATION: { + log('Payload REMOVE_LOCATION', action); + if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { + return update(state, { + dataset: { locations: { $apply: (locations) => locations.filter((location) => location.uuid !== action.payload.location.uuid) } }, + }); + } else { + return state; + } + } + + case RECEIVE_LOCATION: { + log('Payload RECEIVE_LOCATION', action); + if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) { + const index = state.dataset.locations.findIndex((location) => location.uuid === action.payload.location.uuid); + return update(state, { + dataset: { locations: { [index]: { $set: action.payload.location } } }, + }); + } else { + return state; + } + } + + default: + return state; + } } export default datasetsDashboard; diff --git a/src/descriptorlists/reducers/dashboard.ts b/src/descriptorlists/reducers/dashboard.ts index 4222d88..d856fcf 100644 --- a/src/descriptorlists/reducers/dashboard.ts +++ b/src/descriptorlists/reducers/dashboard.ts @@ -5,122 +5,124 @@ import { DescriptorList } from 'model/descriptor.model'; import { Page } from 'model/common.model'; import { - DASHBOARD_GET_DESCRIPTORLIST, - CREATE_DESCRIPTORLIST, - DASHBOARD_REMOVE_DESCRIPTORLIST, - RECEIVE_DESCRIPTORLIST, - DASHBOARD_RECEIVE_DESCRIPTORLISTS, - DASHBOARD_LIST_DESCRIPTORLISTS, - DASHBOARD_APPEND_DESCRIPTORLISTS, + DASHBOARD_GET_DESCRIPTORLIST, + CREATE_DESCRIPTORLIST, + DASHBOARD_REMOVE_DESCRIPTORLIST, + RECEIVE_DESCRIPTORLIST, + DASHBOARD_RECEIVE_DESCRIPTORLISTS, + DASHBOARD_LIST_DESCRIPTORLISTS, + DASHBOARD_APPEND_DESCRIPTORLISTS, } from 'descriptors/constants'; import { LOGIN_USER, LOGIN_APP, LOGOUT } from 'constants/login'; const INITIAL_STATE: { - descriptorList: DescriptorList; - paged: Page; - pagedQuery: any; - loading: boolean; + descriptorList: DescriptorList; + paged: Page; + pagedQuery: any; + loading: boolean; } = { - descriptorList: null, - paged: null, - pagedQuery: null, - loading: null, + descriptorList: null, + paged: null, + pagedQuery: null, + loading: null, }; function descriptorListsDashboard(state = INITIAL_STATE, action: { type: string, payload?: any } = { type: '' }) { - switch (action.type) { - case LOGIN_USER: - case LOGIN_APP: - case LOGOUT: { - return update(state, { $set: INITIAL_STATE }); - } - - case DASHBOARD_GET_DESCRIPTORLIST: { - return update(state, { - loading: { $set: { uuid: action.payload } }, - }); - } + switch (action.type) { + case LOGIN_USER: + case LOGIN_APP: + case LOGOUT: { + return update(state, { $set: INITIAL_STATE }); + } - case DASHBOARD_LIST_DESCRIPTORLISTS: { - return update(state, { - loading: { $set: { uuid: null, ...action.payload } }, - }); - } + case DASHBOARD_GET_DESCRIPTORLIST: { + return update(state, { + loading: { $set: { uuid: action.payload } }, + }); + } - // set the currentDescriptorList to whatever came in - case RECEIVE_DESCRIPTORLIST: { - const receivedIndex = state.paged ? _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid) : -1; + case DASHBOARD_LIST_DESCRIPTORLISTS: { + return update(state, { + loading: { $set: { uuid: null, ...action.payload } }, + }); + } - if (receivedIndex !== -1) { - return update(state, { - descriptorList: { $set: action.payload }, - loading: { $set: null }, - paged: { - content: { - [receivedIndex]: {$set: action.payload}, - }, - }, - }); - } else { - return update(state, { - descriptorList: { $set: action.payload }, - loading: { $set: null }, - paged: {$set: null}, - }); - } - } - case DASHBOARD_REMOVE_DESCRIPTORLIST: { - if (state.paged) { - const removeIndex = _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid); - return removeIndex === -1 ? state - : update(state, { - paged: { - content: {$splice: [[removeIndex, 1]]}, - numberOfElements: {$set: state.paged.numberOfElements - 1}, - totalElements: {$set: state.paged.totalElements - 1}, - }, - }); - } - return state; - } + // set the currentDescriptorList to whatever came in + case RECEIVE_DESCRIPTORLIST: { + const receivedIndex = state.paged ? _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid) : -1; - // set the currentDescriptorList to a blank new object - case CREATE_DESCRIPTORLIST: { - return update(state, { - descriptorList: { $set: new DescriptorList() }, - }); - } + if (receivedIndex !== -1) { + return update(state, { + descriptorList: { $set: action.payload }, + loading: { $set: null }, + paged: { + content: { + [receivedIndex]: { $set: action.payload }, + }, + }, + }); + } else { + return update(state, { + descriptorList: { $set: action.payload }, + loading: { $set: null }, + paged: { $set: null }, + }); + } + } + case DASHBOARD_REMOVE_DESCRIPTORLIST: { + if (state.paged) { + const removeIndex = _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid); + return removeIndex === -1 ? state + : update(state, { + paged: { + content: { $splice: [[removeIndex, 1]] }, + numberOfElements: { $set: state.paged.numberOfElements - 1 }, + totalElements: { $set: state.paged.totalElements - 1 }, + }, + }); + } + return state; + } - // set the paged to whatever came in - case DASHBOARD_RECEIVE_DESCRIPTORLISTS: { - return update(state, { - loading: { $set: null }, - paged: { $set: action.payload.paged }, - pagedQuery: { $set: action.payload.query }, - }); - } + // set the currentDescriptorList to a blank new object + case CREATE_DESCRIPTORLIST: { + const descriptorList: DescriptorList = new DescriptorList(); + descriptorList.versionTag = '1.0'; + return update(state, { + descriptorList: { $set: descriptorList }, + }); + } - case DASHBOARD_APPEND_DESCRIPTORLISTS: { - const {paged, query} = action.payload; + // set the paged to whatever came in + case DASHBOARD_RECEIVE_DESCRIPTORLISTS: { + return update(state, { + loading: { $set: null }, + paged: { $set: action.payload.paged }, + pagedQuery: { $set: action.payload.query }, + }); + } - return !state.paged ? update(state, { - paged: { $set: paged }, - pagedQuery: { $set: query }, - }) : - update(state, { - paged: { - content: {$push: paged.content}, - number: {$set: paged.number}, - last: {$set: paged.last}, - }, - pagedQuery: { $set: query }, - }); - } + case DASHBOARD_APPEND_DESCRIPTORLISTS: { + const { paged, query } = action.payload; - default: - return state; + return !state.paged ? update(state, { + paged: { $set: paged }, + pagedQuery: { $set: query }, + }) : + update(state, { + paged: { + content: { $push: paged.content }, + number: { $set: paged.number }, + last: { $set: paged.last }, + }, + pagedQuery: { $set: query }, + }); } + + default: + return state; + } } export default descriptorListsDashboard; diff --git a/src/descriptors/reducers/dashboard.ts b/src/descriptors/reducers/dashboard.ts index d1372f4..d04d292 100644 --- a/src/descriptors/reducers/dashboard.ts +++ b/src/descriptors/reducers/dashboard.ts @@ -6,128 +6,130 @@ import { IReducerAction, Page } from 'model/common.model'; import { Descriptor } from 'model/descriptor.model'; import { - CREATE_DESCRIPTOR, - RECEIVE_DESCRIPTOR, - DASHBOARD_RECEIVE_DESCRIPTOR_EXTRA, - DASHBOARD_RECEIVE_DESCRIPTOR_PAGE, - DASHBOARD_GET_DESCRIPTOR, - DASHBOARD_LIST_DESCRIPTORS, - DASHBOARD_REMOVE_DESCRIPTOR, - DASHBOARD_APPEND_DESCRIPTOR_PAGE, + CREATE_DESCRIPTOR, + RECEIVE_DESCRIPTOR, + DASHBOARD_RECEIVE_DESCRIPTOR_EXTRA, + DASHBOARD_RECEIVE_DESCRIPTOR_PAGE, + DASHBOARD_GET_DESCRIPTOR, + DASHBOARD_LIST_DESCRIPTORS, + DASHBOARD_REMOVE_DESCRIPTOR, + DASHBOARD_APPEND_DESCRIPTOR_PAGE, } from 'descriptors/constants'; const INITIAL_STATE: { - descriptor: Descriptor; - descriptorExtra: any; - paged: Page; - pagedQuery: any; - loading: boolean; + descriptor: Descriptor; + descriptorExtra: any; + paged: Page; + pagedQuery: any; + loading: boolean; } = { - descriptor: null, - descriptorExtra: null, - paged: null, - pagedQuery: null, - loading: null, + descriptor: null, + descriptorExtra: null, + paged: null, + pagedQuery: null, + loading: null, }; function descriptorsDashboard(state = INITIAL_STATE, action: IReducerAction) { - switch (action.type) { - case DASHBOARD_GET_DESCRIPTOR: { - return update(state, { - loading: { $set: { uuid: action.payload } }, - }); - } - - case DASHBOARD_LIST_DESCRIPTORS: { - return update(state, { - loading: { $set: { uuid: null, ...action.payload } }, - }); - } - - case CREATE_DESCRIPTOR: { - return update(state, { - descriptor: { $set: new Descriptor() }, - descriptorExtra: { $set: null }, - }); - } - - case RECEIVE_DESCRIPTOR: { - const receivedIndex = state.paged ? _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid) : -1; - - if (receivedIndex !== -1) { - return update(state, { - descriptor: { $set: action.payload }, - descriptorExtra: { $set: null }, - loading: { $set: null }, - paged: { - content: { - [receivedIndex]: {$set: action.payload}, - }, - }, - }); - } else { - return update(state, { - descriptor: { $set: action.payload }, - descriptorExtra: { $set: null }, - loading: { $set: null }, - paged: {$set: null}, - }); - } - } - case DASHBOARD_REMOVE_DESCRIPTOR: { - if (state.paged) { - const removeIndex = _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid); - return removeIndex === -1 ? state - : update(state, { - paged: { - content: {$splice: [[removeIndex, 1]]}, - numberOfElements: {$set: state.paged.numberOfElements - 1}, - totalElements: {$set: state.paged.totalElements - 1}, - }, - }); - } - return state; - } - - case DASHBOARD_RECEIVE_DESCRIPTOR_EXTRA: { - log(DASHBOARD_RECEIVE_DESCRIPTOR_EXTRA, action); - return update(state, { - descriptor: { $set: action.payload.descriptor }, - descriptorExtra: { $set: action.payload.extra }, - loading: { $set: null }, - }); - } - - case DASHBOARD_RECEIVE_DESCRIPTOR_PAGE: { - return update(state, { - loading: { $set: null }, - paged: { $set: action.payload.paged }, - pagedQuery: { $set: _.cloneDeep(action.payload.query) }, - }); - } - - case DASHBOARD_APPEND_DESCRIPTOR_PAGE: { - const {paged, query} = action.payload; - - return !state.paged ? update(state, { - paged: { $set: paged }, - pagedQuery: { $set: query }, - }) : - update(state, { - paged: { - content: {$push: paged.content}, - number: {$set: paged.number}, - last: {$set: paged.last}, - }, - pagedQuery: { $set: query }, - }); - } - - default: - return state; + switch (action.type) { + case DASHBOARD_GET_DESCRIPTOR: { + return update(state, { + loading: { $set: { uuid: action.payload } }, + }); } + + case DASHBOARD_LIST_DESCRIPTORS: { + return update(state, { + loading: { $set: { uuid: null, ...action.payload } }, + }); + } + + case CREATE_DESCRIPTOR: { + const descriptor: Descriptor = new Descriptor(); + descriptor.versionTag = '1.0'; + return update(state, { + descriptor: { $set: descriptor }, + descriptorExtra: { $set: null }, + }); + } + + case RECEIVE_DESCRIPTOR: { + const receivedIndex = state.paged ? _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid) : -1; + + if (receivedIndex !== -1) { + return update(state, { + descriptor: { $set: action.payload }, + descriptorExtra: { $set: null }, + loading: { $set: null }, + paged: { + content: { + [receivedIndex]: { $set: action.payload }, + }, + }, + }); + } else { + return update(state, { + descriptor: { $set: action.payload }, + descriptorExtra: { $set: null }, + loading: { $set: null }, + paged: { $set: null }, + }); + } + } + case DASHBOARD_REMOVE_DESCRIPTOR: { + if (state.paged) { + const removeIndex = _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid); + return removeIndex === -1 ? state + : update(state, { + paged: { + content: { $splice: [[removeIndex, 1]] }, + numberOfElements: { $set: state.paged.numberOfElements - 1 }, + totalElements: { $set: state.paged.totalElements - 1 }, + }, + }); + } + return state; + } + + case DASHBOARD_RECEIVE_DESCRIPTOR_EXTRA: { + log(DASHBOARD_RECEIVE_DESCRIPTOR_EXTRA, action); + return update(state, { + descriptor: { $set: action.payload.descriptor }, + descriptorExtra: { $set: action.payload.extra }, + loading: { $set: null }, + }); + } + + case DASHBOARD_RECEIVE_DESCRIPTOR_PAGE: { + return update(state, { + loading: { $set: null }, + paged: { $set: action.payload.paged }, + pagedQuery: { $set: _.cloneDeep(action.payload.query) }, + }); + } + + case DASHBOARD_APPEND_DESCRIPTOR_PAGE: { + const { paged, query } = action.payload; + + return !state.paged ? update(state, { + paged: { $set: paged }, + pagedQuery: { $set: query }, + }) : + update(state, { + paged: { + content: { $push: paged.content }, + number: { $set: paged.number }, + last: { $set: paged.last }, + }, + pagedQuery: { $set: query }, + }); + } + + default: + return state; + } } export default descriptorsDashboard; -- GitLab