Skip to content

Commit

Permalink
Populate fieldValue attribute on edit
Browse files Browse the repository at this point in the history
  • Loading branch information
steventux committed Feb 21, 2024
1 parent fe76bdc commit 4bf1ad0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
27 changes: 13 additions & 14 deletions app/profiles/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,19 @@ export default function ProfileCreate() {
<div className="m-auto w-full text-center mb-10">
<h1 className="text-xl font-bold">Add a new suspension profile</h1>
</div>

<LabelAndTextField fieldName={'name'} fieldSetter={setName} labelText={'Name'} />
<LabelAndTextField fieldName={'forkPsi'} fieldSetter={setForkPsi} labelText={'Fork PSI'} />
<LabelAndTextField fieldName={'forkSag'} fieldSetter={setForkSag} labelText={'Fork sag'} />
<LabelAndTextField fieldName={'forkHsc'} fieldSetter={setForkHsc} labelText={'Fork HSC'} />
<LabelAndTextField fieldName={'forkLsc'} fieldSetter={setForkLsc} labelText={'Fork LSC'} />
<LabelAndTextField fieldName={'forkHsr'} fieldSetter={setForkHsr} labelText={'Fork HSR'} />
<LabelAndTextField fieldName={'forkLsr'} fieldSetter={setForkLsr} labelText={'Fork LSR'} />
<LabelAndTextField fieldName={'shockPsi'} fieldSetter={setShockPsi} labelText={'Shock PSI'} />
<LabelAndTextField fieldName={'shockSag'} fieldSetter={setShockSag} labelText={'Shock sag'} />
<LabelAndTextField fieldName={'shockHsc'} fieldSetter={setShockHsc} labelText={'Shock HSC'} />
<LabelAndTextField fieldName={'shockLsc'} fieldSetter={setShockLsc} labelText={'Shock LSC'} />
<LabelAndTextField fieldName={'shockHsr'} fieldSetter={setShockHsr} labelText={'Shock HSR'} />
<LabelAndTextField fieldName={'shockLsr'} fieldSetter={setShockLsr} labelText={'Shock LSR'} />
<LabelAndTextField fieldName={'name'} fieldValue={name} fieldSetter={setName} labelText={'Name'} />
<LabelAndTextField fieldName={'forkPsi'} fieldValue={forkPsi} fieldSetter={setForkPsi} labelText={'Fork PSI'} />
<LabelAndTextField fieldName={'forkSag'} fieldValue={forkSag} fieldSetter={setForkSag} labelText={'Fork sag'} />
<LabelAndTextField fieldName={'forkHsc'} fieldValue={forkHsc} fieldSetter={setForkHsc} labelText={'Fork HSC'} />
<LabelAndTextField fieldName={'forkLsc'} fieldValue={forkLsc} fieldSetter={setForkLsc} labelText={'Fork LSC'} />
<LabelAndTextField fieldName={'forkHsr'} fieldValue={forkHsr} fieldSetter={setForkHsr} labelText={'Fork HSR'} />
<LabelAndTextField fieldName={'forkLsr'} fieldValue={forkLsr} fieldSetter={setForkLsr} labelText={'Fork LSR'} />
<LabelAndTextField fieldName={'shockPsi'} fieldValue={shockPsi} fieldSetter={setShockPsi} labelText={'Shock PSI'} />
<LabelAndTextField fieldName={'shockSag'} fieldValue={shockSag} fieldSetter={setShockSag} labelText={'Shock sag'} />
<LabelAndTextField fieldName={'shockHsc'} fieldValue={shockHsc} fieldSetter={setShockHsc} labelText={'Shock HSC'} />
<LabelAndTextField fieldName={'shockLsc'} fieldValue={shockLsc} fieldSetter={setShockLsc} labelText={'Shock LSC'} />
<LabelAndTextField fieldName={'shockHsr'} fieldValue={shockHsr} fieldSetter={setShockHsr} labelText={'Shock HSR'} />
<LabelAndTextField fieldName={'shockLsr'} fieldValue={shockLsr} fieldSetter={setShockLsr} labelText={'Shock LSR'} />

<Submit />
</form>
Expand Down
64 changes: 33 additions & 31 deletions app/profiles/edit/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import LabelAndTextField from '@/app/components/form/LabelAndTextField';
import Submit from '@/app/components/form/Submit';

export default function ProfileEdit({params} :{params:{id:number}}) {
const router = useRouter()
const router = useRouter();
const [name, setName] = useState<string>('');
const [forkPsi, setForkPsi] = useState<string>('');
const [forkSag, setForkSag] = useState<string>('');
Expand All @@ -23,24 +23,26 @@ export default function ProfileEdit({params} :{params:{id:number}}) {
const [shockHsr, setShockHsr] = useState<string>('');
const [shockLsr, setShockLsr] = useState<string>('');

const {data : profile, isLoading, error} = useSWR(`/api/profiles/${params.id}`,fetcher)
const {data: profile, isLoading, error} = useSWR(`/api/profiles/${params.id}`,fetcher);

useEffect(()=>{
if(profile){
setName(profile.name)
setForkPsi(profile.forkPsi)
setForkSag(profile.forkSag)
setForkHsc(profile.forkHsc)
setForkLsc(profile.forkLsc)
setForkHsr(profile.forkHsr)
setForkLsr(profile.forkLsr)
setShockPsi(profile.shockPsi)
setShockSag(profile.shockSag)
setShockHsc(profile.shockHsc)
setShockLsc(profile.shockLsc)
setShockHsr(profile.shockHsr)
setShockLsr(profile.shockLsr)
}
if (profile){
setName(profile.name)
setForkPsi(profile.forkPsi)
setForkSag(profile.forkSag)
setForkHsc(profile.forkHsc)
setForkLsc(profile.forkLsc)
setForkHsr(profile.forkHsr)
setForkLsr(profile.forkLsr)
setShockPsi(profile.shockPsi)
setShockSag(profile.shockSag)
setShockHsc(profile.shockHsc)
setShockLsc(profile.shockLsc)
setShockHsr(profile.shockHsr)
setShockLsr(profile.shockLsr)
}
},[profile, isLoading])

const updateProfile = async (e: any) => {
e.preventDefault()
if (name != "" && forkPsi != "" && shockPsi != "") {
Expand All @@ -67,7 +69,7 @@ export default function ProfileEdit({params} :{params:{id:number}}) {
body: JSON.stringify(formData)
});
const content = await res.json();
if(content.success>0)
if(content.success > 0)
{
router.push('/profiles');
}
Expand All @@ -80,19 +82,19 @@ export default function ProfileEdit({params} :{params:{id:number}}) {
return (
<form className="w-full bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4" onSubmit={updateProfile}>
<h1 className="text-xl font-bold">Edit profile</h1>
<LabelAndTextField fieldName={'name'} fieldSetter={setName} labelText={'Name'} />
<LabelAndTextField fieldName={'forkPsi'} fieldSetter={setForkPsi} labelText={'Fork PSI'} />
<LabelAndTextField fieldName={'forkSag'} fieldSetter={setForkSag} labelText={'Fork sag'} />
<LabelAndTextField fieldName={'forkHsc'} fieldSetter={setForkHsc} labelText={'Fork HSC'} />
<LabelAndTextField fieldName={'forkLsc'} fieldSetter={setForkLsc} labelText={'Fork LSC'} />
<LabelAndTextField fieldName={'forkHsr'} fieldSetter={setForkHsr} labelText={'Fork HSR'} />
<LabelAndTextField fieldName={'forkLsr'} fieldSetter={setForkLsr} labelText={'Fork LSR'} />
<LabelAndTextField fieldName={'shockPsi'} fieldSetter={setShockPsi} labelText={'Shock PSI'} />
<LabelAndTextField fieldName={'shockSag'} fieldSetter={setShockSag} labelText={'Shock sag'} />
<LabelAndTextField fieldName={'shockHsc'} fieldSetter={setShockHsc} labelText={'Shock HSC'} />
<LabelAndTextField fieldName={'shockLsc'} fieldSetter={setShockLsc} labelText={'Shock LSC'} />
<LabelAndTextField fieldName={'shockHsr'} fieldSetter={setShockHsr} labelText={'Shock HSR'} />
<LabelAndTextField fieldName={'shockLsr'} fieldSetter={setShockLsr} labelText={'Shock LSR'} />
<LabelAndTextField fieldName={'name'} fieldValue={name} fieldSetter={setName} labelText={'Name'} />
<LabelAndTextField fieldName={'forkPsi'} fieldValue={forkPsi} fieldSetter={setForkPsi} labelText={'Fork PSI'} />
<LabelAndTextField fieldName={'forkSag'} fieldValue={forkSag} fieldSetter={setForkSag} labelText={'Fork sag'} />
<LabelAndTextField fieldName={'forkHsc'} fieldValue={forkHsc} fieldSetter={setForkHsc} labelText={'Fork HSC'} />
<LabelAndTextField fieldName={'forkLsc'} fieldValue={forkLsc} fieldSetter={setForkLsc} labelText={'Fork LSC'} />
<LabelAndTextField fieldName={'forkHsr'} fieldValue={forkHsr} fieldSetter={setForkHsr} labelText={'Fork HSR'} />
<LabelAndTextField fieldName={'forkLsr'} fieldValue={forkLsr} fieldSetter={setForkLsr} labelText={'Fork LSR'} />
<LabelAndTextField fieldName={'shockPsi'} fieldValue={shockPsi} fieldSetter={setShockPsi} labelText={'Shock PSI'} />
<LabelAndTextField fieldName={'shockSag'} fieldValue={shockSag} fieldSetter={setShockSag} labelText={'Shock sag'} />
<LabelAndTextField fieldName={'shockHsc'} fieldValue={shockHsc} fieldSetter={setShockHsc} labelText={'Shock HSC'} />
<LabelAndTextField fieldName={'shockLsc'} fieldValue={shockLsc} fieldSetter={setShockLsc} labelText={'Shock LSC'} />
<LabelAndTextField fieldName={'shockHsr'} fieldValue={shockHsr} fieldSetter={setShockHsr} labelText={'Shock HSR'} />
<LabelAndTextField fieldName={'shockLsr'} fieldValue={shockLsr} fieldSetter={setShockLsr} labelText={'Shock LSR'} />

<Submit />
</form>
Expand Down

0 comments on commit 4bf1ad0

Please sign in to comment.