Skip to content

Commit

Permalink
Merge pull request #42702 from code-dot-org/ben/code-review/create-co…
Browse files Browse the repository at this point in the history
…de-review-data-api-class

Code Review: move data API into class
  • Loading branch information
bencodeorg committed Sep 28, 2021
2 parents b425084 + 8600854 commit 2051aa6
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 127 deletions.
58 changes: 22 additions & 36 deletions apps/src/templates/instructions/ReviewTab.jsx
Expand Up @@ -11,7 +11,7 @@ import {currentLocation, navigateToHref} from '@cdo/apps/utils';
import {ViewType} from '@cdo/apps/code-studio/viewAsRedux';
import Comment from './codeReview/Comment';
import CommentEditor from './codeReview/CommentEditor';
import * as codeReviewDataApi from './codeReview/codeReviewDataApi';
import CodeReviewDataApi from './codeReview/CodeReviewDataApi';
import PeerSelectDropdown from './codeReview/PeerSelectDropdown';

export const VIEWING_CODE_REVIEW_URL_PARAM = 'viewingCodeReview';
Expand Down Expand Up @@ -42,7 +42,8 @@ class ReviewTab extends Component {
projectOwnerName: '',
authorizationError: false,
commentSaveError: false,
commentSaveInProgress: false
commentSaveInProgress: false,
dataApi: {}
};

onSelectPeer = peer => {
Expand Down Expand Up @@ -78,11 +79,17 @@ class ReviewTab extends Component {
return;
}

this.dataApi = new CodeReviewDataApi(
channelId,
serverLevelId,
serverScriptId
);

const initialLoadPromises = [];

initialLoadPromises.push(
codeReviewDataApi
.getCodeReviewCommentsForProject(channelId)
this.dataApi
.getCodeReviewCommentsForProject()
.done((data, _, request) => {
this.setState({
comments: data,
Expand All @@ -92,8 +99,8 @@ class ReviewTab extends Component {
);

initialLoadPromises.push(
codeReviewDataApi
.getPeerReviewStatus(channelId, serverLevelId, serverScriptId)
this.dataApi
.getPeerReviewStatus()
.done(data => {
const id = (data && data.id) || null;
this.setState({
Expand All @@ -116,8 +123,8 @@ class ReviewTab extends Component {
this.props.viewAs !== ViewType.Teacher
) {
initialLoadPromises.push(
codeReviewDataApi
.getReviewablePeers(channelId, serverLevelId, serverScriptId)
this.dataApi
.getReviewablePeers()
.done(data => this.setState({reviewablePeers: data}))
.fail(() => {
this.setState({
Expand Down Expand Up @@ -146,25 +153,14 @@ class ReviewTab extends Component {
};

onNewCommentSubmit = commentText => {
const {
channelId,
serverScriptId,
serverLevelId
} = getStore().getState().pageConstants;
const {token} = this.state;
this.setState({
commentSaveError: false,
commentSaveInProgress: true
});

codeReviewDataApi
.submitNewCodeReviewComment(
commentText,
channelId,
serverScriptId,
serverLevelId,
token
)
this.dataApi
.submitNewCodeReviewComment(commentText, token)
.done(newComment => {
const comments = this.state.comments;
comments.push(newComment);
Expand All @@ -190,7 +186,7 @@ class ReviewTab extends Component {
onCommentDelete = deletedCommentId => {
const {token} = this.state;

codeReviewDataApi
this.dataApi
.deleteCodeReviewComment(deletedCommentId, token)
.done(() => {
const comments = [...this.state.comments];
Expand All @@ -204,7 +200,7 @@ class ReviewTab extends Component {
onCommentResolveStateToggle = (resolvedCommentId, newResolvedStatus) => {
const {token} = this.state;

codeReviewDataApi
this.dataApi
.resolveCodeReviewComment(resolvedCommentId, newResolvedStatus, token)
.done(() => {
const comments = [...this.state.comments];
Expand Down Expand Up @@ -287,18 +283,8 @@ class ReviewTab extends Component {
});

if (isReadyForReview) {
const {
channelId,
serverLevelId,
serverScriptId
} = getStore().getState().pageConstants;
codeReviewDataApi
.enablePeerReview(
channelId,
serverLevelId,
serverScriptId,
this.state.token
)
this.dataApi
.enablePeerReview(this.state.token)
.done(data => {
this.setState({
reviewableProjectId: data.id,
Expand All @@ -315,7 +301,7 @@ class ReviewTab extends Component {
});
});
} else {
codeReviewDataApi
this.dataApi
.disablePeerReview(this.state.reviewableProjectId, this.state.token)
.done(() => {
this.setState({
Expand Down
93 changes: 93 additions & 0 deletions apps/src/templates/instructions/codeReview/CodeReviewDataApi.js
@@ -0,0 +1,93 @@
import $ from 'jquery';

export default class CodeReviewDataApi {
constructor(channelId, levelId, scriptId) {
this.channelId = channelId;
this.levelId = levelId;
this.scriptId = scriptId;
}

getCodeReviewCommentsForProject() {
return $.ajax({
url: `/code_review_comments/project_comments`,
method: 'GET',
data: {channel_id: this.channelId}
});
}

getPeerReviewStatus() {
return $.ajax({
url: `/reviewable_projects/reviewable_status`,
type: 'GET',
data: {
channel_id: this.channelId,
level_id: this.levelId,
script_id: this.scriptId
}
});
}

getReviewablePeers() {
return $.ajax({
url: `/reviewable_projects/for_level`,
type: 'GET',
data: {
channel_id: this.channelId,
level_id: this.levelId,
script_id: this.scriptId
}
});
}

submitNewCodeReviewComment(commentText, token) {
return $.ajax({
url: `/code_review_comments`,
type: 'POST',
headers: {'X-CSRF-Token': token},
data: {
channel_id: this.channelId,
script_id: this.scriptId,
level_id: this.levelId,
comment: commentText
}
});
}

resolveCodeReviewComment(commentId, resolvedStatus, token) {
return $.ajax({
url: `/code_review_comments/${commentId}/toggle_resolved`,
type: 'PATCH',
headers: {'X-CSRF-Token': token},
data: {is_resolved: resolvedStatus}
});
}

deleteCodeReviewComment(commentId, token) {
return $.ajax({
url: `/code_review_comments/${commentId}`,
type: 'DELETE',
headers: {'X-CSRF-Token': token}
});
}

enablePeerReview(token) {
return $.ajax({
url: `/reviewable_projects`,
type: 'POST',
headers: {'X-CSRF-Token': token},
data: {
channel_id: this.channelId,
level_id: this.levelId,
script_id: this.scriptId
}
});
}

disablePeerReview(projectId, token) {
return $.ajax({
url: `/reviewable_projects/${projectId}`,
headers: {'X-CSRF-Token': token},
method: `DELETE`
});
}
}
91 changes: 0 additions & 91 deletions apps/src/templates/instructions/codeReview/codeReviewDataApi.js

This file was deleted.

0 comments on commit 2051aa6

Please sign in to comment.