Skip to content

Commit

Permalink
Pass booking data from tx page to checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
lyyder committed Dec 17, 2018
1 parent f60f9e8 commit f27492e
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/components/BookingPanel/BookingPanel.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const Default = {
props: {
className: css.example,
listing: createListing('listing_1'),
handleBookingSubmit: values => console.log('Submit:', values),
onSubmit: values => console.log('Submit:', values),
title: <span>Booking title</span>,
subTitle: 'Hosted by Author N',
authorDisplayName: 'Author Name',
Expand All @@ -22,7 +22,7 @@ export const WithClosedListing = {
props: {
className: css.example,
listing: createListing('listing_1', { state: LISTING_STATE_CLOSED }),
handleBookingSubmit: values => console.log('Submit:', values),
onSubmit: values => console.log('Submit:', values),
title: <span>Booking title</span>,
subTitle: 'Hosted by Author N',
authorDisplayName: 'Author Name',
Expand Down
6 changes: 3 additions & 3 deletions src/components/BookingPanel/BookingPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const BookingPanel = props => {
listing,
isOwnListing,
unitType,
handleBookingSubmit,
onSubmit,
title,
subTitle,
authorDisplayName,
Expand Down Expand Up @@ -107,7 +107,7 @@ const BookingPanel = props => {
className={css.bookingForm}
submitButtonWrapperClassName={css.bookingDatesSubmitButtonWrapper}
unitType={unitType}
onSubmit={handleBookingSubmit}
onSubmit={onSubmit}
price={price}
isOwnListing={isOwnListing}
timeSlots={timeSlots}
Expand Down Expand Up @@ -158,7 +158,7 @@ BookingPanel.propTypes = {
listing: propTypes.listing.isRequired,
isOwnListing: bool,
unitType: propTypes.bookingUnitType,
handleBookingSubmit: func.isRequired,
onSubmit: func.isRequired,
title: oneOfType([object, string]).isRequired,
subTitle: oneOfType([object, string]),
authorDisplayName: string.isRequired,
Expand Down
2 changes: 2 additions & 0 deletions src/components/TransactionPanel/TransactionPanel.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export const BookingPanelMaybe = props => {
listingTitle,
subTitle,
provider,
onSubmit,
onManageDisableScrolling,
timeSlots,
fetchTimeSlotsError,
Expand All @@ -201,6 +202,7 @@ export const BookingPanelMaybe = props => {
title={listingTitle}
subTitle={subTitle}
authorDisplayName={authorDisplayName}
onSubmit={onSubmit}
onManageDisableScrolling={onManageDisableScrolling}
timeSlots={timeSlots}
fetchTimeSlotsError={fetchTimeSlotsError}
Expand Down
3 changes: 3 additions & 0 deletions src/components/TransactionPanel/TransactionPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class TransactionPanelComponent extends Component {
declineInProgress,
acceptSaleError,
declineSaleError,
onSubmitBookingRequest,
timeSlots,
fetchTimeSlotsError,
} = this.props;
Expand Down Expand Up @@ -330,6 +331,7 @@ export class TransactionPanelComponent extends Component {
listingTitle={listingTitle}
subTitle={bookingSubTitle}
provider={currentProvider}
onSubmit={onSubmitBookingRequest}
onManageDisableScrolling={onManageDisableScrolling}
timeSlots={timeSlots}
fetchTimeSlotsError={fetchTimeSlotsError}
Expand Down Expand Up @@ -394,6 +396,7 @@ TransactionPanelComponent.propTypes = {
onShowMoreMessages: func.isRequired,
onSendMessage: func.isRequired,
onSendReview: func.isRequired,
onSubmitBookingRequest: func.isRequired,
timeSlots: arrayOf(propTypes.timeSlot),
fetchTimeSlotsError: propTypes.error,

Expand Down
2 changes: 2 additions & 0 deletions src/components/TransactionPanel/TransactionPanel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('TransactionPanel - Sale', () => {
onSendMessage: noop,
onSendReview: noop,
onResetForm: noop,
onSubmitBookingRequest: noop,
intl: fakeIntl,
};

Expand Down Expand Up @@ -276,6 +277,7 @@ describe('TransactionPanel - Order', () => {
onDeclineSale: noop,
acceptInProgress: false,
declineInProgress: false,
onSubmitBookingRequest: noop,
};

it('enquired matches snapshot', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ exports[`TransactionPanel - Order accepted matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -2349,6 +2350,7 @@ exports[`TransactionPanel - Order autodeclined matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -3667,6 +3669,7 @@ exports[`TransactionPanel - Order canceled matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -4985,6 +4988,7 @@ exports[`TransactionPanel - Order declined matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -6303,6 +6307,7 @@ exports[`TransactionPanel - Order delivered matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -7621,6 +7626,7 @@ exports[`TransactionPanel - Order enquired matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -8939,6 +8945,7 @@ exports[`TransactionPanel - Order preauthorized matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -10257,6 +10264,7 @@ exports[`TransactionPanel - Sale accepted matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -11575,6 +11583,7 @@ exports[`TransactionPanel - Sale autodeclined matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -12893,6 +12902,7 @@ exports[`TransactionPanel - Sale canceled matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -14211,6 +14221,7 @@ exports[`TransactionPanel - Sale declined matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -15529,6 +15540,7 @@ exports[`TransactionPanel - Sale delivered matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -16847,6 +16859,7 @@ exports[`TransactionPanel - Sale enquired matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down Expand Up @@ -18165,6 +18178,7 @@ exports[`TransactionPanel - Sale preauthorized matches snapshot 1`] = `
}
listingTitle="listing1 title"
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
provider={
Object {
"attributes": Object {
Expand Down
1 change: 1 addition & 0 deletions src/containers/CheckoutPage/CheckoutPage.duck.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const initialState = {
speculateTransactionInProgress: false,
speculateTransactionError: null,
speculatedTransaction: null,
enquiredTransaction: null,
initiateOrderError: null,
};

Expand Down
17 changes: 14 additions & 3 deletions src/containers/CheckoutPage/CheckoutPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ export class CheckoutPageComponent extends Component {
* based on this initial data.
*/
loadInitialData() {
const { bookingData, bookingDates, listing, fetchSpeculatedTransaction, history } = this.props;
const {
bookingData,
bookingDates,
listing,
enquiredTransaction,
fetchSpeculatedTransaction,
history,
} = this.props;
// Browser's back navigation should not rewrite data in session store.
// Action is 'POP' on both history.back() and page refresh cases.
// Action is 'PUSH' when user has directed through a link
Expand All @@ -85,12 +92,12 @@ export class CheckoutPageComponent extends Component {
const hasDataInProps = !!(bookingData && bookingDates && listing) && hasNavigatedThroughLink;
if (hasDataInProps) {
// Store data only if data is passed through props and user has navigated through a link.
storeData(bookingData, bookingDates, listing, STORAGE_KEY);
storeData(bookingData, bookingDates, listing, enquiredTransaction, STORAGE_KEY);
}

// NOTE: stored data can be empty if user has already successfully completed transaction.
const pageData = hasDataInProps
? { bookingData, bookingDates, listing }
? { bookingData, bookingDates, listing, enquiredTransaction }
: storedData(STORAGE_KEY);

const hasData =
Expand Down Expand Up @@ -465,6 +472,7 @@ CheckoutPageComponent.defaultProps = {
bookingDates: null,
speculateTransactionError: null,
speculatedTransaction: null,
enquiredTransaction: null,
currentUser: null,
};

Expand All @@ -480,6 +488,7 @@ CheckoutPageComponent.propTypes = {
speculateTransactionInProgress: bool.isRequired,
speculateTransactionError: propTypes.error,
speculatedTransaction: propTypes.transaction,
enquiredTransaction: propTypes.transaction,
initiateOrderError: propTypes.error,
currentUser: propTypes.currentUser,
params: shape({
Expand Down Expand Up @@ -508,6 +517,7 @@ const mapStateToProps = state => {
speculateTransactionInProgress,
speculateTransactionError,
speculatedTransaction,
enquiredTransaction,
initiateOrderError,
} = state.CheckoutPage;
const { currentUser } = state.user;
Expand All @@ -519,6 +529,7 @@ const mapStateToProps = state => {
speculateTransactionInProgress,
speculateTransactionError,
speculatedTransaction,
enquiredTransaction,
listing,
initiateOrderError,
};
Expand Down
1 change: 1 addition & 0 deletions src/containers/CheckoutPage/CheckoutPage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('CheckoutPage', () => {
speculateTransactionError: null,
speculateTransactionInProgress: false,
speculatedTransaction: null,
enquiredTransaction: null,
};

it('should return the initial state', () => {
Expand Down
33 changes: 29 additions & 4 deletions src/containers/CheckoutPage/CheckoutPageSessionHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import moment from 'moment';
import reduce from 'lodash/reduce';
import { types as sdkTypes } from '../../util/sdkLoader';
import { TRANSITION_ENQUIRE } from '../../util/types';

const { UUID, Money } = sdkTypes;

Expand Down Expand Up @@ -46,8 +47,20 @@ export const isValidListing = listing => {
return validateProperties(listing, props);
};

// Validate content of an enquired transaction received from SessionStore.
// An id is required and the last transition needs to be the enquire transition.
export const isValidEnquiredTransaction = transaction => {
const props = {
id: id => id instanceof UUID,
attributes: v => {
return typeof v === 'string' && v.lastTransition === TRANSITION_ENQUIRE;
},
};
return validateProperties(transaction, props);
};

// Stores given bookingDates and listing to sessionStorage
export const storeData = (bookingData, bookingDates, listing, storageKey) => {
export const storeData = (bookingData, bookingDates, listing, enquiredTransaction, storageKey) => {
if (window && window.sessionStorage && listing && bookingDates && bookingData) {
// TODO: How should we deal with Dates when data is serialized?
// Hard coded serializable date objects atm.
Expand All @@ -59,6 +72,7 @@ export const storeData = (bookingData, bookingDates, listing, storageKey) => {
bookingEnd: { date: bookingDates.bookingEnd, _serializedType: 'SerializableDate' },
},
listing,
enquiredTransaction,
storedAt: { date: new Date(), _serializedType: 'SerializableDate' },
};
/* eslint-enable no-underscore-dangle */
Expand All @@ -83,7 +97,7 @@ export const storedData = storageKey => {
return sdkTypes.reviver(k, v);
};

const { bookingData, bookingDates, listing, storedAt } = checkoutPageData
const { bookingData, bookingDates, listing, enquiredTransaction, storedAt } = checkoutPageData
? JSON.parse(checkoutPageData, reviver)
: {};

Expand All @@ -92,8 +106,19 @@ export const storedData = storageKey => {
? moment(storedAt).isAfter(moment().subtract(1, 'days'))
: false;

if (isFreshlySaved && isValidBookingDates(bookingDates) && isValidListing(listing)) {
return { bookingData, bookingDates, listing };
// resolve enquired transaction as valid if it is missing
const isEnquiredTransactionValid = !!enquiredTransaction
? isValidEnquiredTransaction(enquiredTransaction)
: true;

const isStoredDataValid =
isFreshlySaved &&
isValidBookingDates(bookingDates) &&
isValidListing(listing) &&
isEnquiredTransactionValid;

if (isStoredDataValid) {
return { bookingData, bookingDates, listing, enquiredTransaction };
}
}
return {};
Expand Down
2 changes: 1 addition & 1 deletion src/containers/ListingPage/ListingPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ export class ListingPageComponent extends Component {
listing={currentListing}
isOwnListing={isOwnListing}
unitType={unitType}
handleBookingSubmit={handleBookingSubmit}
onSubmit={handleBookingSubmit}
title={bookingTitle}
subTitle={bookingSubTitle}
authorDisplayName={authorDisplayName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ exports[`ListingPage matches snapshot 1`] = `
<withRouter(InjectIntl(BookingPanel))
authorDisplayName="user-1 display name"
fetchTimeSlotsError={null}
handleBookingSubmit={[Function]}
isOwnListing={false}
listing={
Object {
Expand Down Expand Up @@ -305,6 +304,7 @@ exports[`ListingPage matches snapshot 1`] = `
}
}
onManageDisableScrolling={[Function]}
onSubmit={[Function]}
subTitle="ListingPage.bookingSubTitle"
timeSlots={null}
title={
Expand Down
Loading

0 comments on commit f27492e

Please sign in to comment.