From 47c288581090d3cc617fff4aa81a2a9cbd64bdc8 Mon Sep 17 00:00:00 2001 From: Mario See Date: Fri, 23 Aug 2019 17:33:50 -0400 Subject: [PATCH] Listings set custom expiry date (#3770) * add expire_on column to classified_listings * add expire_on to valid listing params * add expire_on to model * add expire date to new listing form * add field in edit form * add expiry dates to dashboard * update expiry date in listing toolkit * WIP unpublish job and test test still has to run properly * add job to class name * change job to rake task * revert snap change * delete migration file will add new one and revert schema changes to rename column * reset schema * add new migration to schema 'expires_at' * change expire_on to expires_at --- .../classified_listings_controller.rb | 2 +- .../concerns/classified_listings_toolkit.rb | 1 + .../listings/dashboard/listingRow.jsx | 13 +++++++ .../listings/elements/expireDate.jsx | 34 +++++++++++++++++++ app/javascript/listings/listingForm.jsx | 4 +++ app/models/classified_listing.rb | 2 +- .../classified_listings/dashboard.html.erb | 4 +-- app/views/classified_listings/edit.html.erb | 4 +++ ...4_add_expires_at_to_classified_listings.rb | 5 +++ db/schema.rb | 3 +- lib/tasks/fetch.rake | 7 ++++ 11 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 app/javascript/listings/elements/expireDate.jsx create mode 100644 db/migrate/20190822162434_add_expires_at_to_classified_listings.rb diff --git a/app/controllers/classified_listings_controller.rb b/app/controllers/classified_listings_controller.rb index 9fa29c33f..547e6d7da 100644 --- a/app/controllers/classified_listings_controller.rb +++ b/app/controllers/classified_listings_controller.rb @@ -158,7 +158,7 @@ class ClassifiedListingsController < ApplicationController # Never trust parameters from the scary internet, only allow a specific list through. def listing_params - accessible = %i[title body_markdown category tag_list contact_via_connect location organization_id action] + accessible = %i[title body_markdown category tag_list expires_at contact_via_connect location organization_id action] params.require(:classified_listing).permit(accessible) end end diff --git a/app/controllers/concerns/classified_listings_toolkit.rb b/app/controllers/concerns/classified_listings_toolkit.rb index 8ae9ed397..175d09cc0 100644 --- a/app/controllers/concerns/classified_listings_toolkit.rb +++ b/app/controllers/concerns/classified_listings_toolkit.rb @@ -19,6 +19,7 @@ module ClassifiedListingsToolkit @classified_listing.tag_list = listing_params[:tag_list] if listing_params[:tag_list] @classified_listing.category = listing_params[:category] if listing_params[:category] @classified_listing.location = listing_params[:location] if listing_params[:location] + @classified_listing.expires_at = listing_params[:expires_at] if listing_params[:expires_at] @classified_listing.contact_via_connect = listing_params[:contact_via_connect] if listing_params[:contact_via_connect] @classified_listing.save end diff --git a/app/javascript/listings/dashboard/listingRow.jsx b/app/javascript/listings/dashboard/listingRow.jsx index e31da4265..c8a34e7a8 100644 --- a/app/javascript/listings/dashboard/listingRow.jsx +++ b/app/javascript/listings/dashboard/listingRow.jsx @@ -26,6 +26,18 @@ export const ListingRow = ({ listing }) => { '' ); + const expiryDate = listing.expires_at ? + new Date(listing.expires_at.toString()).toLocaleDateString('default', { + day: '2-digit', + month: 'short', + }) : '' ; + + const listingExpiry = expiryDate !== '' ? ( + ` | Expires on: ${expiryDate}` + ) : ( + '' + ); + return (
{orgName} @@ -34,6 +46,7 @@ export const ListingRow = ({ listing }) => { {listingDate} + {listingExpiry} {listingLocation} diff --git a/app/javascript/listings/elements/expireDate.jsx b/app/javascript/listings/elements/expireDate.jsx new file mode 100644 index 000000000..4b6e3c198 --- /dev/null +++ b/app/javascript/listings/elements/expireDate.jsx @@ -0,0 +1,34 @@ +import PropTypes from 'prop-types'; +import { h } from 'preact'; + +const ExpireDate = ({ onChange, defaultValue }) => { + let tomorrow = new Date(); + let monthFromToday = new Date(); + tomorrow.setDate(new Date().getDate() + 1); + tomorrow = tomorrow.toISOString().split('T')[0]; + monthFromToday.setDate(new Date().getDate() + 30); + monthFromToday = monthFromToday.toISOString().split('T')[0]; + + return ( +
+ + +
+ ); +} + +ExpireDate.propTypes = { + onChange: PropTypes.func.isRequired, + defaultValue: PropTypes.string.isRequired, +} + +export default ExpireDate; diff --git a/app/javascript/listings/listingForm.jsx b/app/javascript/listings/listingForm.jsx index d02d988b0..aa547639e 100644 --- a/app/javascript/listings/listingForm.jsx +++ b/app/javascript/listings/listingForm.jsx @@ -5,6 +5,7 @@ import BodyMarkdown from './elements/bodyMarkdown'; import Categories from './elements/categories'; import Tags from './elements/tags'; import OrgSettings from './elements/orgSettings'; +import ExpireDate from './elements/expireDate'; export default class ListingForm extends Component { constructor(props) { @@ -28,6 +29,7 @@ export default class ListingForm extends Component { categoriesForDetails: this.categoriesForDetails, organizations, organizationId: null, // change this for /edit later + expireDate: this.listing.expires_at || '', }; } @@ -47,6 +49,7 @@ export default class ListingForm extends Component { categoriesForSelect, organizations, organizationId, + expireDate, } = this.state; const orgArea = organizations && organizations.length > 0 ? ( @@ -77,6 +80,7 @@ export default class ListingForm extends Component { category={category} onInput={linkState(this, 'tagList')} /> + {orgArea} {/* add contact via connect checkbox later */}
diff --git a/app/models/classified_listing.rb b/app/models/classified_listing.rb index ccc04127b..7a6820d7f 100644 --- a/app/models/classified_listing.rb +++ b/app/models/classified_listing.rb @@ -24,7 +24,7 @@ class ClassifiedListing < ApplicationRecord validate :validate_category algoliasearch per_environment: true do - attribute :title, :processed_html, :bumped_at, :tag_list, :category, :id, :user_id, :slug, :contact_via_connect, :location + attribute :title, :processed_html, :bumped_at, :tag_list, :category, :id, :user_id, :slug, :contact_via_connect, :location, :expires_at attribute :author do { username: author.username, name: author.name, diff --git a/app/views/classified_listings/dashboard.html.erb b/app/views/classified_listings/dashboard.html.erb index a6a5ca9a3..27865fc6a 100644 --- a/app/views/classified_listings/dashboard.html.erb +++ b/app/views/classified_listings/dashboard.html.erb @@ -1,12 +1,12 @@
<%= f.text_field "location", maxlength: 32, placeholder: "32 characters max, plain text" %>
+
+ <%= f.label "expires_at", "Custom Expire Date" %> + <%= f.date_field "expires_at", min: Date.tomorrow, max: @classified_listing.bumped_at + 30.days %> +
<%= f.label "contact_via_connect", "Allow Users to Message Me Via In-App Chat (DEV Connect)" %> <%= f.check_box "contact_via_connect" %> diff --git a/db/migrate/20190822162434_add_expires_at_to_classified_listings.rb b/db/migrate/20190822162434_add_expires_at_to_classified_listings.rb new file mode 100644 index 000000000..5c16ed3cd --- /dev/null +++ b/db/migrate/20190822162434_add_expires_at_to_classified_listings.rb @@ -0,0 +1,5 @@ +class AddExpiresAtToClassifiedListings < ActiveRecord::Migration[5.2] + def change + add_column :classified_listings, :expires_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 6c7ffafb1..b68adff37 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_08_18_191954) do +ActiveRecord::Schema.define(version: 2019_08_22_162434) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -254,6 +254,7 @@ ActiveRecord::Schema.define(version: 2019_08_18_191954) do t.string "category" t.boolean "contact_via_connect", default: false t.datetime "created_at", null: false + t.datetime "expires_at" t.datetime "last_buffered" t.string "location" t.bigint "organization_id" diff --git a/lib/tasks/fetch.rake b/lib/tasks/fetch.rake index 28eba3f2d..5c779dd58 100644 --- a/lib/tasks/fetch.rake +++ b/lib/tasks/fetch.rake @@ -34,6 +34,13 @@ task expire_old_listings: :environment do end end +task expire_old_listings: :environment do + ClassifiedListing.where("expires_at = ?", Time.zone.today).each do |listing| + listing.update(published: false) + listing.remove_from_index! + end +end + task clear_memory_if_too_high: :environment do Rails.cache.clear if Rails.cache.stats.flatten[1]["bytes"].to_i > 9_650_000_000 end