Skip to content

Commit

Permalink
extract more logic to utils/form
Browse files Browse the repository at this point in the history
  • Loading branch information
nighca committed Apr 1, 2024
1 parent cdb7c9e commit 83e7b9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
16 changes: 4 additions & 12 deletions spx-gui/src/components/project/ProjectCreate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

<script setup lang="ts">
import { ref } from 'vue'
import { NForm, NFormItem, NInput, NButton, type FormInst } from 'naive-ui'
import { NForm, NFormItem, NInput, NButton } from 'naive-ui'
import { type ProjectData, getProject, addProject as rawAddProject, IsPublic } from '@/apis/project'
import { useFormRules, type ValidationResult } from '@/utils/form'
import { useForm, type ValidationResult } from '@/utils/form'
import { useMessageHandle } from '@/utils/exception'
import { useUserStore } from '@/stores/user'
import { ApiException, ApiExceptionCode } from '@/apis/common/exception'
Expand All @@ -30,13 +30,11 @@ const emit = defineEmits<{
const userStore = useUserStore()
const formRef = ref<FormInst | null>(null)
const formValue = ref({
name: ''
})
const formRules = useFormRules({
const [formRef, formRules, validateForm] = useForm({
name: validateName
})
Expand All @@ -52,13 +50,7 @@ const addProject = useMessageHandle(
async function handleSubmit() {
if (formRef.value == null) return
const errs = await formRef.value.validate().then( // TODO: extract such logic to utils/form
() => [],
e => {
if (Array.isArray(e)) return e
throw e
}
)
const errs = await validateForm()
if (errs.length > 0) return
const projectData = await addProject({
name: formValue.value.name,
Expand Down
27 changes: 22 additions & 5 deletions spx-gui/src/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* @desc Helpers to deal with form validation
*/

import { type FormRules } from 'naive-ui'
import { ref } from 'vue'
import type { FormRules, FormInst, FormValidationError } from 'naive-ui'
import { useI18n, type LocaleMessage } from './i18n'

export type ValidationResult = LocaleMessage | null | undefined
Expand All @@ -16,11 +17,14 @@ export type FormRulesInput = {
[path: string]: Validator<any>
}

export function useFormRules(input: FormRulesInput): FormRules {
export function useForm(rulesInput: FormRulesInput) {
const { t } = useI18n()

const formRef = ref<FormInst | null>(null)

const rules: FormRules = {}
Object.keys(input).forEach(path => {
const validator = input[path]
Object.keys(rulesInput).forEach(path => {
const validator = rulesInput[path]
rules[path] = {
async validator(_: unknown, v: unknown) {
const result = await validator(v)
Expand All @@ -31,7 +35,20 @@ export function useFormRules(input: FormRulesInput): FormRules {
trigger: ['input', 'blur'] // TODO: we need to define our own trigger logic
}
})
return rules

async function validate() {
if (formRef.value == null) throw new Error('invalid calling of validate without formRef value')
const errs: FormValidationError[] = await formRef.value.validate().then(
() => [],
e => {
if (Array.isArray(e)) return e
throw e
}
)
return errs
}

return [formRef, rules, validate] as const
}

export function isPromiseLike(arg: any): arg is Promise<any> {
Expand Down

0 comments on commit 83e7b9d

Please sign in to comment.