Skip to content

Commit f17364b

Browse files
committed
Add unitAPI wrapper to the editor methods
1 parent 65c890a commit f17364b

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
@@ -128,14 +128,15 @@ PTL.editor = {
128128

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

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

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

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

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

16461639
processSubmission(data) {
@@ -1675,11 +1668,12 @@ PTL.editor = {
16751668
const body = assign({}, this.getValueStateData(ReactEditor.stateValues),
16761669
this.getReqData(), captchaCallbacks);
16771670

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

16851679
processSuggestion() {
@@ -1960,14 +1954,15 @@ PTL.editor = {
19601954

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

19731968
/* Shrinks context lines */
@@ -2056,11 +2051,12 @@ PTL.editor = {
20562051
e.preventDefault();
20572052
this.updateCommentDefaultProperties();
20582053

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

20662062
processAddComment(data) {
@@ -2081,11 +2077,12 @@ PTL.editor = {
20812077
removeComment(e) {
20822078
e.preventDefault();
20832079

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

20912088

@@ -2101,15 +2098,12 @@ PTL.editor = {
21012098
return;
21022099
}
21032100

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

21152109
renderTimeline(data) {
@@ -2277,11 +2271,12 @@ PTL.editor = {
22772271
},
22782272

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

22872282
processRejectSuggestion(data, suggId) {
@@ -2310,11 +2305,12 @@ PTL.editor = {
23102305
},
23112306

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

23202316
processAcceptSuggestion(data, suggId, skipToNext) {
@@ -2353,11 +2349,12 @@ PTL.editor = {
23532349
const isFalsePositive = $check.hasClass('false-positive');
23542350

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

23632360
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)