Skip to content
Snippets Groups Projects
Commit 9e1744f2 authored by Ben Wheeler's avatar Ben Wheeler
Browse files

made project-state reducer use string projectId consistently

parent 8c777a6a
Branches
Tags
No related merge requests found
......@@ -42,7 +42,7 @@ const HashParserHOC = function (WrappedComponent) {
handleHashChange () {
const hashMatch = window.location.hash.match(/#(\d+)/);
const hashProjectId = hashMatch === null ? defaultProjectId : hashMatch[1];
this.props.setProjectId(hashProjectId);
this.props.setProjectId(hashProjectId.toString());
if (hashProjectId !== defaultProjectId) {
this.setState({hideIntro: true});
}
......
......@@ -40,7 +40,7 @@ const ProjectFetcherHOC = function (WrappedComponent) {
props.projectId !== null &&
typeof props.projectId !== 'undefined'
) {
this.props.setProjectId(props.projectId);
this.props.setProjectId(props.projectId.toString());
}
}
componentDidUpdate (prevProps) {
......
......@@ -39,7 +39,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
if (this.props.isCreating && !prevProps.isCreating) {
this.storeProject()
.then(response => {
this.props.onCreated(response.id);
this.props.onCreated(response.id.toString());
})
.catch(err => {
// NOTE: should throw up a notice for user
......
......@@ -17,7 +17,7 @@ const START_LOADING_VM_FILE_UPLOAD = 'scratch-gui/project-state/START_LOADING_FI
const START_SAVING = 'scratch-gui/project-state/START_SAVING';
const START_SAVING_BEFORE_CREATING_NEW = 'scratch-gui/project-state/START_SAVING_BEFORE_CREATING_NEW';
const defaultProjectId = 0; // hardcoded id of default project
const defaultProjectId = '0'; // hardcoded id of default project
const LoadingState = keyMirror({
NOT_LOADED: null,
......@@ -160,25 +160,22 @@ const reducer = function (state, action) {
}
return state;
case SET_PROJECT_ID:
// if we were already showing a project, and a different projectId is set, only fetch that project if:
// * new projectId can be parsed into a valid number; and
// * the project id has changed.
// This prevents re-fetching projects unnecessarily.
if (state.loadingState === LoadingState.SHOWING_WITH_ID) {
// use parseInt to compare because parseInt(null) !== parseInt(0),
// and parseInt(1) === parseInt('1').
if (!isNaN(parseInt(action.id, 10)) &&
(parseInt(state.projectId, 10) !== parseInt(action.id, 10))) {
// if we were already showing a project, and a different projectId is set, only fetch that project if
// projectId is a valid id, and projectId has changed. This prevents re-fetching projects unnecessarily.
if (action.id !== null && action.id !== '' && typeof action.id !== 'undefined') {
if (state.loadingState === LoadingState.SHOWING_WITH_ID) {
if (state.projectId !== action.id) {
return Object.assign({}, state, {
loadingState: LoadingState.FETCHING_WITH_ID,
projectId: action.id
});
}
} else { // allow any other states to transition to fetching project
return Object.assign({}, state, {
loadingState: LoadingState.FETCHING_WITH_ID,
projectId: action.id
});
}
} else { // allow any other states to transition to fetching project
return Object.assign({}, state, {
loadingState: LoadingState.FETCHING_WITH_ID,
projectId: action.id
});
}
return state;
case START_FETCHING_NEW_WITHOUT_SAVING:
......
......@@ -34,7 +34,7 @@ test('onCreated with projectId type string shows project with that id', () => {
expect(resultState.projectId).toBe('100');
});
test('onCreated with projectId type number shows project with that id', () => {
test('onCreated with projectId type number shows project with id of type number', () => {
const initialState = {
projectId: null,
loadingState: LoadingState.CREATING_NEW
......@@ -123,7 +123,7 @@ test('onUpdated with id shows with id', () => {
expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
});
test('onUpdated with id, before new fetches default project', () => {
test('onUpdated with id, before new, fetches default project', () => {
const initialState = {
loadingState: LoadingState.SAVING_WITH_ID_BEFORE_NEW
};
......@@ -132,40 +132,40 @@ test('onUpdated with id, before new fetches default project', () => {
expect(resultState.loadingState).toBe(LoadingState.FETCHING_NEW_DEFAULT_TO_SAVE);
});
test('setProjectId with same id as before, should show with id, not fetch', () => {
test('setProjectId, with same id as before, should show with id, not fetch', () => {
const initialState = {
projectId: 100,
projectId: '100',
loadingState: LoadingState.SHOWING_WITH_ID
};
const action = setProjectId(100);
const action = setProjectId('100');
const resultState = projectStateReducer(initialState, action);
expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
expect(resultState.projectId).toBe(100);
expect(resultState.projectId).toBe('100');
});
test('setProjectId with different id as before, should fetch with id, not show with id', () => {
test('setProjectId, with different id as before, should fetch with id, not show with id', () => {
const initialState = {
projectId: 99,
loadingState: LoadingState.SHOWING_WITH_ID
};
const action = setProjectId(100);
const action = setProjectId('100');
const resultState = projectStateReducer(initialState, action);
expect(resultState.loadingState).toBe(LoadingState.FETCHING_WITH_ID);
expect(resultState.projectId).toBe(100);
expect(resultState.projectId).toBe('100');
});
test('setProjectId with same id as before, but not same type, should show with id, not fetch', () => {
test('setProjectId, with same id as before, but not same type, should fetch because not ===', () => {
const initialState = {
projectId: '100',
loadingState: LoadingState.SHOWING_WITH_ID
};
const action = setProjectId(100);
const resultState = projectStateReducer(initialState, action);
expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
expect(resultState.projectId).toBe('100');
expect(resultState.loadingState).toBe(LoadingState.FETCHING_WITH_ID);
expect(resultState.projectId).toBe(100);
});
test('setProjectId provided a null id should show with id, not fetch', () => {
test('setProjectId, provided a null id, should not change state', () => {
const initialState = {
projectId: '100',
loadingState: LoadingState.SHOWING_WITH_ID
......@@ -176,7 +176,7 @@ test('setProjectId provided a null id should show with id, not fetch', () => {
expect(resultState.projectId).toBe('100');
});
test('requestNewProject when can\'t save should fetch default project without id', () => {
test('requestNewProject, when can\'t save, should fetch default project without id', () => {
const initialState = {
loadingState: LoadingState.SHOWING_WITHOUT_ID
};
......@@ -185,7 +185,7 @@ test('requestNewProject when can\'t save should fetch default project without id
expect(resultState.loadingState).toBe(LoadingState.FETCHING_NEW_DEFAULT);
});
test('requestNewProject when can save, should save and prepare to fetch default project', () => {
test('requestNewProject, when can save, should save and prepare to fetch default project', () => {
const initialState = {
loadingState: LoadingState.SHOWING_WITH_ID
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment