import React from 'react';
import ErrorsModal from 'misago/components/threads/moderation/errors-list'; // jshint ignore:line
import MergeThreads from 'misago/components/threads/moderation/merge'; // jshint ignore:line
import MoveThreads from 'misago/components/threads/moderation/move'; // jshint ignore:line
import * as select from 'misago/reducers/selection'; // jshint ignore:line
import ajax from 'misago/services/ajax'; // jshint ignore:line
import modal from 'misago/services/modal'; // jshint ignore:line
import snackbar from 'misago/services/snackbar'; // jshint ignore:line
import store from 'misago/services/store'; // jshint ignore:line
import Countdown from 'misago/utils/countdown'; // jshint ignore:line
export default class extends React.Component {
/* jshint ignore:start */
callApi = (ops, successMessage, onSuccess=null) => {
// freeze threads
this.props.threads.forEach((thread) => {
this.props.freezeThread(thread.id);
});
// list ids
const ids = this.props.threads.map((thread) => {
return thread.id;
});
// always return current acl
ops.push({op: 'add', path: 'acl', value: true});
ajax.patch(this.props.api, { ids, ops }).then(
(data) => {
// unfreeze
this.props.threads.forEach((thread) => {
this.props.freezeThread(thread.id);
});
// update threads
data.forEach((thread) => {
this.props.updateThread(thread);
});
// show success message and call callback
snackbar.success(successMessage);
if (onSuccess) {
onSuccess();
}
},
(rejection) => {
// unfreeze
this.props.threads.forEach((thread) => {
this.props.freezeThread(thread.id);
});
// escape on non-400 error
if (rejection.status !== 400) {
return snackbar.apiError(rejection);
}
// build errors list
let errors = [];
let threadsMap = {}
this.props.threads.forEach((thread) => {
threadsMap[thread.id] = thread;
});
rejection.forEach(({id, detail }) => {
if (typeof threadsMap[id] !== 'undefined') {
errors.push({
errors: detail,
thread: threadsMap[id]
});
}
});
modal.show(
);
}
);
};
pinGlobally = () => {
this.callApi([
{
op: 'replace',
path: 'weight',
value: 2
}
], gettext("Selected threads were pinned globally."));
};
pinLocally = () => {
this.callApi([
{
op: 'replace',
path: 'weight',
value: 1
}
], gettext("Selected threads were pinned locally."));
};
unpin = () => {
this.callApi([
{
op: 'replace',
path: 'weight',
value: 0
}
], gettext("Selected threads were unpinned."));
};
approve = () => {
this.callApi([
{
op: 'replace',
path: 'is-unapproved',
value: false
}
], gettext("Selected threads were approved."));
};
open = () => {
this.callApi([
{
op: 'replace',
path: 'is-closed',
value: false
}
], gettext("Selected threads were opened."));
};
close = () => {
this.callApi([
{
op: 'replace',
path: 'is-closed',
value: true
}
], gettext("Selected threads were closed."));
};
unhide = () => {
this.callApi([
{
op: 'replace',
path: 'is-hidden',
value: false
}
], gettext("Selected threads were unhidden."));
};
hide = () => {
this.callApi([
{
op: 'replace',
path: 'is-hidden',
value: true
}
], gettext("Selected threads were hidden."));
};
move = () => {
modal.show(
);
};
merge = () => {
const errors = [];
this.props.threads.forEach((thread) => {
if (!thread.acl.can_merge) {
errors.append({
'id': thread.id,
'title': thread.title,
'errors': [
gettext("You don't have permission to merge this thread with others.")
]
});
}
});
if (this.props.threads.length < 2) {
snackbar.info(
gettext("You have to select at least two threads to merge."));
} else if (errors.length) {
modal.show();
return;
} else {
modal.show();
}
};
delete = () => {
if (!confirm(gettext("Are you sure you want to delete selected threads?"))) {
return;
}
this.props.threads.map((thread) => {
this.props.freezeThread(thread.id);
});
const ids = this.props.threads.map((thread) => { return thread.id; });
ajax.delete(this.props.api, ids).then(() => {
this.props.threads.map((thread) => {
this.props.freezeThread(thread.id);
this.props.deleteThread(thread);
});
snackbar.success(gettext("Selected threads were deleted."));
}, (rejection) => {
if (rejection.status === 400) {
const failedThreads = rejection.map((thread) => { return thread.id; });
this.props.threads.map((thread) => {
this.props.freezeThread(thread.id);
if (failedThreads.indexOf(thread.id) === -1) {
this.props.deleteThread(thread);
}
});
modal.show();
} else {
snackbar.apiError(rejection);
}
});
};
/* jshint ignore:end */
getPinGloballyButton() {
if (!this.props.moderation.can_pin_globally) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
getPinLocallyButton() {
if (!this.props.moderation.can_pin) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
getUnpinButton() {
if (!this.props.moderation.can_pin) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
getMoveButton() {
if (!this.props.moderation.can_move) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
getMergeButton() {
if (!this.props.moderation.can_merge) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
getApproveButton() {
if (!this.props.moderation.can_approve) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
getOpenButton() {
if (!this.props.moderation.can_close) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
getCloseButton() {
if (!this.props.moderation.can_close) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
getUnhideButton() {
if (!this.props.moderation.can_unhide) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
getHideButton() {
if (!this.props.moderation.can_hide) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
getDeleteButton() {
if (!this.props.moderation.can_delete) return null;
/* jshint ignore:start */
return (
);
/* jshint ignore:end */
}
render() {
/* jshint ignore:start */
return
{this.getPinGloballyButton()}
{this.getPinLocallyButton()}
{this.getUnpinButton()}
{this.getMoveButton()}
{this.getMergeButton()}
{this.getApproveButton()}
{this.getOpenButton()}
{this.getCloseButton()}
{this.getUnhideButton()}
{this.getHideButton()}
{this.getDeleteButton()}
;
/* jshint ignore:end */
}
}