diff --git a/campaigns.go b/campaigns.go index b6e1b47..5746a52 100644 --- a/campaigns.go +++ b/campaigns.go @@ -22,6 +22,9 @@ import ( type campaignReq struct { models.Campaign + // Indicates if the "send_at" date should be written or set to null. + SendLater bool `db:"-" json:"send_later"` + // This overrides Campaign.Lists to receive and // write a list of int IDs during creation and updation. // Campaign.Lists is JSONText for sending lists children @@ -271,6 +274,7 @@ func handleUpdateCampaign(c echo.Context) error { o.Body, o.ContentType, o.SendAt, + o.SendLater, pq.StringArray(normalizeTags(o.Tags)), o.TemplateID, o.ListIDs) diff --git a/frontend/src/Campaign.js b/frontend/src/Campaign.js index a15d5d6..582aa4e 100644 --- a/frontend/src/Campaign.js +++ b/frontend/src/Campaign.js @@ -217,14 +217,12 @@ class TheFormDef extends React.PureComponent { } componentWillReceiveProps(nextProps) { - const has = nextProps.isSingle && nextProps.record.send_at !== null - if (!has) { + if (nextProps.record.send_at === this.props.record.send_at) { return } - - if (this.state.sendLater !== has) { - this.setState({ sendLater: has }) - } + this.setState({ + sendLater: nextProps.isSingle && nextProps.record.send_at !== null + }) } validateEmail = (rule, value, callback) => { @@ -303,7 +301,11 @@ class TheFormDef extends React.PureComponent { cs.ModelCampaigns, cs.Routes.UpdateCampaign, cs.MethodPut, - { ...values, id: this.props.record.id } + { + ...values, + id: this.props.record.id, + send_at: !this.state.sendLater ? null : values.send_at + } ) .then(resp => { notification["success"]({ @@ -511,9 +513,7 @@ class TheFormDef extends React.PureComponent { - {getFieldDecorator("send_later", { - defaultChecked: this.props.isSingle - })( + {getFieldDecorator("send_later")( 0 THEN $8 ELSE tags END), - template_id=(CASE WHEN $9 != 0 THEN $9 ELSE template_id END), + send_at=(CASE WHEN $8 THEN $7::TIMESTAMP WITH TIME ZONE WHEN NOT $8 THEN NULL ELSE send_at END), + tags=(CASE WHEN ARRAY_LENGTH($9::VARCHAR(100)[], 1) > 0 THEN $9 ELSE tags END), + template_id=(CASE WHEN $10 != 0 THEN $10 ELSE template_id END), updated_at=NOW() WHERE id = $1 RETURNING id ), d AS ( -- Reset list relationships - DELETE FROM campaign_lists WHERE campaign_id = $1 AND NOT(list_id = ANY($10)) + DELETE FROM campaign_lists WHERE campaign_id = $1 AND NOT(list_id = ANY($11)) ) INSERT INTO campaign_lists (campaign_id, list_id, list_name) - (SELECT $1 as campaign_id, id, name FROM lists WHERE id=ANY($10::INT[])) + (SELECT $1 as campaign_id, id, name FROM lists WHERE id=ANY($11::INT[])) ON CONFLICT (campaign_id, list_id) DO UPDATE SET list_name = EXCLUDED.list_name; -- name: update-campaign-counts