Commit 54ce704a authored by Matija Obreza's avatar Matija Obreza
Browse files

Reset app state on login/logout

parent 49678ac6
......@@ -2,6 +2,7 @@ import * as update from 'immutability-helper';
import {log} from 'utilities/debug';
import { CREATE_DATASET, RECEIVE_DATASET, RECEIVE_DATASET_PAGE, ADD_CREATOR_TO_DATASET, REMOVE_CREATOR_FROM_DATASET, UPDATE_DATASET_CREATOR, ADD_LOCATION, RECEIVE_LOCATION, REMOVE_LOCATION } from 'constants/datasets';
import { LOGIN_USER, LOGIN_APP, LOGOUT } from 'constants/login';
import { Dataset } from 'model/dataset.model';
......@@ -14,6 +15,12 @@ const INITIAL_STATE = {
function datasets(state = INITIAL_STATE, action: { type?: string, payload?: any } = { type: '', payload: {} }) {
switch (action.type) {
case LOGIN_USER:
case LOGIN_APP:
case LOGOUT: {
return update(state, { $set: INITIAL_STATE });
}
case CREATE_DATASET: {
return update(state, {
currentDataset: { $set: new Dataset() },
......
......@@ -4,6 +4,7 @@ import { DescriptorList } from 'model/descriptor.model';
import { Page } from 'model/common.model';
import { GET_DESCRIPTORLIST, RECEIVE_DESCRIPTORLIST, CREATE_DESCRIPTORLIST, LIST_DESCRIPTORLISTS, RECEIVE_DESCRIPTORLISTS } from 'constants/descriptors';
import { LOGIN_USER, LOGIN_APP, LOGOUT } from 'constants/login';
const INITIAL_STATE: {
currentDescriptorList: DescriptorList;
......@@ -20,6 +21,12 @@ const INITIAL_STATE: {
function descriptorList(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 GET_DESCRIPTORLIST: {
return update(state, {
loading: { $set: { uuid: action.payload } },
......
import * as update from 'immutability-helper';
import * as _ from 'lodash';
import {log} from 'utilities/debug';
import { log } from 'utilities/debug';
import {IReducerAction, Page} from 'model/common.model';
import {Descriptor} from 'model/descriptor.model';
import { IReducerAction, Page } from 'model/common.model';
import { Descriptor } from 'model/descriptor.model';
import {
CREATE_DESCRIPTOR, RECEIVE_DESCRIPTOR, RECEIVE_DESCRIPTOR_EXTRA, RECEIVE_DESCRIPTOR_PAGE, GET_DESCRIPTOR, LIST_DESCRIPTORS,
} from 'constants/descriptors';
import { CREATE_DESCRIPTOR, RECEIVE_DESCRIPTOR, RECEIVE_DESCRIPTOR_EXTRA, RECEIVE_DESCRIPTOR_PAGE, GET_DESCRIPTOR, LIST_DESCRIPTORS } from 'constants/descriptors';
import { LOGIN_USER, LOGIN_APP, LOGOUT } from 'constants/login';
const INITIAL_STATE: {
currentDescriptor: Descriptor;
currentDescriptorExtra: any;
paged: Page<Descriptor>;
pagedQuery: any;
loading: boolean;
currentDescriptor: Descriptor;
currentDescriptorExtra: any;
paged: Page<Descriptor>;
pagedQuery: any;
loading: boolean;
} = {
currentDescriptor: null,
currentDescriptorExtra: null,
paged: null,
pagedQuery: null,
loading: null,
};
};
function descriptors(state = INITIAL_STATE, action: IReducerAction) {
switch (action.type) {
case GET_DESCRIPTOR: {
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 LIST_DESCRIPTORS: {
return update(state, {
loading: { $set: { uuid: null, ...action.payload } },
});
}
case GET_DESCRIPTOR: {
return update(state, {
loading: { $set: { uuid: action.payload } },
});
}
case CREATE_DESCRIPTOR: {
return update(state, {
currentDescriptor: {$set: new Descriptor()},
currentDescriptorExtra: {$set: null},
});
}
case LIST_DESCRIPTORS: {
return update(state, {
loading: { $set: { uuid: null, ...action.payload } },
});
}
case RECEIVE_DESCRIPTOR: {
log(RECEIVE_DESCRIPTOR, action);
return update(state, {
currentDescriptor: {$set: action.payload},
currentDescriptorExtra: {$set: null},
loading: {$set: null},
});
}
case CREATE_DESCRIPTOR: {
return update(state, {
currentDescriptor: { $set: new Descriptor() },
currentDescriptorExtra: { $set: null },
});
}
case RECEIVE_DESCRIPTOR_EXTRA: {
log(RECEIVE_DESCRIPTOR, action);
return update(state, {
currentDescriptor: {$set: action.payload.descriptor},
currentDescriptorExtra: {$set: action.payload.extra},
loading: {$set: null},
});
}
case RECEIVE_DESCRIPTOR: {
log(RECEIVE_DESCRIPTOR, action);
return update(state, {
currentDescriptor: { $set: action.payload },
currentDescriptorExtra: { $set: null },
loading: { $set: null },
});
}
case RECEIVE_DESCRIPTOR_PAGE: {
return update(state, {
loading: {$set: null},
paged: {$set: action.payload.paged},
pagedQuery: {$set: _.cloneDeep(action.payload.query)},
});
}
case RECEIVE_DESCRIPTOR_EXTRA: {
log(RECEIVE_DESCRIPTOR, action);
return update(state, {
currentDescriptor: { $set: action.payload.descriptor },
currentDescriptorExtra: { $set: action.payload.extra },
loading: { $set: null },
});
}
default:
return state;
case RECEIVE_DESCRIPTOR_PAGE: {
return update(state, {
loading: { $set: null },
paged: { $set: action.payload.paged },
pagedQuery: { $set: _.cloneDeep(action.payload.query) },
});
}
default:
return state;
}
}
export default descriptors;
Supports Markdown
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