Implement dynamic selection of primary domain name (#32)

* Add dynamic selection of primary domain

* Update alert message

* Fix alert message

* Update alert message

---------

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
This commit is contained in:
Nabarun Gogoi 2023-12-28 12:11:46 +05:30 committed by GitHub
parent f870ab90f7
commit 42ec846ff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import React from 'react'; import React, { useEffect, useState } from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { import {
@ -15,6 +15,7 @@ const SetupDomain = () => {
handleSubmit, handleSubmit,
formState: { isValid }, formState: { isValid },
watch, watch,
setValue,
} = useForm({ } = useForm({
defaultValues: { defaultValues: {
domainName: '', domainName: '',
@ -22,6 +23,25 @@ const SetupDomain = () => {
}, },
}); });
const [domainStr, setDomainStr] = useState('');
useEffect(() => {
const subscription = watch((value, { name }) => {
if (name === 'domainName' && value.domainName) {
const domainArr = value.domainName.split('www.');
setDomainStr(domainArr.length > 1 ? domainArr[1] : domainArr[0]);
if (value.domainName.startsWith('www.')) {
setValue('isWWW', 'true');
} else {
setValue('isWWW', 'false');
}
}
});
return () => subscription.unsubscribe();
}, [watch]);
return ( return (
<form <form
onSubmit={handleSubmit(() => {})} onSubmit={handleSubmit(() => {})}
@ -53,14 +73,14 @@ const SetupDomain = () => {
<Typography>Primary domain</Typography> <Typography>Primary domain</Typography>
<div className="flex flex-col gap-3"> <div className="flex flex-col gap-3">
<Radio <Radio
label={watch('domainName')} label={domainStr}
crossOrigin={undefined} crossOrigin={undefined}
{...register('isWWW')} {...register('isWWW')}
value="false" value="false"
type="radio" type="radio"
/> />
<Radio <Radio
label={`www.${watch('domainName')}`} label={`www.${domainStr}`}
crossOrigin={undefined} crossOrigin={undefined}
{...register('isWWW')} {...register('isWWW')}
value="true" value="true"
@ -68,8 +88,11 @@ const SetupDomain = () => {
/> />
</div> </div>
<Alert color="blue"> <Alert color="blue">
<i>^</i> For simplicity, well redirect the www variant to{' '} <i>^</i> For simplicity, well redirect the{' '}
{watch('domainName')}. Redirect preferences can be changed later. {watch('isWWW') === 'true'
? `non-www variant to www.${domainStr}`
: `www variant to ${domainStr}`}
. Redirect preferences can be changed later
</Alert> </Alert>
</div> </div>
)} )}