Generate form data typescript

export const generateFormData = <T = Record<string, unknown | unknown[]>>(
  forms: T
): FormData => {
  const formData = new FormData()
 
  for (const name in forms) {
    if (Array.isArray(forms[name as keyof T])) {
      ;(forms[name as keyof T] as unknown[]).forEach((value) => {
        formData.append(name, value as string | File)
      })
    } else {
      formData.append(name, forms[name as keyof T] as string | File)
    }
  }

  return formData
}

//created

  const create = async (
    payload: InferType<typeof ApplicationFormSchema>,
    form: SubmissionContext
  ): Promise<void> => {
    const { data, error } = await useHttp<CreateApplicationResponse>(
      API_ENDPOINT.APP.ADMIN_APPLICATION,
      {
        method: 'POST',
        body: generateFormData<typeof payload>(payload),
      }
    )

    if (error.value) {
      const errorMessage = error.value.data?.message

      if (errorMessage === 'fn(tx): app already exists') {
        form.setFieldError('name', 'Application already registered')
      }

      throw new Error(error.value.message)
    }

    if (data.value) {
      setTimeout(async () => {
        await navigateTo(
          `/dashboard/applications/detail/${data.value?.data.id}`,
          {
            replace: true,
          }
        )
      }, 2000)
    }
  }

Contact us

Perumahan Griya Pratama Asri 2 No.H-12, Somoragen, Joho, Kec. Prambanan, Kabupaten Klaten, Jawa Tengah 57454
Find Me
iconiconiconiconhttps://www.buymeacoffee.com/outyourimpossible
2024-PRESENT © Muh Syahendra A