generated from taylorbryant/gatsby-starter-tailwind
-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
viet nguyen
committed
Dec 17, 2023
1 parent
230bd78
commit d79432b
Showing
3 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/app/editArea/[slug]/addClimbs/components/AddClimbsForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use client' | ||
import { useSession } from 'next-auth/react' | ||
|
||
import { SingleEntryForm } from 'app/editArea/[slug]/components/SingleEntryForm' | ||
import { AREA_DESCRIPTION_FORM_VALIDATION_RULES } from '@/components/edit/EditAreaForm' | ||
import useUpdateAreasCmd from '@/js/hooks/useUpdateAreasCmd' | ||
import { MarkdownTextArea } from '@/components/ui/form/MarkdownTextArea' | ||
import useUpdateClimbsCmd from '@/js/hooks/useUpdateClimbsCmd' | ||
import { DashboardInput } from '@/components/ui/form/Input' | ||
import { DynamicClimbInputList } from './DynamicClimbInputList' | ||
/** | ||
* Area description edit form | ||
* @param param0 | ||
* @returns | ||
*/ | ||
export const AddClimbsForm: React.FC<{ parentAreaUuid: string }> = ({ parentAreaUuid }) => { | ||
const session = useSession({ required: true }) | ||
const { updateClimbCmd } = useUpdateClimbsCmd( | ||
{ | ||
parentId: parentAreaUuid, | ||
accessToken: session?.data?.accessToken as string | ||
} | ||
) | ||
|
||
return ( | ||
<SingleEntryForm<{ climbList: any[] }> | ||
title='Add climbs' | ||
initialValues={{ climbList: [{ climbName: '' }] }} | ||
// initialValues={{ description: initialValue }} | ||
submitHandler={async (data) => { | ||
console.log(data) | ||
// await updateClimbCmd({ description }) | ||
}} | ||
> | ||
<DynamicClimbInputList parentAreaUuid={parentAreaUuid} name='climbList' /> | ||
</SingleEntryForm> | ||
) | ||
} |
76 changes: 76 additions & 0 deletions
76
src/app/editArea/[slug]/addClimbs/components/DynamicClimbInputList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useCallback } from 'react' | ||
import { useFieldArray, useFormContext } from 'react-hook-form' | ||
import { ListPlus, Minus } from '@phosphor-icons/react/dist/ssr' | ||
|
||
import { BaseInput } from '@/components/ui/form/Input' | ||
|
||
export const DynamicClimbInputList: React.FC<{ parentAreaUuid: string, name: string }> = ({ parentAreaUuid, name = 'climbList' }) => { | ||
const { | ||
fields, | ||
append, | ||
remove | ||
} = useFieldArray({ | ||
name | ||
}) | ||
|
||
const addMoreFields = useCallback(() => { | ||
for (let i = 0; i < 5; i++) { | ||
append({ climbName: '' }) | ||
} | ||
}, [append]) | ||
|
||
return ( | ||
<div> | ||
<ul> | ||
{fields.map((item, index) => { | ||
return ( | ||
<li key={item.id}> | ||
|
||
<NewClimbInput | ||
parentAreaUuid={parentAreaUuid} index={index} count={fields.length} | ||
onRemove={(removeIndex) => remove(removeIndex)} | ||
/> | ||
|
||
</li> | ||
) | ||
})} | ||
</ul> | ||
|
||
<button | ||
type='button' | ||
className='mt-6 btn btn-sm btn-primary btn-outline' | ||
onClick={addMoreFields} | ||
disabled={fields.length >= 30} | ||
> | ||
<ListPlus size={18} /> Bulk entry | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
const NewClimbInput: React.FC<{ parentAreaUuid: string, index: number, count: number, onRemove: (index: number) => void }> = ({ parentAreaUuid, index, count, onRemove }) => { | ||
const formContext = useFormContext() | ||
const inputName = `climbList[${index}].climbName` | ||
return ( | ||
<div> | ||
<div className='form-control'> | ||
<label className='label' htmlFor={inputName}><span className='label-text text-secondary'>Climb name {count > 1 ? index + 1 : ''}</span></label> | ||
<div className='flex items-center gap-4'> | ||
<BaseInput | ||
formContext={formContext} | ||
name={inputName} | ||
label={`Climb #${index + 1}`} | ||
className='w-full' | ||
/> | ||
{count > 1 && | ||
<button | ||
type='button' | ||
className='btn btn-sm btn-outline btn-circle' | ||
onClick={() => onRemove(index)} | ||
><Minus size={18} /> | ||
</button>} | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters