Skip to content

Commit

Permalink
Fix App renaming not working (#816)
Browse files Browse the repository at this point in the history
Fix rename failing
  • Loading branch information
Janpot authored Aug 18, 2022
1 parent 23ceebb commit b5d4fba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
5 changes: 4 additions & 1 deletion packages/toolpad-app/src/server/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ export async function updateApp(appId: string, name: string): Promise<void> {
id: appId,
},
data: { name },
select: {},
select: {
// Only return the id to reduce amount of data returned from the db
id: true,
},
});
}

Expand Down
21 changes: 11 additions & 10 deletions packages/toolpad-app/src/toolpad/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,24 @@ interface AppNameEditableProps {
editing?: boolean;
setEditing: (editing: boolean) => void;
loading?: boolean;
description?: React.ReactNode;
}

function AppNameEditable({ app, editing, setEditing, loading, description }: AppNameEditableProps) {
const [showAppRenameError, setShowAppRenameError] = React.useState<boolean>(false);
function AppNameEditable({ app, editing, setEditing, loading }: AppNameEditableProps) {
const [appRenameError, setAppRenameError] = React.useState<Error | null>(null);
const appNameInput = React.useRef<HTMLInputElement | null>(null);
const [appName, setAppName] = React.useState<string>(app?.name || '');

const handleAppNameChange = React.useCallback(
(newValue: string) => {
setShowAppRenameError(false);
setAppRenameError(null);
setAppName(newValue);
},
[setAppName],
);

const handleAppRenameClose = React.useCallback(() => {
setEditing(false);
setShowAppRenameError(false);
setAppRenameError(null);
}, [setEditing]);

const handleAppRenameSave = React.useCallback(
Expand All @@ -194,8 +193,8 @@ function AppNameEditable({ app, editing, setEditing, loading, description }: App
try {
await client.mutation.updateApp(app.id, name);
await client.invalidateQueries('getApps');
} catch (err) {
setShowAppRenameError(true);
} catch (err: any) {
setAppRenameError(err);
setEditing(true);
}
}
Expand All @@ -209,8 +208,8 @@ function AppNameEditable({ app, editing, setEditing, loading, description }: App
<EditableText
defaultValue={app?.name}
editable={editing}
helperText={showAppRenameError ? `An app named "${appName}" already exists` : description}
error={showAppRenameError}
helperText={appRenameError ? `An app named "${appName}" already exists` : null}
error={!!appRenameError}
onChange={handleAppNameChange}
onClose={handleAppRenameClose}
onSave={handleAppRenameSave}
Expand Down Expand Up @@ -450,8 +449,10 @@ function AppRow({ app, activeDeployment, onDelete }: AppRowProps) {
app={app}
editing={editingName}
setEditing={setEditingName}
description={app ? `Edited ${getReadableDuration(app.editedAt)}` : <Skeleton />}
/>
<Typography variant="caption">
{app ? `Edited ${getReadableDuration(app.editedAt)}` : <Skeleton />}
</Typography>
</TableCell>
<TableCell align="right">
<Stack direction="row" spacing={1} justifyContent={'flex-end'}>
Expand Down

0 comments on commit b5d4fba

Please sign in to comment.