All files / web/src/components/family LinkChildForm.tsx

0% Statements 0/284
0% Branches 0/1
0% Functions 0/1
0% Lines 0/284

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
'use client'

import { useCallback, useEffect, useState } from 'react'
import { useTheme } from '@/contexts/ThemeContext'
import { useLinkChild } from '@/hooks/useUserPlayers'
import { css } from '../../../styled-system/css'

interface LinkChildFormProps {
  isOpen: boolean
  onClose: () => void
  onSuccess?: () => void
}

/**
 * Modal form to link to an existing child via family code
 *
 * Used when a second parent wants to add a child that was
 * created by another parent.
 */
export function LinkChildForm({ isOpen, onClose, onSuccess }: LinkChildFormProps) {
  const { resolvedTheme } = useTheme()
  const isDark = resolvedTheme === 'dark'

  const [familyCode, setFamilyCode] = useState('')
  const [linkedPlayer, setLinkedPlayer] = useState<{
    name: string
    emoji: string
  } | null>(null)

  // React Query mutation
  const linkChild = useLinkChild()

  // Reset state when modal opens
  useEffect(() => {
    if (isOpen) {
      setFamilyCode('')
      setLinkedPlayer(null)
      linkChild.reset()
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isOpen])

  // Handle form submission
  const handleSubmit = useCallback(
    (e: React.FormEvent) => {
      e.preventDefault()

      if (!familyCode.trim()) return

      linkChild.mutate(familyCode.trim(), {
        onSuccess: (data) => {
          if (data.success && data.player) {
            setLinkedPlayer({
              name: data.player.name,
              emoji: data.player.emoji,
            })
            onSuccess?.()
          }
        },
      })
    },
    [familyCode, linkChild, onSuccess]
  )

  // Handle close and reset
  const handleClose = useCallback(() => {
    setFamilyCode('')
    setLinkedPlayer(null)
    linkChild.reset()
    onClose()
  }, [onClose, linkChild])

  if (!isOpen) return null

  // Get error message from mutation
  const error = linkChild.data?.success === false ? linkChild.data.error : null

  return (
    <div
      data-component="link-child-modal"
      className={css({
        position: 'fixed',
        inset: 0,
        zIndex: 10000,
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        backgroundColor: 'rgba(0, 0, 0, 0.5)',
      })}
      onClick={handleClose}
    >
      <div
        className={css({
          backgroundColor: isDark ? 'gray.800' : 'white',
          borderRadius: '16px',
          padding: '24px',
          maxWidth: '400px',
          width: '90%',
          boxShadow: '0 4px 20px rgba(0, 0, 0, 0.3)',
          position: 'relative',
        })}
        onClick={(e) => e.stopPropagation()}
      >
        {linkedPlayer ? (
          // Success state
          <div
            className={css({
              textAlign: 'center',
              padding: '20px 0',
            })}
          >
            <div
              className={css({
                fontSize: '4rem',
                marginBottom: '16px',
              })}
            >
              {linkedPlayer.emoji}
            </div>
            <h2
              className={css({
                fontSize: '1.25rem',
                fontWeight: 'bold',
                color: isDark ? 'white' : 'gray.800',
                marginBottom: '8px',
              })}
            >
              Successfully Linked!
            </h2>
            <p
              className={css({
                fontSize: '0.875rem',
                color: isDark ? 'gray.400' : 'gray.600',
                marginBottom: '20px',
              })}
            >
              You now have access to {linkedPlayer.name}&apos;s practice data.
            </p>
            <button
              type="button"
              onClick={handleClose}
              data-action="close-link-success"
              className={css({
                padding: '12px 24px',
                backgroundColor: isDark ? 'green.700' : 'green.500',
                color: 'white',
                border: 'none',
                borderRadius: '8px',
                fontSize: '14px',
                fontWeight: 'medium',
                cursor: 'pointer',
                _hover: {
                  backgroundColor: isDark ? 'green.600' : 'green.600',
                },
              })}
            >
              Done
            </button>
          </div>
        ) : (
          // Form state
          <>
            <h2
              className={css({
                fontSize: '1.25rem',
                fontWeight: 'bold',
                color: isDark ? 'white' : 'gray.800',
                marginBottom: '8px',
              })}
            >
              Link Existing Child
            </h2>
            <p
              className={css({
                fontSize: '0.875rem',
                color: isDark ? 'gray.400' : 'gray.600',
                marginBottom: '20px',
              })}
            >
              Enter the family code shared by another parent to link to their child.
            </p>

            <form onSubmit={handleSubmit}>
              <input
                type="text"
                value={familyCode}
                onChange={(e) => setFamilyCode(e.target.value.toUpperCase())}
                placeholder="FAM-XXXXXX"
                data-input="family-code"
                className={css({
                  width: '100%',
                  padding: '14px 16px',
                  backgroundColor: isDark ? 'gray.700' : 'gray.100',
                  border: '2px solid',
                  borderColor: error ? 'red.500' : isDark ? 'gray.600' : 'gray.200',
                  borderRadius: '8px',
                  fontSize: '1.125rem',
                  fontFamily: 'monospace',
                  letterSpacing: '0.1em',
                  textAlign: 'center',
                  color: isDark ? 'white' : 'gray.800',
                  marginBottom: '8px',
                  outline: 'none',
                  transition: 'border-color 0.15s ease',
                  _focus: {
                    borderColor: isDark ? 'blue.500' : 'blue.400',
                  },
                  _placeholder: {
                    color: isDark ? 'gray.500' : 'gray.400',
                  },
                })}
              />

              {error && (
                <p
                  className={css({
                    fontSize: '0.8125rem',
                    color: 'red.500',
                    marginBottom: '12px',
                    textAlign: 'center',
                  })}
                >
                  {error}
                </p>
              )}

              <button
                type="submit"
                disabled={linkChild.isPending || !familyCode.trim()}
                data-action="submit-link-child"
                className={css({
                  width: '100%',
                  padding: '14px',
                  backgroundColor: isDark ? 'blue.700' : 'blue.500',
                  color: 'white',
                  border: 'none',
                  borderRadius: '8px',
                  fontSize: '16px',
                  fontWeight: 'medium',
                  cursor: linkChild.isPending ? 'wait' : 'pointer',
                  transition: 'all 0.15s ease',
                  marginTop: '8px',
                  _hover: {
                    backgroundColor: isDark ? 'blue.600' : 'blue.600',
                  },
                  _disabled: {
                    opacity: 0.5,
                    cursor: 'not-allowed',
                  },
                })}
              >
                {linkChild.isPending ? 'Linking...' : 'Link Child'}
              </button>
            </form>
          </>
        )}

        {/* Close button */}
        <button
          type="button"
          onClick={handleClose}
          data-action="close-link-child-modal"
          className={css({
            position: 'absolute',
            top: '12px',
            right: '12px',
            padding: '8px',
            backgroundColor: 'transparent',
            border: 'none',
            cursor: 'pointer',
            color: isDark ? 'gray.500' : 'gray.400',
            fontSize: '20px',
            lineHeight: 1,
            _hover: {
              color: isDark ? 'gray.300' : 'gray.600',
            },
          })}
        >
          ×
        </button>
      </div>
    </div>
  )
}