Skip to content

Commit 6d44a8c

Browse files
committed
Add unitAPI wrapper to the editor methods
1 parent 3b905e7 commit 6d44a8c

File tree

2 files changed

+89
-92
lines changed

2 files changed

+89
-92
lines changed

pootle/static/js/editor/app.js

Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,15 @@ PTL.editor = {
127127

128128
this.setActiveUnit = debounce((body, newUnit) => {
129129
this.fetchUnits().always(() => {
130-
UnitAPI.fetchUnit(newUnit.id, body)
131-
.then(
132-
(data) => {
133-
this.setEditUnit(data);
134-
this.renderUnit();
135-
},
136-
this.error
137-
);
130+
this.unitAPI({
131+
method: 'fetchUnit',
132+
params: { uId: newUnit.id, body },
133+
success: (data) => {
134+
this.setEditUnit(data);
135+
this.renderUnit();
136+
},
137+
error: this.error,
138+
});
138139
});
139140
}, 250);
140141

@@ -402,22 +403,6 @@ PTL.editor = {
402403
this.unitIndex(e);
403404
});
404405

405-
/* XHR activity indicator */
406-
$(document).ajaxStart(() => {
407-
clearTimeout(this.delayedActivityTimer);
408-
if (this.isLoading) {
409-
return;
410-
}
411-
412-
this.showActivity();
413-
});
414-
$(document).ajaxStop(() => {
415-
clearTimeout(this.delayedActivityTimer);
416-
if (!this.isLoading) {
417-
this.hideActivity();
418-
}
419-
});
420-
421406
/* Load MT providers */
422407
this.settings.mt.forEach((provider) => {
423408
require.ensure([], () => {
@@ -664,6 +649,11 @@ PTL.editor = {
664649
return true;
665650
},
666651

652+
unitAPI({ method, params, success, error, always }) {
653+
this.showActivity();
654+
return UnitAPI[method](params).then(success, error).always(always, () => this.hideActivity());
655+
},
656+
667657

668658
/*
669659
* Text utils
@@ -1490,11 +1480,13 @@ PTL.editor = {
14901480
if (previousUids.length > 0) {
14911481
reqData.previous_uids = previousUids;
14921482
}
1493-
return UnitAPI.fetchUnits(reqData)
1494-
.then(
1495-
(data) => this.storeUnitData(data, { isInitial: initial }),
1496-
this.error
1497-
).always(() => this.markAsFetched(offsetToFetch));
1483+
return this.unitAPI({
1484+
method: 'fetchUnits',
1485+
params: { body: reqData },
1486+
success: (data) => this.storeUnitData(data, { isInitial: initial }),
1487+
error: this.error,
1488+
always: () => this.markAsFetched(offsetToFetch),
1489+
});
14981490
}
14991491
/* eslint-disable new-cap */
15001492
return $.Deferred((deferred) => deferred.reject(false));
@@ -1635,11 +1627,12 @@ PTL.editor = {
16351627
};
16361628
assign(body, suggData);
16371629
}
1638-
UnitAPI.addTranslation(this.units.getCurrent().id, body)
1639-
.then(
1640-
(data) => this.processSubmission(data),
1641-
this.error
1642-
);
1630+
this.unitAPI({
1631+
method: 'addTranslation',
1632+
params: { uId: this.units.getCurrent().id, body },
1633+
success: (data) => this.processSubmission(data),
1634+
error: this.error,
1635+
});
16431636
},
16441637

16451638
processSubmission(data) {
@@ -1674,11 +1667,12 @@ PTL.editor = {
16741667
const body = assign({}, this.getValueStateData(ReactEditor.stateValues),
16751668
this.getReqData(), captchaCallbacks);
16761669

1677-
UnitAPI.addSuggestion(this.units.getCurrent().id, body)
1678-
.then(
1679-
(data) => this.processSuggestion(data),
1680-
this.error
1681-
);
1670+
this.unitAPI({
1671+
method: 'addSuggestion',
1672+
params: { uId: this.units.getCurrent().id, body },
1673+
success: (data) => this.processSuggestion(data),
1674+
error: this.error,
1675+
});
16821676
},
16831677

16841678
processSuggestion() {
@@ -1959,14 +1953,15 @@ PTL.editor = {
19591953

19601954
/* Gets more context units */
19611955
moreContext(amount = CTX_STEP) {
1962-
return (
1963-
UnitAPI.getContext(this.units.getCurrent().id,
1964-
{ gap: this.ctxGap, qty: amount })
1965-
.then(
1966-
(data) => this.handleContextSuccess(data),
1967-
this.error
1968-
)
1969-
);
1956+
return this.unitAPI({
1957+
method: 'getContext',
1958+
params: {
1959+
uId: this.units.getCurrent().id,
1960+
body: { gap: this.ctxGap, qty: amount },
1961+
},
1962+
success: (data) => this.handleContextSuccess(data),
1963+
error: this.error,
1964+
});
19701965
},
19711966

19721967
/* Shrinks context lines */
@@ -2055,11 +2050,12 @@ PTL.editor = {
20552050
e.preventDefault();
20562051
this.updateCommentDefaultProperties();
20572052

2058-
UnitAPI.addComment(this.units.getCurrent().id, $(e.target).serializeObject())
2059-
.then(
2060-
(data) => this.processAddComment(data),
2061-
this.error
2062-
);
2053+
this.unitAPI({
2054+
method: 'addComment',
2055+
params: { uId: this.units.getCurrent().id, body: $(e.target).serializeObject() },
2056+
success: (data) => this.processAddComment(data),
2057+
error: this.error,
2058+
});
20632059
},
20642060

20652061
processAddComment(data) {
@@ -2080,11 +2076,12 @@ PTL.editor = {
20802076
removeComment(e) {
20812077
e.preventDefault();
20822078

2083-
UnitAPI.removeComment(this.units.getCurrent().id)
2084-
.then(
2085-
() => $('.js-comment-first').fadeOut(200),
2086-
this.error
2087-
);
2079+
this.unitAPI({
2080+
method: 'removeComment',
2081+
params: { uId: this.units.getCurrent().id },
2082+
success: () => $('.js-comment-first').fadeOut(200),
2083+
error: this.error,
2084+
});
20882085
},
20892086

20902087

@@ -2100,15 +2097,12 @@ PTL.editor = {
21002097
return;
21012098
}
21022099

2103-
const $node = $('.translate-container');
2104-
$node.spin();
2105-
2106-
UnitAPI.getTimeline(this.units.getCurrent().id)
2107-
.then(
2108-
(data) => this.renderTimeline(data),
2109-
this.error
2110-
)
2111-
.always(() => $node.spin(false));
2100+
this.unitAPI({
2101+
method: 'getTimeline',
2102+
params: { uId: this.units.getCurrent().id },
2103+
success: (data) => this.renderTimeline(data),
2104+
error: this.error,
2105+
});
21122106
},
21132107

21142108
renderTimeline(data) {
@@ -2276,11 +2270,12 @@ PTL.editor = {
22762270
},
22772271

22782272
rejectSuggestion(suggId, { requestData = {} } = {}) {
2279-
UnitAPI.rejectSuggestion(this.units.getCurrent().id, suggId, requestData)
2280-
.then(
2281-
(data) => this.processRejectSuggestion(data, suggId),
2282-
this.error
2283-
);
2273+
this.unitAPI({
2274+
method: 'rejectSuggestion',
2275+
params: { uId: this.units.getCurrent().id, suggId, body: requestData },
2276+
success: (data) => this.processRejectSuggestion(data, suggId),
2277+
error: this.error,
2278+
});
22842279
},
22852280

22862281
processRejectSuggestion(data, suggId) {
@@ -2309,11 +2304,12 @@ PTL.editor = {
23092304
},
23102305

23112306
acceptSuggestion(suggId, { requestData = {}, skipToNext = false } = {}) {
2312-
UnitAPI.acceptSuggestion(this.units.getCurrent().id, suggId, requestData)
2313-
.then(
2314-
(data) => this.processAcceptSuggestion(data, suggId, skipToNext),
2315-
this.error
2316-
);
2307+
this.unitAPI({
2308+
method: 'acceptSuggestion',
2309+
params: { uId: this.units.getCurrent().id, suggId, body: requestData },
2310+
success: (data) => this.processAcceptSuggestion(data, suggId, skipToNext),
2311+
error: this.error,
2312+
});
23172313
},
23182314

23192315
processAcceptSuggestion(data, suggId, skipToNext) {
@@ -2352,11 +2348,12 @@ PTL.editor = {
23522348
const isFalsePositive = $check.hasClass('false-positive');
23532349

23542350
const opts = isFalsePositive ? null : { mute: 1 };
2355-
UnitAPI.toggleCheck(this.units.getCurrent().id, checkId, opts)
2356-
.then(
2357-
() => this.processToggleCheck(checkId, isFalsePositive),
2358-
this.error
2359-
);
2351+
this.unitAPI({
2352+
method: 'toggleCheck',
2353+
params: { uId: this.units.getCurrent().id, checkId, body: opts },
2354+
success: () => this.processToggleCheck(checkId, isFalsePositive),
2355+
error: this.error,
2356+
});
23602357
},
23612358

23622359
processToggleCheck(checkId, isFalsePositive) {

pootle/static/js/shared/api/UnitAPI.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,51 @@ const UnitAPI = {
1414

1515
apiRoot: PTL.unitApiRoot,
1616

17-
fetchUnits(body) {
17+
fetchUnits({ body }) {
1818
return fetch({
1919
body,
2020
url: this.apiRoot,
2121
});
2222
},
2323

24-
fetchUnit(uId, body = {}) {
24+
fetchUnit({ uId, body = {} }) {
2525
return fetch({
2626
body,
2727
queue: 'unitWidget',
2828
url: `${this.apiRoot}${uId}/edit/`,
2929
});
3030
},
3131

32-
addTranslation(uId, body) {
32+
addTranslation({ uId, body }) {
3333
return fetch({
3434
body,
3535
method: 'POST',
3636
url: `${this.apiRoot}${uId}`,
3737
});
3838
},
3939

40-
getContext(uId, body) {
40+
getContext({ uId, body }) {
4141
return fetch({
4242
body,
4343
url: `${this.apiRoot}${uId}/context/`,
4444
});
4545
},
4646

47-
getTimeline(uId) {
47+
getTimeline({ uId }) {
4848
return fetch({
4949
url: `${this.apiRoot}${uId}/timeline/`,
5050
});
5151
},
5252

53-
addComment(uId, body) {
53+
addComment({ uId, body }) {
5454
return fetch({
5555
body,
5656
method: 'POST',
5757
url: `${this.apiRoot}${uId}/comment/`,
5858
});
5959
},
6060

61-
removeComment(uId) {
61+
removeComment({ uId }) {
6262
return fetch({
6363
method: 'DELETE',
6464
url: `${this.apiRoot}${uId}/comment/`,
@@ -67,23 +67,23 @@ const UnitAPI = {
6767

6868
/* Unit suggestions */
6969

70-
addSuggestion(uId, body) {
70+
addSuggestion({ uId, body }) {
7171
return fetch({
7272
body,
7373
method: 'POST',
7474
url: `${this.apiRoot}${uId}/suggestions/`,
7575
});
7676
},
7777

78-
acceptSuggestion(uId, suggId, body) {
78+
acceptSuggestion({ uId, suggId, body }) {
7979
return fetch({
8080
body,
8181
method: 'POST',
8282
url: `${this.apiRoot}${uId}/suggestions/${suggId}/`,
8383
});
8484
},
8585

86-
rejectSuggestion(uId, suggId, body) {
86+
rejectSuggestion({ uId, suggId, body }) {
8787
return fetch({
8888
body,
8989
method: 'DELETE',
@@ -93,7 +93,7 @@ const UnitAPI = {
9393

9494
/* Quality checks */
9595

96-
toggleCheck(uId, checkId, body = {}) {
96+
toggleCheck({ uId, checkId, body = {} }) {
9797
return fetch({
9898
body,
9999
method: 'POST',

0 commit comments

Comments
 (0)