Here is my app._index.jsx file. I am new to the react and the Shopify model. I am trying to get the key from the formsubmit and save that value to metafield definition so each store/shop can have their own key saved. I want to use this key in my app at later stage. Think of this as a GA config key etc. The path to shopifyserver is actual path because ../shopify.server.js path was not being found.
I can see that the admin exists on the load but not when the action is triggered and i get undefined for the admin and the graphql is never triggered . Hope you can help.
import { useEffect } from "react";
import { json } from "@remix-run/node";
import { authenticate } from "/Users/ja/Desktop/ShopifyApp/new-data-publisher/app/shopify.server.js";
import { useLoaderData,useActionData, useNavigation, useSubmit } from "@remix-run/react";
import {
Page,
Layout,
Text,
Card,
Button,
BlockStack,
Box,
List,
Link,
InlineStack,
Form
} from "@shopify/polaris";
//console.log( authenticate);
export const loader = async ({ request }) => {
const { admin } = await authenticate.admin(request);
//return { admin }
return null;
};
export async function action({ request, newappkeyValue }) {
try {
const { admin } = await authenticate?.admin(request);
// Ensure admin object is available before proceeding
if (!admin) {
console.error('Error: Admin object not available.');
return json({ error: 'Admin object not available' }, { status: 500 });
}
const newAppKey = newappkeyValue;
const response = await admin.graphql(`
mutation CreateMetafieldDefinition($definition: MetafieldDefinitionInput!) {
metafieldDefinitionCreate(definition: $definition) {
createdDefinition {
id
name
namespace
key
description
}
userErrors {
field
message
code
}
}
}
`, {
variables: {
definition: {
name: "newAppkey",
namespace: "$app:new-appkey",
key: `${newAppKey}`,
description: "stores customers new app key.",
type: "list.single_line_text_field"
}
},
});
const responseJson = await response.json();
return json({
createdDefinition: responseJson.data.metafieldDefinitionCreate.createdDefinition,
userErrors: responseJson.data.metafieldDefinitionCreate.userErrors,
});
} catch (error) {
console.error('Error in action:', error.message);
return json({ error: 'Internal Server Error' }, { status: 500 });
}
}
export default function Index() {
const nav = useNavigation();
const actionData = useActionData();
const submit = useSubmit();
const isLoading =
["loading", "submitting"].includes(nav.state) && nav.formMethod === "POST";
//const newAppKey = actionData?.product?.id.replace(
// "gid://shopify/Product/",
// ""
//);
//useEffect(() => {
// if (newAppKey) {
// shopify.toast.show("newAppKey Added");
//}
//}, [newAppKey]);
//const generateProduct = () => submit({}, { replace: true, method: "POST" });
return (
);
}