import { Button, Container, Form } from "react-bootstrap"; import { MusicianObj } from "../../Musicians/Musician/Musician"; import { GroupObj } from "../../Group/Group"; import { patchMusician, patchGroup } from "../../api"; import { useState } from "react"; interface EditBioFormProps { entity: MusicianObj | GroupObj; hideModal: React.Dispatch>; onBioChange: () => void; setGroup?: React.Dispatch>; setMusician?: React.Dispatch>; token: string; livestream_id?: string; } function EditBioForm(props: EditBioFormProps) { const [formBio, setFormBio] = useState(props.entity.bio); const [formLivestreamId, setFormLivestreamId] = useState( props.livestream_id, ); const [canSubmit, setCanSubmit] = useState(false); const [error, setError] = useState(""); const handleBioChange = (event: React.ChangeEvent) => { setFormBio(event.target.value); setCanSubmit(true); }; const handleLivestreamChange = ( event: React.ChangeEvent, ) => { setFormLivestreamId(event.target.value); setCanSubmit(true); }; const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); if (props.entity instanceof MusicianObj) { updateMusician(props.token, props.entity); } else if (props.entity instanceof GroupObj) { updateGroup(props.token, props.entity); } else { console.error("Invalid entity type"); } props.onBioChange(); props.hideModal(false); }; const updateMusician = async ( accessToken: string, musician: MusicianObj, ): Promise => { patchMusician( musician.id, formBio, musician.name, musician.headshot_id, accessToken, ) .then((patchedMusician) => { if (props.setMusician) { props.setMusician(patchedMusician); } }) .catch((error) => { setError("Failed to update bio: " + error.response.data.detail); }); }; const updateGroup = async ( accessToken: string, group: GroupObj, ): Promise => { const livestream_id = formLivestreamId ? formLivestreamId : ""; patchGroup(group.id, formBio, livestream_id, group.name, accessToken) .then((patchedGroup) => { if (props.setGroup) { props.setGroup(patchedGroup); } }) .catch((error) => { console.error(error); setError("Failed to update bio: " + error.response.data.detail); }); }; const SubmitButton = canSubmit ? ( ) : ( ); return (
{/* need to account for empty string, which is falsy but the field should still show */} {props.livestream_id != undefined && (

A livestream id is part of a youtube url. Either ".../v=[livestream_id]" or ".../live/[livestream_id]". For example, "ncyl7cTU9k8" but without the quotations. Don't mess it up.{" "}

To remove an embedded livestream, just clear this field and submit the form.

Livestream ID:
)} Bio {error && ( {error} )} {SubmitButton}
); } export default EditBioForm;