|
1 | | -import { Formik, Form, Field, FieldArray } from 'formik'; |
| 1 | +import { Formik, Form, Field } from 'formik'; |
| 2 | +import CircularProgress from './CircularProgress'; |
2 | 3 |
|
3 | 4 | export interface VideoFormProps { |
4 | 5 | onSubmit: (videos: string[]) => Promise<void> | void; |
5 | 6 | } |
6 | 7 |
|
7 | 8 | function VideoForm({ onSubmit }: VideoFormProps) { |
| 9 | + const isValidYouTubeUrlOrId = (url: string) => { |
| 10 | + const regExp = /^.*(youtu.be\/|v\/|e\/|u\/\w+\/|embed\/|v=)?([^#&?]*).*/; |
| 11 | + const match = url.match(regExp); |
| 12 | + return ( |
| 13 | + Array.isArray(match) && (match[2].length === 11 || url.length === 11) |
| 14 | + ); |
| 15 | + }; |
| 16 | + |
| 17 | + const validateYouTubeLinks = ({ videos }: { videos: string }) => { |
| 18 | + const errors: { videos?: string } = {}; |
| 19 | + if (typeof videos !== 'string') { |
| 20 | + errors.videos = 'Please enter YouTube links.'; |
| 21 | + |
| 22 | + return errors; |
| 23 | + } |
| 24 | + |
| 25 | + const urls = videos.split(','); |
| 26 | + const invalidUrls = urls.filter( |
| 27 | + (url) => !isValidYouTubeUrlOrId(url.trim()), |
| 28 | + ); |
| 29 | + |
| 30 | + if (invalidUrls.length > 0) { |
| 31 | + errors.videos = [ |
| 32 | + 'The following values are not valid YouTube IDs or links.', |
| 33 | + ...invalidUrls, |
| 34 | + ].join(','); |
| 35 | + |
| 36 | + return errors; |
| 37 | + } |
| 38 | + }; |
| 39 | + |
8 | 40 | return ( |
9 | 41 | <Formik |
10 | | - initialValues={{ videos: [''] }} |
11 | | - onSubmit={(values) => { |
12 | | - void onSubmit(values.videos); |
13 | | - }}> |
14 | | - {({ values }) => ( |
15 | | - <Form className="space-y-4"> |
16 | | - <FieldArray name="videos"> |
17 | | - {({ insert, remove }) => ( |
18 | | - <div> |
19 | | - {values.videos.map((video, index) => ( |
20 | | - <div |
21 | | - key={index} |
22 | | - className="flex flex-wrap items-center gap-2"> |
23 | | - <Field |
24 | | - name={`videos.${index}`} |
25 | | - placeholder="Enter YouTube URL" |
26 | | - className="flex-1 px-3 py-2 border rounded shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-600" |
27 | | - /> |
28 | | - <button |
29 | | - type="button" |
30 | | - onClick={() => remove(index)} |
31 | | - className="px-3 py-1 bg-red-500 text-white rounded hover:bg-red-600 focus:outline-none"> |
32 | | - Remove |
33 | | - </button> |
34 | | - <button |
35 | | - type="button" |
36 | | - onClick={() => { |
37 | | - insert(index, ''); |
38 | | - }} |
39 | | - className="px-3 py-1 bg-green-500 text-white rounded hover:bg-green-600 focus:outline-none"> |
40 | | - Add |
41 | | - </button> |
42 | | - </div> |
43 | | - ))} |
44 | | - </div> |
45 | | - )} |
46 | | - </FieldArray> |
47 | | - <div className="flex justify-center"> |
| 42 | + initialValues={{ videos: '' }} |
| 43 | + onSubmit={async ({ videos }, { setSubmitting }) => { |
| 44 | + const videoIds = videos.split(',').map((url) => url.trim()); |
| 45 | + setSubmitting(true); |
| 46 | + try { |
| 47 | + await onSubmit(videoIds); |
| 48 | + } catch (e) {} |
| 49 | + setSubmitting(false); |
| 50 | + }} |
| 51 | + validate={validateYouTubeLinks}> |
| 52 | + {({ errors, touched, isSubmitting }) => ( |
| 53 | + <Form> |
| 54 | + <Field |
| 55 | + name="videos" |
| 56 | + as="textarea" |
| 57 | + placeholder="Enter comma-separated YouTube links or IDs, e.g., https://youtu.be/dQw4w9WgXcQ, 3fumBcKC6RE" |
| 58 | + className="w-full p-2 border rounded shadow-sm" |
| 59 | + /> |
| 60 | + {typeof errors.videos === 'string' && touched.videos === true && ( |
| 61 | + <ul> |
| 62 | + {errors.videos.split(',').map((error) => ( |
| 63 | + <li key={error}>{error}</li> |
| 64 | + ))} |
| 65 | + </ul> |
| 66 | + )} |
| 67 | + {isSubmitting |
| 68 | +? ( |
| 69 | + <button |
| 70 | + onClick={(e) => { |
| 71 | + e.preventDefault(); |
| 72 | + }} |
| 73 | + className="mt-2 bg-blue-500 text-white p-2 rounded"> |
| 74 | + <CircularProgress /> |
| 75 | + </button> |
| 76 | + ) |
| 77 | +: ( |
48 | 78 | <button |
49 | 79 | type="submit" |
50 | | - className="py-2 px-4 bg-indigo-600 text-white rounded hover:bg-indigo-700 focus:outline-none"> |
| 80 | + className="mt-2 bg-blue-500 text-white p-2 rounded"> |
51 | 81 | Submit |
52 | 82 | </button> |
53 | | - </div> |
| 83 | + )} |
54 | 84 | </Form> |
55 | 85 | )} |
56 | 86 | </Formik> |
|
0 commit comments