Skip to content

Instantly share code, notes, and snippets.

@nandorojo
Created July 18, 2022 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nandorojo/c26fba7d79b88bed99d45e18851943fb to your computer and use it in GitHub Desktop.
Save nandorojo/c26fba7d79b88bed99d45e18851943fb to your computer and use it in GitHub Desktop.
field.ts
export function useSellerBookingRequestPersonalized() {
const getArtistsWhoMaybeCouldBid = ({ artists, bookingRequest }) => {
return artists?.filter((artist) =>
bookingRequest?.artist_ids?.includes(artist.id)
);
};
const getAreAllMyBidsRejected = ({ artists, bookingRequest }) => {
const artistsWhoMaybeCouldBid = getArtistsWhoMaybeCouldBid({
artists,
bookingRequest,
});
if (!artistsWhoMaybeCouldBid) return false;
for (const artistWhoMaybeCouldBid of artistsWhoMaybeCouldBid) {
const bid = bookingRequest?.bids_display?.[artistWhoMaybeCouldBid.id];
if (bid?.status !== BidStatus.REJECTED) {
return false;
}
}
return true;
};
const getIsSellerActionRequired = ({ bookingRequest, artists }) => {
const isActionRequired = artists?.some((artist) =>
bookingRequest?.seller_action_required_artist_ids?.includes?.(artist.id)
);
const isFutureEvent =
bookingRequest && isFuture(new Date(bookingRequest.start_time));
return !!(isActionRequired && isFutureEvent);
};
const getArtistsWhoCanBid = ({ artists, bookingRequest }) => {
if (bookingRequest?.status !== BookingRequestStatus.PENDING) {
return null;
}
return artists
?.filter((artist) => {
const myBid = bookingRequest?.bids_display?.[artist.id];
const isArtistAvailable = bookingRequest?.artist_ids?.includes(
artist.id
);
return (
!!isArtistAvailable &&
myBid?.status !== BidStatus.ACCEPTED &&
myBid?.status !== BidStatus.REJECTED &&
myBid?.status !== BidStatus.ARTIST_UNAVAILABLE
);
})
.sort((a, b) => {
const shouldABid =
bookingRequest?.bids_display?.[a.id]?.status === BidStatus.PENDING;
const shouldBBid =
bookingRequest?.bids_display?.[b.id]?.status === BidStatus.PENDING;
if (shouldABid && shouldBBid) {
return 0;
}
if (shouldABid && !shouldBBid) {
return -1;
}
return 1;
});
};
const getDidSomeonElsesBidGetChosen = ({
artists,
bookingRequest,
}): boolean => {
if (!artists || !bookingRequest) {
return false;
}
if (bookingRequest.status !== BookingRequestStatus.ACCEPTED) {
return false;
}
const bids = { ...bookingRequest.bids_display };
for (const myArtist of artists || []) {
const bidStatus = bookingRequest.bids_display?.[myArtist.id]?.status;
if (bidStatus === BidStatus.ACCEPTED) {
return false;
}
if (myArtist.id in bids) {
delete bids[myArtist.id];
}
}
for (const bid of Object.values(bids)) {
if (bid.status === BidStatus.ACCEPTED) {
return true;
}
}
return false;
};
const maybeGetMyAcceptedBid = (props) => {
const { artists, bookingRequest } = props;
if (!artists || !bookingRequest) {
return null;
}
if (bookingRequest.status !== BookingRequestStatus.ACCEPTED) {
return null;
}
for (const artist of artists || []) {
const bid = bookingRequest.bids_display?.[artist.id];
if (bid?.status === BidStatus.ACCEPTED) {
return {
artist,
bid,
};
}
}
return null;
};
const getIsPastEvent = ({
bookingRequest,
}: Pick<Props, "bookingRequest">) => {
if (!bookingRequest?.start_time) return false;
const startTime = new Date(bookingRequest.start_time).getTime();
const now = new Date().getTime();
return startTime < now;
};
const getStatusDescription = (props) => {
let statusDescription:
| {
text: string;
variant: keyof DripsyTheme["colors"];
filling: Filling;
capitalize?: boolean;
}
| undefined;
const { bookingRequest } = props;
const isActionRequired = getIsSellerActionRequired(props);
const maybeMyAcceptedBid = maybeGetMyAcceptedBid(props);
const didSomeonElsesBidGetChosen = getDidSomeonElsesBidGetChosen(props);
const isPastEvent = getIsPastEvent(props);
const areAllMyBidsRejected = getAreAllMyBidsRejected(props);
const isCancelled =
bookingRequest?.status === BookingRequestStatus.CANCELLED;
if (maybeMyAcceptedBid?.artist) {
statusDescription = {
text: "Bid Accepted",
filling: "faded",
variant: "success",
};
} else if (isActionRequired) {
statusDescription = {
text: "Action Required",
filling: "faded",
variant: "error",
};
} else if (isCancelled) {
statusDescription = {
capitalize: true,
variant: "warning",
filling: "faded",
text: `${BookingRequestStatus.CANCELLED} by Buyer`,
};
} else if (didSomeonElsesBidGetChosen) {
statusDescription = {
capitalize: true,
filling: "faded",
variant: "warning",
text: "Other artist chosen",
};
} else if (
isPastEvent &&
bookingRequest?.status === BookingRequestStatus.PENDING
) {
statusDescription = {
filling: "faded",
variant: "error",
text: "Expired - no artist chosen",
};
} else if (areAllMyBidsRejected) {
statusDescription = {
filling: "faded",
variant: "warning",
text: "Rejected",
};
} else {
statusDescription = {
filling: "faded",
variant: "text",
text: bookingRequest?.status.replaceAll("_", " ") ?? "",
};
}
return statusDescription;
};
return {
getArtistsWhoCanBid,
getArtistsWhoMaybeCouldBid,
getDidSomeonElsesBidGetChosen,
maybeGetMyAcceptedBid,
getIsSellerActionRequired,
getIsPastEvent,
getStatusDescription,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment