Skip to content

Commit

Permalink
Change .then to async/await on V2
Browse files Browse the repository at this point in the history
  • Loading branch information
kostadriano committed Mar 13, 2019
1 parent d4c347f commit 75c36d3
Show file tree
Hide file tree
Showing 14 changed files with 381 additions and 367 deletions.
13 changes: 9 additions & 4 deletions app/assets/javascripts/actions/attachment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { updateStorySuccess } from './story';
import actionTypes from './actionTypes';
import { storyFailure } from './story';

export const addAttachmentToStory = (storyId, attachment) => ({
type: actionTypes.ADD_ATTACHMENT,
Expand All @@ -14,15 +15,19 @@ export const removeAttachment = (storyId, attachmentId) => ({
});

export const addAttachment = (storyId, projectId, attachment) =>
(dispatch, getState, { Story }) => {
async (dispatch, getState, { Story }) => {
dispatch(addAttachmentToStory(storyId, attachment));

const { stories } = getState();
const story = stories.find(story => story.id === storyId);
const options = { collapse: false };

Story.update(story._editing, projectId, options).then((story) => {
dispatch(updateStorySuccess(story));
})
try {
const result = await Story.update(story._editing, projectId, options);
return updateStorySuccess(result);
}
catch (error) {
return storyFailure(error);
}
}

26 changes: 18 additions & 8 deletions app/assets/javascripts/actions/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,27 @@ export const createNoteSuccess = (storyId, note) => ({
});

export const deleteNote = (projectId, storyId, noteId) =>
(dispatch, getState, { Note }) => {
async (dispatch, getState, { Note }) => {
dispatch(setLoadingStory(storyId));
return Note.destroy(projectId, storyId, noteId)
.then(() => dispatch(deleteNoteSuccess(storyId, noteId)))
.catch((error) => dispatch(storyFailure(storyId, error)))

try {
await Note.destroy(projectId, storyId, noteId);
return dispatch(deleteNoteSuccess(storyId, noteId));
}
catch (error) {
return dispatch(storyFailure(storyId, error));
}
}

export const createNote = (projectId, storyId, note) =>
(dispatch, getState, { Note }) => {
async (dispatch, getState, { Note }) => {
dispatch(setLoadingStory(storyId));
return Note.post(projectId, storyId, note)
.then((note) => dispatch(createNoteSuccess(storyId, note)))
.catch((error) => dispatch(storyFailure(storyId, error)))

try {
const result = await Note.post(projectId, storyId, note);
return dispatch(createNoteSuccess(storyId, result));
}
catch (error) {
return dispatch(storyFailure(storyId, error));
}
}
29 changes: 17 additions & 12 deletions app/assets/javascripts/actions/projectBoard.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@ const receiveProject = data => ({
data
});

export const fetchProjectBoard = projectId => {
return (dispatch, getState, { ProjectBoard }) => {
export const fetchProjectBoard = projectId =>
async (dispatch, getState, { ProjectBoard }) => {
dispatch(requestProjectBoard());

ProjectBoard.get(projectId)
.then(({ project, users, stories, pastIterations }) => {
dispatch(receiveProject(project));
dispatch(receivePastIterations(pastIterations));
dispatch(receiveUsers(users));
dispatch(receiveStories(stories));
dispatch(receiveProjectBoard(projectId));
})
.catch(error => dispatch(errorRequestProjectBoard(error)));
try {
const {
project, users,
stories, pastIterations
} = await ProjectBoard.get(projectId);

dispatch(receiveProject(project));
dispatch(receivePastIterations(pastIterations));
dispatch(receiveUsers(users));
dispatch(receiveStories(stories));
dispatch(receiveProjectBoard(projectId));
}
catch (error) {
return dispatch(errorRequestProjectBoard(error));
}
};
};
43 changes: 24 additions & 19 deletions app/assets/javascripts/actions/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,44 @@ export const updateCollapsedStory = (storyId, projectId, newAttributes) =>
}

export const saveStory = (storyId, projectId, options) =>
(dispatch, getState, { Story }) => {
async (dispatch, getState, { Story }) => {
const { stories } = getState();
const story = stories.find((story) => story.id === storyId);

dispatch(setLoadingStory(story.id))

if (Story.isNew(story)) {
return Story.post(story._editing, projectId)
.then((story) =>
dispatch(addStory(story))
)
.catch((error) => dispatch(storyFailure(story.id, error)));
try {
const result = await Story.post(story._editing, projectId)
return dispatch(addStory(result))
}
catch (error) {
return dispatch(storyFailure(story.id, error))
}
};

if (story._editing._isDirty) {
return Story.update(story._editing, projectId, options)
.then((story) => {
dispatch(updateStorySuccess(story))
})
.catch((error) => dispatch(storyFailure(story.id, error)))
try {
const result = await Story.update(story._editing, projectId, options)
return dispatch(updateStorySuccess(result))
}
catch (error) {
return dispatch(storyFailure(story.id, error))
}
}

return dispatch(toggleStory(story.id));
};

export const deleteStory = (storyId, projectId) =>
(dispatch, getState, { Story }) => {
async (dispatch, getState, { Story }) => {
dispatch(setLoadingStory(storyId))
return Story.deleteStory(storyId, projectId)
.then(() => dispatch(deleteStorySuccess(storyId)))
.catch((error) => {
dispatch(storyFailure(storyId, error))
// TODO: dispatch an action to notify user on error
alert(error.message);
});

try {
await Story.deleteStory(storyId, projectId);
return dispatch(deleteStorySuccess(storyId));
}
catch (error) {
return dispatch(storyFailure(storyId, error))
}
}
57 changes: 32 additions & 25 deletions app/assets/javascripts/actions/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,39 @@ export const toggleTaskSuccess = (task, story) => ({
story
});

export const createTask = (projectId, storyId, task) => {
return (dispatch, getState, { Task }) => {
dispatch(setLoadingStory(storyId))
return Task.post(projectId, storyId, task)
.then(task => {
dispatch(createTaskSuccess(task, storyId))
})
.catch((error) => dispatch(storyFailure(storyId, error)))
export const createTask = (projectId, storyId, task) =>
async (dispatch, getState, { Task }) => {
dispatch(setLoadingStory(storyId));

try {
const result = await Task.post(projectId, storyId, task);
return dispatch(createTaskSuccess(result, storyId));
}
catch (error) {
return dispatch(storyFailure(storyId, error));
}
};
};

export const deleteTask = (projectId, storyId, taskId) => {
return (dispatch, getState, { Task }) => {
dispatch(setLoadingStory(storyId))
return Task.destroy(projectId, storyId, taskId)
.then(() => {
dispatch(deleteTaskSuccess(taskId, storyId))
})
.catch((error) => dispatch(storyFailure(storyId, error)))

export const deleteTask = (projectId, storyId, taskId) =>
async (dispatch, getState, { Task }) => {
dispatch(setLoadingStory(storyId));

try {
await Task.destroy(projectId, storyId, taskId);
return dispatch(deleteTaskSuccess(taskId, storyId));
}
catch (error) {
return dispatch(storyFailure(storyId, error));
}
};
};

export const toggleTask = (projectId, story, task, status) => {
return (dispatch, getState, { Task }) => {
return Task.toggle(projectId, story.id, task.id, status)
.then((task) => dispatch(toggleTaskSuccess(task, story)))
.catch((error) => console.error(error));
export const toggleTask = (projectId, story, task, status) =>
async (dispatch, getState, { Task }) => {
try {
const result = await Task.toggle(projectId, story.id, task.id, status);
return dispatch(toggleTaskSuccess(result, story));
}
catch (error) {
return dispatch(storyFailure(storyId, error));
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import Dropzone from 'react-dropzone';
import AttachmentsList from '../attachment/AttachmentList';
import { upload, acceptedMimeTypes } from '../../../models/beta/fileUpload';
import { upload, acceptedMimeTypes } from '../../../models/beta/attachment';
import { editingStoryPropTypesShape } from '../../../models/beta/story';

class ExpandedStoryAttachments extends React.Component {
Expand Down
24 changes: 20 additions & 4 deletions app/assets/javascripts/models/beta/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ export const destroy = (projectId, storyId, noteId) =>
httpService
.delete(`/projects/${projectId}/stories/${storyId}/notes/${noteId}`);

export const post = (projectId, storyId, note) =>
httpService
.post(`/projects/${projectId}/stories/${storyId}/notes`, { note })
.then(({ data }) => changeCase.camelKeys(data, { recursive: true, arrayRecursive: true }));
export const post = async (projectId, storyId, note) => {
note = serialize(note);

const { data } = await httpService
.post(`/projects/${projectId}/stories/${storyId}/notes`, { note });

return deserialize(data);
}

export const addNote = (story, note) => ({
...story,
Expand Down Expand Up @@ -40,3 +44,15 @@ export const notePropTypesShape = PropTypes.shape({
PropTypes.array
])
});

const deserialize = (data) =>
changeCase.camelKeys(data, {
recursive: true,
arrayRecursive: true
});

const serialize = (data) =>
changeCase.snakeKeys(data, {
recursive: true,
arrayRecursive: true
});
27 changes: 18 additions & 9 deletions app/assets/javascripts/models/beta/projectBoard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import * as Story from './story';
import * as Project from './project';
import PropTypes from 'prop-types';

export function get(projectId) {
return httpService
.get(`/beta/project_boards/${projectId}`)
.then(({ data }) => changeCase.camelKeys(data, { recursive: true, arrayRecursive: true }))
.then((projectBoard) => ({
...projectBoard,
project: Project.deserialize(projectBoard),
stories: projectBoard.stories.map(Story.deserialize)
}));
export const get = async (projectId) => {
const { data } = await httpService
.get(`/beta/project_boards/${projectId}`);

return deserialize(data);
};

export const projectBoardPropTypesShape = PropTypes.shape({
Expand All @@ -22,3 +18,16 @@ export const projectBoardPropTypesShape = PropTypes.shape({
]),
isFetched: PropTypes.bool.isRequired
});

const deserialize = (data) => {
const projectBoard = changeCase.camelKeys(data, {
recursive: true,
arrayRecursive: true
});

return {
...projectBoard,
project: Project.deserialize(projectBoard),
stories: projectBoard.stories.map(Story.deserialize)
}
}
32 changes: 17 additions & 15 deletions app/assets/javascripts/models/beta/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,27 @@ export const states = [
'rejected'
];

export const update = (story, projectId, options) => {
export const update = async (story, projectId, options) => {
const newStory = serialize(story);

return httpService
.put(`/projects/${projectId}/stories/${story.id}`, { story: newStory })
.then(({ data }) => deserialize(data.story, options));
const { data } = await httpService
.put(`/projects/${projectId}/stories/${story.id}`, { story: newStory });

return deserialize(data.story, options);
};

export const post = (story, projectId) => {
export const post = async (story, projectId) => {
const newStory = serialize(story);

return httpService
.post(`/projects/${projectId}/stories`, { story: newStory })
.then(({ data }) => deserialize(data.story));
const { data } = await httpService
.post(`/projects/${projectId}/stories`, { story: newStory });

return deserialize(data.story);
};

export const deleteStory = (storyId, projectId) =>
httpService
.delete(`/projects/${projectId}/stories/${storyId}`)
.delete(`/projects/${projectId}/stories/${storyId}`);

export const updateStory = (story, newAttributes) => ({
...story,
Expand Down Expand Up @@ -254,11 +256,11 @@ const emptyStory = {
};

export const storyTransitions = {
START : 'start',
FINISH : 'finish',
START: 'start',
FINISH: 'finish',
DELIVER: 'deliver',
ACCEPT : 'accept',
REJECT : 'reject',
ACCEPT: 'accept',
REJECT: 'reject',
RESTART: 'restart'
};

Expand All @@ -282,13 +284,13 @@ const stateTransitions = {
[status.REJECTED]: {
[storyTransitions.RESTART]: status.STARTED
},
[status.ACCEPTED] : {}
[status.ACCEPTED]: {}
};

export const getNextState = (currentState, transition) => {
const nextState = stateTransitions[currentState][transition];

if(nextState) {
if (nextState) {
return nextState;
}

Expand Down

0 comments on commit 75c36d3

Please sign in to comment.