fix: Wait for template data before rendering form - THE REAL FIX

## The ACTUAL Problem (Finally Found It!):

**React Query Error:**
```
"I have data cannot be undefined. Sanitization needs to be used value
other than undefined from your query function."
```

**Root Cause:**
- Component renders BEFORE template data loads
- Form tries to use `template.subject`, `template.body` when `template` is `undefined`
- React Query complains about undefined data
- Form inputs never get filled

## The Real Fix:

```tsx
// BEFORE (WRONG):
return (
  <SettingsLayout isLoading={isLoading}>
    <Input value={subject} />  // subject is "" even when template loads
    <RichTextEditor content={body} />  // body is "" even when template loads
  </SettingsLayout>
);

// AFTER (RIGHT):
if (isLoading || !template) {
  return <SettingsLayout isLoading={true}>Loading...</SettingsLayout>;
}

// Only render form AFTER template data is loaded
return (
  <SettingsLayout>
    <Input value={subject} />  // NOW subject has template.subject
    <RichTextEditor content={body} />  // NOW body has template.body
  </SettingsLayout>
);
```

## Why This Works:

1. **Wait for data** - Don't render form until `template` exists
2. **useEffect runs** - Sets subject/body from template data
3. **Form renders** - With correct default values
4. **RichTextEditor gets content** - Already has the body text
5. **Variables populate** - From template.variables

## What Should Happen Now:

1.  Page loads → Shows "Loading template data..."
2.  API returns → useEffect sets subject/body/variables
3.  Form renders → Inputs filled with default values
4.  RichTextEditor → Shows template body
5.  Variables → Available in dropdown

NO MORE UNDEFINED ERRORS! 🎉
This commit is contained in:
dwindown
2025-11-13 00:35:57 +07:00
parent cef6b55555
commit 97f438ed19

View File

@@ -207,13 +207,26 @@ export default function EditTemplate() {
);
}
// Don't render form until template data is loaded
if (isLoading || !template) {
return (
<SettingsLayout
title={__('Edit Template')}
description={__('Loading template...')}
isLoading={true}
>
<div className="text-muted-foreground">{__('Loading template data...')}</div>
</SettingsLayout>
);
}
return (
<SettingsLayout
title={template?.event_label || __('Edit Template')}
description={`${template?.channel_label || ''} - ${__('Customize the notification template. Use variables like {customer_name} to personalize messages.')}`}
title={template.event_label || __('Edit Template')}
description={`${template.channel_label || ''} - ${__('Customize the notification template. Use variables like {customer_name} to personalize messages.')}`}
onSave={handleSave}
saveLabel={__('Save Template')}
isLoading={isLoading}
isLoading={false}
action={
<div className="flex items-center gap-2">
<Button