the flow we just created is not showing up

This commit is contained in:
Leon Serfaty G
2025-07-18 04:52:21 +00:00
parent 6954cf4364
commit 80aa0f32ba
3 changed files with 58 additions and 14 deletions
+16 -3
View File
@@ -22,6 +22,12 @@ const flowSchema = z.object({
path: z.string().min(1, 'Path is required').startsWith('/', { message: 'Path must start with /' }),
});
type State = {
success: boolean;
message: string;
errors?: z.ZodIssue[] | null;
}
export async function getFlows(): Promise<Flow[]> {
try {
const stmt = db.prepare(
@@ -46,14 +52,14 @@ export async function getFlow(id: number): Promise<Flow | null> {
}
}
export async function saveFlow(formData: FormData) {
export async function saveFlow(prevState: State, formData: FormData): Promise<State> {
const validatedFields = flowSchema.safeParse(Object.fromEntries(formData.entries()));
if (!validatedFields.success) {
return {
success: false,
message: 'Invalid fields.',
errors: validatedFields.error.flatten().fieldErrors,
errors: validatedFields.error.issues,
};
}
@@ -75,9 +81,16 @@ export async function saveFlow(formData: FormData) {
}
} catch (error: any) {
console.error('Failed to save flow:', error);
if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
return {
success: false,
message: 'A flow with this path already exists.',
errors: [{ path: ['path'], message: 'A flow with this path already exists.', code: 'custom' }],
};
}
return {
success: false,
message: error.code === 'SQLITE_CONSTRAINT_UNIQUE' ? 'A flow with this path already exists.' : 'An internal error occurred.',
message: 'An internal error occurred.',
errors: null,
};
}