Skip to content

Commit

Permalink
refactor: remove optimistically_update reducer and change sort_storie…
Browse files Browse the repository at this point in the history
…s reducer name
  • Loading branch information
Guilherme-NL authored and GabrielRMuller committed Aug 12, 2024
1 parent aa6e8ab commit f7f0153
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
3 changes: 1 addition & 2 deletions app/assets/javascripts/actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default keyMirror({
RECEIVE_HISTORY_ERROR: null,
CLOSE_HISTORY: null,
UPDATE_STORY_SUCCESS: null,
SORT_STORIES_SUCCESS: null,
SORT_STORIES: null,
STORY_FAILURE: null,
SET_LOADING_STORY: null,
ADD_TASK: null,
Expand All @@ -45,5 +45,4 @@ export default keyMirror({
TOGGLE_COLUMN_VISIBILITY: null,
REVERT_TO_CALCULATED_VELOCITY: null,
SIMULATE_SPRINT_VELOCITY: null,
OPTIMISTICALLY_UPDATE: null,
});
27 changes: 16 additions & 11 deletions app/assets/javascripts/actions/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,12 @@ export const updateStorySuccess = (story, from) => ({
from,
});

export const sortStoriesSuccess = (stories, from) => ({
type: actionTypes.SORT_STORIES_SUCCESS,
export const sortStories = (stories, from) => ({
type: actionTypes.SORT_STORIES,
stories,
from,
});

export const optimisticallyUpdate = (story, from) => ({
type: actionTypes.OPTIMISTICALLY_UPDATE,
story,
from,
});

export const storyFailure = (id, error, from) => ({
type: actionTypes.STORY_FAILURE,
id,
Expand Down Expand Up @@ -227,13 +221,20 @@ export const dragDropStory =

const newStory = Story.addNewAttributes(story, newAttributes);

const storiesWithUpdatedPositions = Story.sortOptimistically(
stories,
newStory,
from
);

try {
dispatch(optimisticallyUpdate(newStory, from));
// Optimistic Update:
dispatch(sortStories(storiesWithUpdatedPositions, from));

const updatedStories = await Story.updatePosition(newStory);

await wait(300);
return dispatch(sortStoriesSuccess(updatedStories, from));
return dispatch(sortStories(updatedStories, from));
} catch (error) {
dispatch(sendErrorNotification(error));
return dispatch(storyFailure(story.id, error, from));
Expand All @@ -256,7 +257,11 @@ export const saveStory =
Story.needConfirmation,
{
onConfirmed: async () => {
const newStory = await Story.post(story._editing, projectId);
const newPosition = Story.getHighestNewPosition(
storiesWithScope(stories, from)
);
const storyWithNewPosition = { ...story._editing, newPosition };
const newStory = await Story.post(storyWithNewPosition, projectId);
dispatch(
addStory({ story: newStory, id: story._editing.id }, from)
);
Expand Down
16 changes: 1 addition & 15 deletions app/assets/javascripts/reducers/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const storiesReducer = (state = initialState, action) => {
);
})
);
case actionTypes.SORT_STORIES_SUCCESS:
case actionTypes.SORT_STORIES:
return normalizeAllScopes(
allScopes(state, null, stories => {
return stories.map(story => {
Expand All @@ -152,20 +152,6 @@ const storiesReducer = (state = initialState, action) => {
});
})
);
case actionTypes.OPTIMISTICALLY_UPDATE:
return normalizeAllScopes(
allScopes(state, action.story.id, stories => {
return stories.map(
updateIfSameId(action.story.id, story => {
const newStory = setLoadingValue(action.story, true);
return addNewAttributes(story, {
...newStory,
needsToSave: false,
});
})
);
})
);
case actionTypes.STORY_FAILURE:
return {
...state,
Expand Down

0 comments on commit f7f0153

Please sign in to comment.