Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix billing address pre-fill in checkout #4144

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions frontend/src/components/checkout-page-handler/billing-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ export const BillingCard = ({
state: { invoiceInformation, hasAdmissionTicket },
updateInformation,
} = useCart();
const savedBillingInformation = me?.billingAddresses.find(
(billingAddress) =>
billingAddress.isBusiness === invoiceInformation.isBusiness,
);

const countries = useCountries();
const [formState, { text, email, select, textarea, checkbox }] =
useFormState<InvoiceInformationState>({ ...invoiceInformation });
Expand All @@ -53,6 +48,12 @@ export const BillingCard = ({
const emptyInvoiceInformation = Object.entries(invoiceInformation).every(
([key, value]) => key === "isBusiness" || !value,
);

const savedBillingInformation = me?.billingAddresses.find(
(billingAddress) =>
billingAddress.isBusiness === invoiceInformation.isBusiness,
);

if (emptyInvoiceInformation && savedBillingInformation) {
formState.setField("companyName", savedBillingInformation.companyName);
formState.setField("name", savedBillingInformation.userName);
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/tickets-page/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
UpdateProductAction,
Voucher,
} from "./types";
import { EMPTY_INITIAL_CART_REDUCER } from "./use-cart";

const updateProductReducer = (
state: OrderState,
Expand Down Expand Up @@ -227,7 +228,7 @@ export const reducer = (state: OrderState, action: OrderAction): OrderState => {
...state,
selectedProducts: {},
invoiceInformation: {
...state.invoiceInformation,
...EMPTY_INITIAL_CART_REDUCER.invoiceInformation,
isBusiness: action.isBusiness,
},
};
Expand Down
42 changes: 22 additions & 20 deletions frontend/src/components/tickets-page/use-cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,32 @@ function fromBinaryStr(binary) {
return decoder.decode(bytes);
}

export const EMPTY_INITIAL_CART_REDUCER = {
selectedProducts: {},
invoiceInformation: {
isBusiness: false,
companyName: "",
name: "",
vatId: "",
address: "",
zipCode: "",
city: "",
country: "",
fiscalCode: "",
pec: "",
sdi: "",
},
selectedHotelRooms: {},
voucherCode: "",
voucherUsed: false,
hasAdmissionTicket: false,
};

export const createCartContext = ({
cartCookie = "",
}: {
cartCookie?: string;
}) => {
const emptyInitialCartReducer = {
selectedProducts: {},
invoiceInformation: {
isBusiness: false,
companyName: "",
name: "",
vatId: "",
address: "",
zipCode: "",
city: "",
country: "",
fiscalCode: "",
},
selectedHotelRooms: {},
voucherCode: "",
voucherUsed: false,
hasAdmissionTicket: false,
};

let storedCart = null;

try {
Expand All @@ -112,7 +114,7 @@ export const createCartContext = ({

const [state, dispatcher] = useReducer(
reducer,
storedCart || emptyInitialCartReducer,
storedCart || EMPTY_INITIAL_CART_REDUCER,
);

useEffect(() => {
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/pages/tickets/business.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { addApolloState, getApolloClient } from "~/apollo/client";
import { Tickets } from "~/components/tickets-page/tickets";
import { TicketsPageWrapper } from "~/components/tickets-page/wrapper";
import { prefetchSharedQueries } from "~/helpers/prefetch";
import { CheckoutCategory, queryTickets } from "~/types";
import { CheckoutCategory, queryCurrentUser, queryTickets } from "~/types";

export const BusinessTicketsPage = ({ cartCookie }) => {
return (
Expand Down Expand Up @@ -61,6 +61,14 @@ export const getServerSideProps: GetServerSideProps = async ({
}),
]);

try {
await queryCurrentUser(client, {
conference: process.env.conferenceCode,
});
} catch (e) {
console.debug("User not logged in");
}

const cartCookie = req.cookies["tickets-cart-v6"];
return addApolloState(
client,
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/pages/tickets/personal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { addApolloState, getApolloClient } from "~/apollo/client";
import { Tickets } from "~/components/tickets-page/tickets";
import { TicketsPageWrapper } from "~/components/tickets-page/wrapper";
import { prefetchSharedQueries } from "~/helpers/prefetch";
import { CheckoutCategory, queryTickets } from "~/types";
import { CheckoutCategory, queryCurrentUser, queryTickets } from "~/types";

export const PersonalTicketsPage = ({ cartCookie }) => {
return (
Expand Down Expand Up @@ -61,6 +61,14 @@ export const getServerSideProps: GetServerSideProps = async ({
}),
]);

try {
await queryCurrentUser(client, {
conference: process.env.conferenceCode,
});
} catch (e) {
console.debug("User not logged in");
}

const cartCookie = req.cookies["tickets-cart-v6"];
return addApolloState(
client,
Expand Down