Skip to content
Draft
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
66 changes: 41 additions & 25 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ export const Home: FC = () => {
client?.deregisterElement("shopifyRefreshButton");

client?.registerElement("shopifyRefreshButton", { type: "refresh_button" });
client?.registerElement("shopifyMenu", {
type: "menu",
items: [{
title: "Change Linked Customer",
payload: { type: "changePage", page: "link_customer" },
}, {
title: "Settings",
payload: "settings",
}],
});

// Redundant in app v1 -- we don't link customers and we don't link to admin settings
// client?.registerElement("shopifyMenu", {
// type: "menu",
// items: [{
// title: "Change Linked Customer",
// payload: { type: "changePage", page: "link_customer" },
// }, {
// title: "Settings",
// payload: "settings",
// }],
// });

}, [client, state])

const onChangePageOrder = (orderId: Order['legacyResourceId']) => {
Expand All @@ -43,23 +46,36 @@ export const Home: FC = () => {
return;
}

getEntityCustomerList(client, userId)
.then(async (customers: string[]) => {
const customerId = customers[0];

try {
const { customer } = await getCustomer(client, customerId);
const { orders } = customer;
// todo: change the getCustomer() method to accept an email
getCustomer(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't find one customer by email. To find via email we need to use Customers query. This query may return more than one customer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be do faster to Linking the customer one by one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On LinkCustomer page, we just need to delete the existing association and add a new one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that's fine

client,
state.context?.type === 'ticket' ? state.context?.data.ticket.primaryUser.email : state.context?.data.email
).then((customer) => {
if (customer) {
// todo: if we have a customer, link it to th app state, e.g. dispatch({ type: "linkedCustomer", customer }) so we can reference it in the various pages
} else {
// todo: if we don't have a customer then we don't show the app home page (show a message saying "Shopify customer could not be found for joe.bloggs@example.com")
}
});

dispatch({ type: "linkedCustomer", customer });
dispatch({ type: "linkedOrders", orders });
} catch (e) {
const error = e as Error;
// getEntityCustomerList(client, userId)
// .then(async (customers: string[]) => {
// const customerId = customers[0];
//
// try {
// const { customer } = await getCustomer(client, customerId);
// const { orders } = customer;
//
// dispatch({ type: "linkedCustomer", customer });
// dispatch({ type: "linkedOrders", orders });
// } catch (e) {
// const error = e as Error;
//
// throw new Error(error.message || "Failed to fetch");
// }
// })
// .catch((error: Error) => dispatch({ type: "error", error }));

throw new Error(error.message || "Failed to fetch");
}
})
.catch((error: Error) => dispatch({ type: "error", error }));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [client, userId]);

Expand Down
13 changes: 7 additions & 6 deletions src/pages/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export const Main: FC = () => {
console.error(`Shopify: ${state._error}`);
}

useTryToLinkCustomer(
useCallback(() => dispatch({ type: "changePage", page: "home" }), [dispatch]),
useCallback(() => dispatch({ type: "changePage", page: "link_customer" }), [dispatch]),
);
// we don't need this as we simply match based on DP user's email address (and never show the link customer screen
// useTryToLinkCustomer(
// useCallback(() => dispatch({ type: "changePage", page: "home" }), [dispatch]),
// useCallback(() => dispatch({ type: "changePage", page: "link_customer" }), [dispatch]),
// );

useDeskproAppEvents({
onShow: () => {
Expand All @@ -44,13 +45,13 @@ export const Main: FC = () => {

const page = match<Page|undefined>(state.page)
.with("home", () => <Home />)
.with("link_customer", () => <LinkCustomer />)
// .with("link_customer", () => <LinkCustomer />) // Link customers page is redundant in v1
.with("view_customer", () => <ViewCustomer />)
.with("edit_customer", () => <EditCustomer />)
.with("list_orders", () => <ListOrders />)
.with("view_order", () => <ViewOrder />)
.with("edit_order", () => <EditOrder />)
.otherwise(() => <LinkCustomer />)
.otherwise(() => <Home />) // Always default to home page as we don't need to link customers

return (
<>
Expand Down