import { useMutation, useQuery } from '@apollo/client' import React, { FormEvent, useEffect, useState } from 'react' import { Redirect, useParams } from 'react-router-dom' import { FORM, FORMSUBMIT } from '../../apollo' import { ChoiseAnswer, ChoisesQuestion, Form, InputAnswer, InputQuestion, QueryFormArgs, } from '../../apollo/typeDefs.gen' import Lists from './Lists' interface IFormQuery { form: Form } const DoForm: React.FC = () => { const { id: idString } = useParams<{ id: string }>() const id = parseInt(idString) const { data, error, loading } = useQuery(FORM, { variables: { id }, skip: isNaN(id), }) const [ doFormSubmit, { error: submitError, data: submitData, loading: submitLoading }, ] = useMutation(FORMSUBMIT) const [answers, setAnswer] = useState<(InputAnswer | ChoiseAnswer)[]>([]) const getInitialState = (data: IFormQuery) => { if (data && data.form) { return data.form.questions.flatMap( (el: InputQuestion | ChoisesQuestion) => { if (el.__typename === 'ChoisesQuestion') return [ { __typename: 'ChoiseAnswer', type: 'CHOISE', userChoise: -1 }, ] if (el.__typename === 'InputQuestion') return [{ __typename: 'InputAnswer', type: 'INPUT', userInput: '' }] return [] } ) } return [] } useEffect(() => { if (data) { const initialState = getInitialState(data) setAnswer(initialState) } }, [data]) useEffect(() => console.log(answers), [answers]) if (isNaN(id)) return if (loading) return
Loading...
if (error) return
{error.message}
const { form } = data! const handleSubmit = (e: FormEvent) => { e.preventDefault() console.log('Submited form:', answers) answers.forEach((el) => { delete el.__typename }) const submitAnswers = JSON.stringify(answers) console.log('Filtered answers: ', submitAnswers) doFormSubmit({ variables: { formId: id, answers: submitAnswers, }, }) } const answerChange = (num: number) => { return (value: number | string) => { setAnswer((prev) => { return prev.map((el, index) => { if (index === num) { if (el.__typename === 'ChoiseAnswer' && typeof value === 'number') return { ...el, userChoise: value } if (el.__typename === 'InputAnswer' && typeof value === 'string') return { ...el, userInput: value } } return el }) }) } } return (

{form.title}

{form.dateCreated}

{form.author?.name || 'No author'}

{form.submissions ? (

Submission{form.submissions.length > 1 && 's'}:

    {form.submissions.map((submission, submissionIndex) => (
  • User:{' '} {submission.user ? submission.user.name : 'No submitter'}

      {submission.answers.map( (answer: InputAnswer | ChoiseAnswer, answerIndex) => (
    • {form.questions[answerIndex].title}

      {answer.__typename === 'ChoiseAnswer' && (

      { (form.questions[answerIndex] as ChoisesQuestion) .variants[answer.userChoise].text }

      )} {answer.__typename === 'InputAnswer' && (

      {answer.userInput}

      )}
    • ) )}
  • ))}
) : (
    {form.questions.map((el: InputQuestion | ChoisesQuestion) => { if (el.__typename === 'InputQuestion') return (
  • ) if (el.__typename === 'ChoisesQuestion') return (
  • ) return
  • Unknown question type
  • })}
{submitLoading ?

Uploading...

: }
)} {submitError &&

{submitError.message}

} {submitData && submitData.formSubmit && submitData.formSubmit.success && (

Successfully uploaded

)}
) } export default DoForm