feat: auto form submission

This commit is contained in:
karishmas6
2024-10-11 06:14:40 +05:30
parent 44739eb65d
commit fb7a998175

View File

@@ -1,4 +1,4 @@
import { useState, useEffect, useCallback } from 'react'; import React, { useState, useEffect, useCallback } from 'react';
import type { SyntheticEvent } from 'react'; import type { SyntheticEvent } from 'react';
import KeyboardArrowRightIcon from '@mui/icons-material/KeyboardArrowRight'; import KeyboardArrowRightIcon from '@mui/icons-material/KeyboardArrowRight';
import { NavBarForm, NavBarInput } from "../atoms/form"; import { NavBarForm, NavBarInput } from "../atoms/form";
@@ -6,8 +6,6 @@ import { UrlFormButton } from "../atoms/buttons/buttons";
import { useSocketStore } from '../../context/socket'; import { useSocketStore } from '../../context/socket';
import { Socket } from "socket.io-client"; import { Socket } from "socket.io-client";
// TODO: Bring back REFRESH
type Props = { type Props = {
currentAddress: string; currentAddress: string;
handleRefresh: (socket: Socket) => void; handleRefresh: (socket: Socket) => void;
@@ -19,7 +17,6 @@ export const UrlForm = ({
handleRefresh, handleRefresh,
setCurrentAddress, setCurrentAddress,
}: Props) => { }: Props) => {
// Internal address state, initially set to currentAddress. We need this else the input field will not update with recordingUrl
const [address, setAddress] = useState<string>(currentAddress); const [address, setAddress] = useState<string>(currentAddress);
const { socket } = useSocketStore(); const { socket } = useSocketStore();
@@ -27,15 +24,7 @@ export const UrlForm = ({
setAddress((event.target as HTMLInputElement).value); setAddress((event.target as HTMLInputElement).value);
}, []); }, []);
const onSubmit = (event: SyntheticEvent): void => { const submitForm = useCallback((url: string): void => {
event.preventDefault();
let url = address;
// If no manual change, use the currentAddress prop
if (address === currentAddress) {
url = currentAddress;
}
// Add protocol if missing // Add protocol if missing
if (!/^(?:f|ht)tps?\:\/\//.test(url)) { if (!/^(?:f|ht)tps?\:\/\//.test(url)) {
url = "https://" + url; url = "https://" + url;
@@ -49,12 +38,20 @@ export const UrlForm = ({
} catch (e) { } catch (e) {
alert(`ERROR: ${url} is not a valid url!`); alert(`ERROR: ${url} is not a valid url!`);
} }
}, [setCurrentAddress]);
const onSubmit = (event: SyntheticEvent): void => {
event.preventDefault();
submitForm(address);
}; };
// Sync internal state with currentAddress prop when it changes // Sync internal state with currentAddress prop when it changes and auto-submit
useEffect(() => { useEffect(() => {
setAddress(currentAddress); setAddress(currentAddress);
}, [currentAddress]); if (currentAddress !== '') {
submitForm(currentAddress);
}
}, [currentAddress, submitForm]);
return ( return (
<NavBarForm onSubmit={onSubmit}> <NavBarForm onSubmit={onSubmit}>
@@ -68,4 +65,4 @@ export const UrlForm = ({
</UrlFormButton> </UrlFormButton>
</NavBarForm> </NavBarForm>
); );
}; };