WIP Add proper error handling when creating a listing (#2755)

* Add proper error handling when creating a listing

* Remove accidentally committed stylesheet

* Make error text

* Preserve input values if the submission is invalid
This commit is contained in:
Andy Zhao 2019-05-07 18:25:25 -04:00 committed by Ben Halpern
parent 51837621a5
commit bb75ee3fc6
7 changed files with 26 additions and 13 deletions

View file

@ -181,9 +181,12 @@
}
.classified-errors {
background: $red;
color: white;
color: $black;
padding: 20px;
border-radius: 3px;
h2 {
color: $black;
}
}
label {
display: inline-block;

View file

@ -1 +0,0 @@
@import 'variables';

View file

@ -46,12 +46,16 @@ class ClassifiedListingsController < ApplicationController
@classified_listing.bumped_at = Time.current
@classified_listing.published = true
@classified_listing.organization_id = current_user.organization_id if @org
return unless @classified_listing.save
clear_listings_cache
credits.limit(@number_of_credits_needed).update_all(spent: true)
@classified_listing.index!
redirect_to "/listings"
if @classified_listing.save
clear_listings_cache
credits.limit(@number_of_credits_needed).update_all(spent: true)
@classified_listing.index!
redirect_to "/listings"
else
@credits = current_user.credits.where(spent: false)
@classified_listing.cached_tag_list = classified_listing_params[:tag_list]
render :new
end
end
def update

View file

@ -3,8 +3,14 @@ import { h, Component } from 'preact';
class Categories extends Component {
options = () => {
const { categoriesForSelect } = this.props
const { categoriesForSelect, category } = this.props
return categoriesForSelect.map(array => {
// array example: ["Education/Courses (1 Credit)", "education"]
if(category === array[1]) {
return(
<option value={array[1]} selected>{array[0]}</option>
)
}
return(
<option value={array[1]}>{array[0]}</option>
)
@ -51,6 +57,7 @@ class Categories extends Component {
Categories.propTypes = {
categoriesForSelect: PropTypes.array.isRequired,
categoriesForDetails: PropTypes.array.isRequired,
category: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
}

View file

@ -47,7 +47,7 @@ export default class ListingForm extends Component {
<div>
<Title defaultValue={title} onChange={linkState(this, 'title')} />
<BodyMarkdown defaultValue={bodyMarkdown} onChange={linkState(this, 'bodyMarkdown')} />
<Categories categoriesForSelect={categoriesForSelect} categoriesForDetails={categoriesForDetails} onChange={linkState(this, 'category')} />
<Categories categoriesForSelect={categoriesForSelect} categoriesForDetails={categoriesForDetails} onChange={linkState(this, 'category')} category={category} />
<Tags defaultValue={tagList} category={category} onInput={linkState(this, 'tagList')} />
{/* add contact via connect checkbox later */}
</div>

View file

@ -89,7 +89,7 @@ class ClassifiedListing < ApplicationRecord
end
def restrict_markdown_input
errors.add(:body_markdown, "has too many linebreaks. No no more than 12 allowed.") if body_markdown.to_s.scan(/(?=\n)/).count > 12
errors.add(:body_markdown, "has too many linebreaks. No more than 12 allowed.") if body_markdown.to_s.scan(/(?=\n)/).count > 12
errors.add(:body_markdown, "is not allowed to include images.") if body_markdown.to_s.include?("![")
errors.add(:body_markdown, "is not allowed to include liquid tags.") if body_markdown.to_s.include?("{% ")
end

View file

@ -6,7 +6,7 @@
<div class="classified-form-inner">
<% if classified_listing.errors.any? %>
<div class="classified-errors">
<h2><%= pluralize(classified_listing.errors.count, "error") %> prohibited this classified_listing from being saved:</h2>
<h2><%= pluralize(classified_listing.errors.count, "error") %> prohibited this listing from being saved:</h2>
<ul>
<% classified_listing.errors.full_messages.each do |message| %>
@ -16,7 +16,7 @@
</div>
<% end %>
<div id="listingform-data"
data-listing="<%= classified_listing.to_json(only: %i[id title slug cached_tag_list]) %>"
data-listing="<%= classified_listing.to_json(only: %i[id title body_markdown category cached_tag_list]) %>"
data-organizations="<%= @organizations.to_json %>"
data-categories-for-select="<%= ClassifiedListing.select_options_for_categories.to_json %>"
data-categories-for-details="<%= ClassifiedListing.categories_available.transform_values{ |value_hash| value_hash.except(:cost) }.values.to_json %>"