From 4972d5b6870d08afe107f7d7cefec4bbe1eec8dd Mon Sep 17 00:00:00 2001 From: Mark Pittaway Date: Wed, 18 Oct 2023 15:19:07 +1100 Subject: [PATCH] Remove duplicate code, use httpRequestVoidLocal from API --- .../src/components/fields/editor.tsx | 74 +++++-------------- 1 file changed, 18 insertions(+), 56 deletions(-) diff --git a/client/extensions/tga-sign-off/src/components/fields/editor.tsx b/client/extensions/tga-sign-off/src/components/fields/editor.tsx index f153de3..4184112 100644 --- a/client/extensions/tga-sign-off/src/components/fields/editor.tsx +++ b/client/extensions/tga-sign-off/src/components/fields/editor.tsx @@ -21,7 +21,6 @@ export class UserSignOffField extends React.Component { this.state = {users: {}}; this.removeSignOff = this.removeSignOff.bind(this); - this.sendSignOff = this.sendSignOff.bind(this); } componentDidMount() { @@ -51,12 +50,15 @@ export class UserSignOffField extends React.Component { }); } - sendSignOff() { + sendSignOff(authorIds?: Array) { const {notify} = superdesk.ui; const {gettext} = superdesk.localization; const {signOffIds} = getSignOffDetails(this.props.item, this.state.users); - const authorIds = getListAuthorIds(this.props.item) - .filter((authorId) => !signOffIds.includes(authorId)); + + if (authorIds == null) { + authorIds = getListAuthorIds(this.props.item) + .filter((authorId) => !signOffIds.includes(authorId)); + } if (authorIds.length === 0) { notify.error( @@ -67,35 +69,17 @@ export class UserSignOffField extends React.Component { return; } - superdesk.httpRequestJsonLocal({ - method: "POST", - path: "/sign_off_request", - payload: { - item_id: this.props.item._id, - authors: authorIds, - } - }).then(() => { - notify.success(gettext('Sign off request email(s) sent')); - }, (error) => { - notify.error(gettext('Failed to send sign off request email(s). {{ error }}', error)); - }); - } - - sendSingleSignOff(authorId: IUser['_id']) { - const {notify} = superdesk.ui; - const {gettext} = superdesk.localization; - superdesk.httpRequestJsonLocal({ method: 'POST', path: '/sign_off_request', payload: { item_id: this.props.item._id, - authors: [authorId], + authors: authorIds, } }).then(() => { notify.success(gettext('Sign off request email(s) sent')); }, (error) => { - notify.error(gettext('Failed to send sign off request email(s). {{ error }}', {error})); + notify.error(gettext('Failed to send sign off request email(s). {{ error }}', error)); }); } @@ -114,36 +98,14 @@ export class UserSignOffField extends React.Component { gettext('Remove publishing sign off') ).then((response) => { if (response) { - const baseUrl = superdesk.instance.config.server.url; - - fetch(`${baseUrl}/sign_off_request/${this.props.item._id}/${sign_off_data.user_id}`, { + superdesk.httpRequestVoidLocal({ method: 'DELETE', - headers: { - Authorization: superdesk.session.getToken(), - 'Content-Type': 'application/json', - }, - mode: 'cors', - credentials: 'include', - }) - .then((res) => { - if (res.ok) { - notify.success(gettext('Publishing sign off removed')); - } else { - // Attempt to convert error response to JSON, - // otherwise return body as text as returned from the server - res.text() - .then((bodyText) => { - try { - return Promise.reject(JSON.parse(bodyText)); - } catch (_err) { - return Promise.reject(bodyText); - } - }); - } - }) - .catch((error) => { - notify.error(gettext('Failed to remove sign off. {{ error }}', {error})) - }); + path: `/sign_off_request/${this.props.item._id}/${sign_off_data.user_id}`, + }).then(() => { + notify.success(gettext('Publishing sign off removed')); + }).catch((error: string) => { + notify.error(gettext('Failed to remove sign off. {{ error }}', {error})) + }); } }); } @@ -165,7 +127,7 @@ export class UserSignOffField extends React.Component { type="warning" icon="warning-sign" text={gettext('Send Sign Off Email(s)')} - onClick={this.sendSignOff} + onClick={this.sendSignOff.bind(this, undefined)} expand={true} disabled={this.props.readOnly} /> @@ -224,7 +186,7 @@ export class UserSignOffField extends React.Component { buttonProps={this.props.readOnly ? undefined : { text: gettext('Resend'), icon: 'refresh', - onClick: this.sendSingleSignOff.bind(this, pendingReview.user_id), + onClick: this.sendSignOff.bind(this, [pendingReview.user_id]), }} date={pendingReview.expires} /> @@ -241,7 +203,7 @@ export class UserSignOffField extends React.Component { buttonProps={this.props.readOnly ? undefined : { text: gettext('Send'), icon: 'assign', - onClick: this.sendSingleSignOff.bind(this, authorId), + onClick: this.sendSignOff.bind(this, [authorId]), }} /> )