|
| 1 | +import { useState, useEffect } from "react"; |
| 2 | +import { useParams } from "react-router-dom"; |
| 3 | +import { |
| 4 | + Layout, |
| 5 | + Form, |
| 6 | + FormLayout, |
| 7 | + TextField, |
| 8 | + Card, |
| 9 | + Page, |
| 10 | + Frame, |
| 11 | + SkeletonPage, |
| 12 | + SkeletonBodyText, |
| 13 | +} from "@shopify/polaris"; |
| 14 | +import { useAppBridge } from "@shopify/app-bridge-react"; |
| 15 | +import { Redirect } from "@shopify/app-bridge/actions"; |
| 16 | +import { useAuthenticatedFetch } from "../../../hooks/useAuthenticatedFetch"; |
| 17 | +import { useAppQuery } from "../../../hooks/useAppQuery"; |
| 18 | + |
| 19 | +// Utility hooks for invoking the server endpoints that you created |
| 20 | +function useCustomization(id) { |
| 21 | + const url = `/api/deliveryCustomization/${id}`; |
| 22 | + return useAppQuery({ url }); |
| 23 | +} |
| 24 | + |
| 25 | +function useUpdateCustomization() { |
| 26 | + const fetch = useAuthenticatedFetch(); |
| 27 | + return async (deliveryCustomization) => { |
| 28 | + return await fetch("/api/deliveryCustomization/update", { |
| 29 | + method: "PUT", |
| 30 | + headers: { "Content-Type": "application/json" }, |
| 31 | + body: JSON.stringify(deliveryCustomization), |
| 32 | + }); |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +// A utility hook for redirecting back to the Delivery Customizations list |
| 37 | +function useRedirectToCustomizations() { |
| 38 | + const app = useAppBridge(); |
| 39 | + const redirect = Redirect.create(app); |
| 40 | + return () => { |
| 41 | + redirect.dispatch(Redirect.Action.ADMIN_PATH, { |
| 42 | + path: "/settings/shipping/customizations", |
| 43 | + }); |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +export default function NewCustomizationPage() { |
| 48 | + // Read the function and customization IDs from the URL |
| 49 | + const { functionId, id } = useParams(); |
| 50 | + |
| 51 | + // Fetch customization data |
| 52 | + const { isLoading, data: customization } = useCustomization(id); |
| 53 | + |
| 54 | + // Utility hooks |
| 55 | + const updateCustomization = useUpdateCustomization(); |
| 56 | + const redirect = useRedirectToCustomizations(); |
| 57 | + |
| 58 | + // Page state management |
| 59 | + const [mutationIsLoading, setMutationIsLoading] = useState(false); |
| 60 | + const [formData, setFormData] = useState({}); |
| 61 | + |
| 62 | + // Page breadcrumbs [ |
| 63 | + const breadcrumbs = [ |
| 64 | + { |
| 65 | + content: "Delivery Customizations", |
| 66 | + onAction: redirect, |
| 67 | + }, |
| 68 | + ]; |
| 69 | + |
| 70 | + // Store the form state on change |
| 71 | + const handleInputChange = (value, name) => { |
| 72 | + setFormData((data) => ({ ...data, [name]: value })); |
| 73 | + }; |
| 74 | + |
| 75 | + // Invoke the server endpoint when the form is submitted |
| 76 | + const handleSubmit = async () => { |
| 77 | + setMutationIsLoading(true); |
| 78 | + const response = await updateCustomization({ |
| 79 | + functionId, |
| 80 | + ...formData, |
| 81 | + }); |
| 82 | + if (response.status != 200) { |
| 83 | + const errorResponse = await response.json(); |
| 84 | + console.log( |
| 85 | + "Error updating delivery customization: ", |
| 86 | + errorResponse.error |
| 87 | + ); |
| 88 | + } else { |
| 89 | + redirect(); |
| 90 | + } |
| 91 | + setMutationIsLoading(false); |
| 92 | + }; |
| 93 | + |
| 94 | + // When the customization is loaded, set the form state |
| 95 | + useEffect(() => { |
| 96 | + setFormData(customization); |
| 97 | + }, [customization, setFormData]); |
| 98 | + |
| 99 | + if (isLoading) { |
| 100 | + return ( |
| 101 | + <Frame> |
| 102 | + <SkeletonPage |
| 103 | + title="Add message to delivery options" |
| 104 | + breadcrumbs={breadcrumbs} |
| 105 | + primaryAction |
| 106 | + > |
| 107 | + <Layout.Section> |
| 108 | + <Card sectioned> |
| 109 | + <SkeletonBodyText /> |
| 110 | + </Card> |
| 111 | + </Layout.Section> |
| 112 | + </SkeletonPage> |
| 113 | + </Frame> |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + // A basic input form page created using Polaris components |
| 118 | + return ( |
| 119 | + <Frame> |
| 120 | + <Page |
| 121 | + title="Add message to delivery options" |
| 122 | + primaryAction={{ |
| 123 | + onAction: handleSubmit, |
| 124 | + content: "Save", |
| 125 | + loading: mutationIsLoading, |
| 126 | + }} |
| 127 | + breadcrumbs={breadcrumbs} |
| 128 | + > |
| 129 | + <Layout.Section> |
| 130 | + <Card> |
| 131 | + <Card.Section> |
| 132 | + <Form onSubmit={handleSubmit}> |
| 133 | + <FormLayout> |
| 134 | + <FormLayout.Group condensed> |
| 135 | + <TextField |
| 136 | + type="text" |
| 137 | + maxLength={2} |
| 138 | + label="State/Province Code" |
| 139 | + value={formData?.stateProvinceCode} |
| 140 | + onChange={(value) => |
| 141 | + handleInputChange(value, "stateProvinceCode") |
| 142 | + } |
| 143 | + disabled={isLoading} |
| 144 | + requiredIndicator |
| 145 | + /> |
| 146 | + </FormLayout.Group> |
| 147 | + <FormLayout.Group> |
| 148 | + <TextField |
| 149 | + type="text" |
| 150 | + multiline={2} |
| 151 | + label="Message" |
| 152 | + value={formData?.message} |
| 153 | + onChange={(value) => handleInputChange(value, "message")} |
| 154 | + disabled={isLoading} |
| 155 | + requiredIndicator |
| 156 | + /> |
| 157 | + </FormLayout.Group> |
| 158 | + </FormLayout> |
| 159 | + </Form> |
| 160 | + </Card.Section> |
| 161 | + </Card> |
| 162 | + </Layout.Section> |
| 163 | + </Page> |
| 164 | + </Frame> |
| 165 | + ); |
| 166 | +} |
0 commit comments