<commit_before>import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';
import { _ } from 'meteor/underscore';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Bookdata, BookdataSchema } from '../../api/bookdata/bookdata.js';
import { YourBookdata, YourBookSchema } from '../../api/yourbooks/yourbooks.js';

Template.Browse_Books_Page.helpers({

  /**
   * @returns {*} All of the Bookdata documents.
   */
  bookdataList() {
    return Bookdata.find();
  },
});

Template.Sell_Books_Page.onRendered(function enableDropDown() {
  this.$('.dropdown').dropdown();
});

const displayErrorMessages = 'displayErrorMessages';

/*  export const groupObjects = [{ label: 'First', value: 'First' },
                             { label: 'Last', value: 'Last' },
                             { label: 'Email', value: 'Email' },
                             { label: 'Price', value: 'Price' },
                             { label: 'Description', value: 'Description' }];
                             */

export const conditionList = ['Excellent', 'Great', 'Good', 'Fair', 'Poor'];

Template.Sell_Books_Page.onCreated(function onCreated() {
  this.messageFlags = new ReactiveDict();
  this.messageFlags.set(displayErrorMessages, false);
  this.context = YourBookSchema.namedContext('Sell_Books_Page');
});

Template.Sell_Books_Page.helpers({
  errorClass() {
    return Template.instance().messageFlags.get(displayErrorMessages) ? 'error' : '';
  },
  fieldError(fieldName) {
    const invalidKeys = Template.instance().context.invalidKeys();
    const errorObject = _.find(invalidKeys, (keyObj) => keyObj.name === fieldName);
    return errorObject && Template.instance().context.keyErrorMessage(errorObject.name);
  },
  conditions() {
    return _.map(conditionList, function makeLevelObject(level) {
      return { label: level };
    });
  },
  booktitles() {
    const bookDataTitles = Bookdata.find().fetch();
    const l = [];
    _.map(bookDataTitles, function (book) {
      l.push({
        value: book, label: book.title, selected: false,
      });
    });
    return l;
  },
  /*group() {
    return groupObjects;
  },
  */
});

Template.Sell_Books_Page.events({
  'submit .sale-data-form'(event, instance) {
    event.preventDefault();
    // Get name (text field)
    // Are these getting the right things? Especially Condition and Titles
    const title = event.target.Titles.value;
    const email = event.target.Email.value;
    const firstName = event.target.First.value;
    const lastName = event.target.Last.value;
    const price = event.target.Price.value;
    const condition = event.target.Condition.value;
    const description = event.target.Description.value;
    console.log('Can you see me?');
    console.log(`price: ${price}, condition: ${condition}, description: ${description}, lastName: ${lastName}, firstName: ${firstName}, email" ${email}`);

    const newSaleData = { title, condition, firstName, lastName, price, email, description };
    // Clear out any old validation errors.
    instance.context.resetValidation();
    // Invoke clean so that newStudentData reflects what will be inserted.
    YourBookSchema.clean(newSaleData);
    console.log(newSaleData);
    // Determine validity.
    instance.context.validate(newSaleData);
    if (instance.context.isValid()) {
      console.log('What is here vv');
      console.log(newSaleData);
      YourBookdata.insert(newSaleData);
      instance.messageFlags.set(displayErrorMessages, false);
      FlowRouter.go('Your_Books_Page');
    } else {
      console.log('What is here else');
      console.log(newSaleData);
      instance.messageFlags.set(displayErrorMessages, true);
    }
  },
});
//<commit_msg>Schema now inserts a card into your-books-page.js<commit_after>import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';
import { _ } from 'meteor/underscore';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Bookdata, BookdataSchema } from '../../api/bookdata/bookdata.js';
import { YourBookdata, YourBookSchema } from '../../api/yourbooks/yourbooks.js';

Template.Browse_Books_Page.helpers({

  /**
   * @returns {*} All of the Bookdata documents.
   */
  bookdataList() {
    return Bookdata.find();
  },
});

Template.Sell_Books_Page.onRendered(function enableDropDown() {
  this.$('.dropdown').dropdown();
});

const displayErrorMessages = 'displayErrorMessages';

/*  export const groupObjects = [{ label: 'First', value: 'First' },
                             { label: 'Last', value: 'Last' },
                             { label: 'Email', value: 'Email' },
                             { label: 'Price', value: 'Price' },
                             { label: 'Description', value: 'Description' }];
                             */

export const conditionList = ['Excellent', 'Great', 'Good', 'Fair', 'Poor'];

Template.Sell_Books_Page.onCreated(function onCreated() {
  this.messageFlags = new ReactiveDict();
  this.messageFlags.set(displayErrorMessages, false);
  this.context = YourBookSchema.namedContext('Sell_Books_Page');
});

Template.Sell_Books_Page.helpers({
  errorClass() {
    return Template.instance().messageFlags.get(displayErrorMessages) ? 'error' : '';
  },
  fieldError(fieldName) {
    const invalidKeys = Template.instance().context.invalidKeys();
    const errorObject = _.find(invalidKeys, (keyObj) => keyObj.name === fieldName);
    return errorObject && Template.instance().context.keyErrorMessage(errorObject.name);
  },
  conditions() {
    return _.map(conditionList, function makeLevelObject(level) {
      return { label: level };
    });
  },
  booktitles() {
    const bookDataTitles = Bookdata.find().fetch();
    const l = [];
    _.map(bookDataTitles, function (book) {
      l.push({
        value: book, label: book.title, selected: false,
      });
    });
    return l;
  },
  /*group() {
    return groupObjects;
  },
  */
});

Template.Sell_Books_Page.events({
  'submit .sale-data-form'(event, instance) {
    event.preventDefault();
    // Get name (text field)
    // Are these getting the right things? Especially Condition and Titles
    const title = event.target.Titles.value;
    const address = event.target.Email.value;
    const first = event.target.First.value;
    const last = event.target.Last.value;
    const price = event.target.Price.value;
    const condition = event.target.Condition.value;
    const description = event.target.Description.value;
    console.log('Can you see me?');
    console.log(`price: ${price}, condition: ${condition}, description: ${description}, lastName: ${last}, firstName: ${first}, address" ${address}`);

    const newSaleData = { title, condition, first, last, price, address, description };
    // Clear out any old validation errors.
    instance.context.resetValidation();
    // Invoke clean so that newStudentData reflects what will be inserted.
    YourBookSchema.clean(newSaleData);
    console.log(newSaleData);
    // Determine validity.
    instance.context.validate(newSaleData);
    if (instance.context.isValid()) {
      console.log('What is here vv');
      console.log(newSaleData);
      YourBookdata.insert(newSaleData);
      instance.messageFlags.set(displayErrorMessages, false);
      FlowRouter.go('Your_Books_Page');
    } else {
      console.log('What is here else');
      console.log(newSaleData);
      instance.messageFlags.set(displayErrorMessages, true);
    }
  },
});
//
{'fraction_non_alphanumeric': 0.145234291799787, 'fraction_numerical': 0.0, 'mean_word_length': 3.26875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1545280', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2182, 'n_words': 474}