diff --git a/billing-simulator/components/SimulatorValidationSchema.js b/billing-simulator/components/SimulatorValidationSchema.js index 42edb64..4fd2876 100644 --- a/billing-simulator/components/SimulatorValidationSchema.js +++ b/billing-simulator/components/SimulatorValidationSchema.js @@ -3,19 +3,10 @@ import { calculateUpdate } from "../utils/timelineHelper"; function arrayUptoValidate(array) { let length = array.length - 1; - console.log(length); array.pop(); - console.log(array); return array.every((value, index) => { let nextIndex = index + 1; - console.log("nextIndex:" + nextIndex); - console.log("value.up_to:" + value.up_to); if (nextIndex < length) { - console.log("array[nextIndex].up_to:" + array[nextIndex].up_to); - console.log( - "value.up_to < array[nextIndex].up_to:" + value.up_to < - array[nextIndex].up_to - ); return value.up_to * 1 < array[nextIndex].up_to * 1; } else { return true; @@ -23,7 +14,7 @@ function arrayUptoValidate(array) { }); } -function SimulatorValidationSchema(parameter) { +function SimulatorValidationSchema() { return Yup.object({ create_date: Yup.string().required("Required").nullable(), billing_cycle_anchor: Yup.string() @@ -44,13 +35,19 @@ function SimulatorValidationSchema(parameter) { name: "billing_cycle_anchor", exclusive: false, params: {}, - message: `billing_cycle_anchor cannot be later than next natural billing date ${calculateUpdate( - parameter - )} for plan`, + message: `billing_cycle_anchor cannot be later than next natural billing date for plan`, test: function (value) { // You can access the price field with `this.parent`. if (value !== null) { - return new Date(value) <= calculateUpdate(parameter); + return ( + new Date(value) <= + calculateUpdate( + this.parent.trial_end, + this.parent.create_date, + this.parent.interval, + this.parent.interval_count + ) + ); } else return true; }, }), diff --git a/billing-simulator/pages/index.js b/billing-simulator/pages/index.js index ec238c2..d2fd0a9 100644 --- a/billing-simulator/pages/index.js +++ b/billing-simulator/pages/index.js @@ -9,7 +9,7 @@ import { XIcon, } from "@heroicons/react/solid"; -import { Formik, Form, Field, FieldArray, ErrorMessage } from "formik"; +import { Formik, Form, Field, FieldArray } from "formik"; import SimulatorValidationSchema from "../components/SimulatorValidationSchema"; import * as htmlToImage from "html-to-image"; @@ -130,7 +130,7 @@ export default function Home() { { up_to: "inf", unit_amount: 1000, flat_amount: 0 }, ], }} - validationSchema={SimulatorValidationSchema(parameter)} + validationSchema={SimulatorValidationSchema()} onSubmit={(values) => { gtag.event({ action: "submit_form", diff --git a/billing-simulator/utils/timelineHelper.js b/billing-simulator/utils/timelineHelper.js index bd81225..bfba3c8 100644 --- a/billing-simulator/utils/timelineHelper.js +++ b/billing-simulator/utils/timelineHelper.js @@ -237,20 +237,20 @@ export const widthCalculator = (eventPoint, windowWidth) => { }; }; -export const calculateUpdate = (parameter) => { - const endDate = - parameter.billing_cycle_anchor !== null - ? parameter.billing_cycle_anchor - : parameter.trial_end !== null - ? parameter.trial_end - : parameter.create_date; +export const calculateUpdate = ( + trial_end, + create_date, + interval, + interval_count +) => { + const endDate = trial_end !== null ? trial_end : create_date; const updateDate = - parameter.interval === "year" - ? addYears(new Date(endDate), parameter.interval_count * 1) - : parameter.interval === "month" - ? addMonths(new Date(endDate), parameter.interval_count * 1) - : parameter.interval === "week" - ? addWeeks(new Date(endDate), parameter.interval_count * 1) - : addDays(new Date(endDate), parameter.interval_count * 1); + interval === "year" + ? addYears(new Date(endDate), interval_count * 1) + : interval === "month" + ? addMonths(new Date(endDate), interval_count * 1) + : interval === "week" + ? addWeeks(new Date(endDate), interval_count * 1) + : addDays(new Date(endDate), interval_count * 1); return updateDate; };