Last active
January 9, 2022 04:06
-
-
Save dipeshhkc/a05d536f71517b8cbda92e09c02bb53f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ActionFunction, json } from 'remix'; | |
import { db } from '~/utils/db.server'; | |
// return type of this action | |
export type NewTaskActionData = { | |
formError?: string; | |
fieldErrors?: { | |
task?: string; | |
}; | |
fields?: { | |
task?: string; | |
}; | |
ok?: boolean; | |
}; | |
const badRequest = (data: NewTaskActionData) => { | |
return json(data, { status: 400 }); | |
}; | |
export const action: ActionFunction = async ({ request }) => { | |
//setting values to fields from request | |
const form = await request.formData(); | |
type fieldType = 'task'; | |
const fieldList: fieldType[] = ['task']; | |
const fields = {} as Record<fieldType, string>; | |
for (const fieldName of fieldList) { | |
const fieldValue = form.get(fieldName) || ''; | |
fields[fieldName] = fieldValue as string; | |
} | |
//validation | |
let fieldErrors = {} as Record<fieldType, string>; | |
if (!fields.task) { | |
fieldErrors.task = 'Task cannot be empty'; | |
return badRequest({ fieldErrors, fields }); | |
} | |
//adding task to database | |
try { | |
await db.task.create({ | |
data: { | |
name: fields.task, | |
}, | |
}); | |
} catch (err) { | |
console.log('Error', err); | |
return badRequest({ | |
formError: err.message, | |
}); | |
} | |
return json({ ok: true }); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment