Skip to content

Commit

Permalink
fix(error): errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Jan 10, 2025
1 parent 8000fd5 commit 4f82a8a
Show file tree
Hide file tree
Showing 3 changed files with 354 additions and 346 deletions.
Original file line number Diff line number Diff line change
@@ -1,97 +1,99 @@
import { Button } from '@mui/material'
import { FC, useEffect, useState } from 'react'
import { FormProvider, useForm } from 'react-hook-form'
import { ClipLoader } from 'react-spinners'
import { BaseModal } from '~/components/Modal'
import { putNodeData } from '~/network/fetchSourcesData'
import { useModal } from '~/stores/useModalStore'
import { useTopicsStore } from '~/stores/useTopicsStore'
import { Topic } from '~/types'
import { colors } from '~/utils/colors'
import { TitleEditor } from './Title'
import styled from 'styled-components'

type Props = {
topic: Topic
onClose: () => void
}

export type FormData = {
name: string
}

export const EditTopicModal: FC<Props> = ({ topic, onClose }) => {
const { close } = useModal('editTopic')
const [data] = useTopicsStore((s) => [s.data])
const form = useForm<FormData>({ mode: 'onChange' })
const { watch, setValue, reset, getValues } = form
const [loading, setLoading] = useState(false)

useEffect(() => {
if (topic) {
setValue('name', topic?.name)
}

return () => {
reset()
}
}, [topic, setValue, reset])

const nameValue = watch('name')
const name = nameValue?.trim()

const closeHandler = () => {
onClose()
close()
}

const handleSave = async () => {
setLoading(true)

try {
await putNodeData(topic?.ref_id, { node_type: topic?.node_type, node_data: { name } })

if (data) {
const newData = { ...data }

newData[topic?.ref_id].name = name

useTopicsStore.setState({ data: newData })
}

closeHandler()
} catch (error) {
console.warn(error)
} finally {
setLoading(false)
}
}

const isTopicNameChanged = getValues().name && topic?.name !== getValues().name

return (
<BaseModal id="editTopic" kind="regular" onClose={closeHandler} preventOutsideClose>
<FormProvider {...form}>
<TitleEditor />
<Button
color="secondary"
disabled={loading || !name || !isTopicNameChanged}
onClick={handleSave}
size="large"
variant="contained"
>
Save Changes
{loading && (
<ClipLoaderWrapper>
<ClipLoader color={colors.lightGray} size={12} />
</ClipLoaderWrapper>
)}
</Button>
</FormProvider>
</BaseModal>
)
}

const ClipLoaderWrapper = styled.span`
margin-top: 2px;
`
import { Button } from '@mui/material'
import { FC, useEffect, useState } from 'react'
import { FormProvider, useForm } from 'react-hook-form'
import { ClipLoader } from 'react-spinners'
import styled from 'styled-components'
import { BaseModal } from '~/components/Modal'
import { putNodeData } from '~/network/fetchSourcesData'
import { useModal } from '~/stores/useModalStore'
import { useTopicsStore } from '~/stores/useTopicsStore'
import { Topic } from '~/types'
import { colors } from '~/utils/colors'
import { TitleEditor } from './Title'

type Props = {
topic: Topic
onClose: () => void
}

export type FormData = {
name: string
}

export const EditTopicModal: FC<Props> = ({ topic, onClose }) => {
const { close } = useModal('editTopic')
const [data] = useTopicsStore((s) => [s.data])
const form = useForm<FormData>({ mode: 'onChange' })
const { watch, setValue, reset, getValues } = form
const [loading, setLoading] = useState(false)

useEffect(() => {
if (topic) {
setValue('name', topic?.name)
}

return () => {
reset()
}
}, [topic, setValue, reset])

const nameValue = watch('name')
const name = nameValue?.trim()

const closeHandler = () => {
onClose()
close()
}

const handleSave = async () => {
setLoading(true)

try {
await putNodeData(topic?.ref_id, {
node_type: topic?.node_type,
properties: { name }
})

if (data) {
const newData = { ...data }

newData[topic?.ref_id].name = name
useTopicsStore.setState({ data: newData })
}

closeHandler()
} catch (error) {
console.warn(error)
} finally {
setLoading(false)
}
}

const isTopicNameChanged = getValues().name && topic?.name !== getValues().name

return (
<BaseModal id="editTopic" kind="regular" onClose={closeHandler} preventOutsideClose>
<FormProvider {...form}>
<TitleEditor />
<Button
color="secondary"
disabled={loading || !name || !isTopicNameChanged}
onClick={handleSave}
size="large"
variant="contained"
>
Save Changes
{loading && (
<ClipLoaderWrapper>
<ClipLoader color={colors.lightGray} size={12} />
</ClipLoaderWrapper>
)}
</Button>
</FormProvider>
</BaseModal>
)
}

const ClipLoaderWrapper = styled.span`
margin-top: 2px;
`
Loading

0 comments on commit 4f82a8a

Please sign in to comment.