Skip to content

Commit

Permalink
Fix buggy nodes lookup in NodesMultiSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Feb 5, 2024
1 parent 4e9ecc5 commit e417e60
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
8 changes: 4 additions & 4 deletions resources/scripts/api/admin/nodes/useNodesSWR.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import useSWR from 'swr'

import getNodes, { NodeResponse, QueryParams } from '@/api/admin/nodes/getNodes'
import getNodes, {NodeResponse, QueryParams} from '@/api/admin/nodes/getNodes'


const useNodesSWR = ({ page, query, id, cotermId, ...params }: QueryParams) => {
const useNodesSWR = ({page, query, id, cotermId, ...params}: QueryParams) => {
return useSWR<NodeResponse>(
['admin:nodes', page, query, Boolean(id), cotermId],
() => getNodes({ page, query, id, cotermId, ...params })
['admin:nodes', page, query, id, cotermId],
() => getNodes({page, query, id, cotermId, ...params})
)
}

Expand Down
116 changes: 58 additions & 58 deletions resources/scripts/components/admin/coterms/EditCotermModal.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useFlashKey } from '@/util/useFlash'
import { port } from '@/util/validation'
import { zodResolver } from '@hookform/resolvers/zod'
import { useEffect } from 'react'
import { FormProvider, useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { KeyedMutator } from 'swr'
import { z } from 'zod'

import { Coterm, CotermResponse } from '@/api/admin/coterms/getCoterms'
import {useFlashKey} from '@/util/useFlash'
import {port} from '@/util/validation'
import {zodResolver} from '@hookform/resolvers/zod'
import {useEffect} from 'react'
import {FormProvider, useForm} from 'react-hook-form'
import {useTranslation} from 'react-i18next'
import {KeyedMutator} from 'swr'
import {z} from 'zod'

import {Coterm, CotermResponse} from '@/api/admin/coterms/getCoterms'
import updateCoterm from '@/api/admin/coterms/updateCoterm'
import useAttachedNodes from '@/api/admin/coterms/useAttachedNodes'

Expand All @@ -25,12 +25,12 @@ interface Props {
mutate: KeyedMutator<CotermResponse>
}

const EditCotermModal = ({ coterm, onClose, mutate }: Props) => {
const { t: tStrings } = useTranslation('strings')
const { clearFlashes, clearAndAddHttpError } = useFlashKey(
`admin.coterms.${coterm?.id}.update`
const EditCotermModal = ({coterm, onClose, mutate}: Props) => {
const {t: tStrings} = useTranslation('strings')
const {clearFlashes, clearAndAddHttpError} = useFlashKey(
`admin.coterms.${coterm?.id}.update`
)
const { data: attachedNodes } = useAttachedNodes(coterm?.id ?? -1, {})
const {data: attachedNodes} = useAttachedNodes(coterm ? coterm.id : -1, {})

const schema = z.object({
name: z.string().min(1).max(191),
Expand Down Expand Up @@ -67,7 +67,7 @@ const EditCotermModal = ({ coterm, onClose, mutate }: Props) => {
isTlsEnabled: old.isTlsEnabled,
fqdn: old.fqdn,
port: old.port,
nodeIds: attachedNodes?.items.map(node => node.id.toString()) ?? [],
nodeIds: attachedNodes ? attachedNodes.items.map(node => node.id.toString()) : [],
}))
}, [attachedNodes])

Expand All @@ -81,14 +81,14 @@ const EditCotermModal = ({ coterm, onClose, mutate }: Props) => {

clearFlashes()
try {
const updatedCoterm = await updateCoterm(coterm.id, data)
const updatedCoterm = await updateCoterm(coterm!.id, data)
mutate(data => {
if (!data) return data

return {
...data,
items: data.items.map(item =>
item.id === updatedCoterm.id ? updatedCoterm : item
item.id === updatedCoterm.id ? updatedCoterm : item
),
}
}, false)
Expand All @@ -99,47 +99,47 @@ const EditCotermModal = ({ coterm, onClose, mutate }: Props) => {
}

return (
<Modal open={Boolean(coterm)} onClose={handleClose}>
<Modal.Header>
<Modal.Title>Edit {coterm?.name}</Modal.Title>
</Modal.Header>

<FormProvider {...form}>
<form onSubmit={form.handleSubmit(submit)}>
<Modal.Body>
<FlashMessageRender
className='mb-5'
byKey={`admin.coterms.${coterm?.id}.update`}
/>
<TextInputForm name='name' label={tStrings('name')} />
<div className={'space-y-3'}>
<TextInputForm
name='fqdn'
label={tStrings('fqdn')}
<Modal open={Boolean(coterm)} onClose={handleClose}>
<Modal.Header>
<Modal.Title>Edit {coterm?.name}</Modal.Title>
</Modal.Header>

<FormProvider {...form}>
<form onSubmit={form.handleSubmit(submit)}>
<Modal.Body>
<FlashMessageRender
className='mb-5'
byKey={`admin.coterms.${coterm?.id}.update`}
/>
<CheckboxForm
name={'isTlsEnabled'}
label={'Is TLS Enabled?'}
/>
</div>
<TextInputForm name='port' label={tStrings('port')} />
<CotermNodesMultiSelectForm />
</Modal.Body>

<Modal.Actions>
<Modal.Action type='button' onClick={handleClose}>
{tStrings('cancel')}
</Modal.Action>
<Modal.Action
type='submit'
loading={form.formState.isSubmitting}
>
{tStrings('save')}
</Modal.Action>
</Modal.Actions>
</form>
</FormProvider>
</Modal>
<TextInputForm name='name' label={tStrings('name')}/>
<div className={'space-y-3'}>
<TextInputForm
name='fqdn'
label={tStrings('fqdn')}
/>
<CheckboxForm
name={'isTlsEnabled'}
label={'Is TLS Enabled?'}
/>
</div>
<TextInputForm name='port' label={tStrings('port')}/>
<CotermNodesMultiSelectForm/>
</Modal.Body>

<Modal.Actions>
<Modal.Action type='button' onClick={handleClose}>
{tStrings('cancel')}
</Modal.Action>
<Modal.Action
type='submit'
loading={form.formState.isSubmitting}
>
{tStrings('save')}
</Modal.Action>
</Modal.Actions>
</form>
</FormProvider>
</Modal>
)
}

Expand Down

0 comments on commit e417e60

Please sign in to comment.