Skip to content

Commit

Permalink
fix: save voice configs
Browse files Browse the repository at this point in the history
  • Loading branch information
okradze committed Nov 29, 2023
1 parent bec6a1c commit 3c67f16
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions apps/ui/src/plugins/contact/pages/Voice/VoiceView/useVoiceView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useVoicesService } from 'plugins/contact/services/voice/useVoicesServic
import { useContext, useState } from 'react'
import { useParams } from 'react-router-dom'
import { useConfigsService } from 'services/config/useConfigsService'
import { useCreateConfigService } from 'services/config/useCreateConfigService'
import { ConfigInput, useCreateConfigService } from 'services/config/useCreateConfigService'
import { useUpdateConfigService } from 'services/config/useUpdateConfigService'

export const useVoiceView = ({ voiceSlug }: { voiceSlug?: string }) => {
Expand Down Expand Up @@ -43,10 +43,10 @@ export const useVoiceView = ({ voiceSlug }: { voiceSlug?: string }) => {
const handleSubmit = async (values: any) => {
setIsLoading(true)

const configs = []
const configs: ConfigInput[] = []

for (const key in values) {
const value = values[key]
const value = values[key] || ''
const field = voice?.fields.find((field: any) => field.key === key)

configs.push({
Expand All @@ -60,17 +60,18 @@ export const useVoiceView = ({ voiceSlug }: { voiceSlug?: string }) => {
}

try {
if (filteredConfig.length === 0) {
const promises = configs.map((config: any) => createConfig(config))
await Promise.all(promises)
} else {
const promises = configs.map((config: any) =>
updateConfig(filteredConfig.find((cfg: any) => cfg.key === config.key).id, config),
)

await Promise.all(promises)
}
const promises = voice?.fields.map((field: any) => {
const existingConfig = filteredConfig?.find((config: any) => config.key === field.key)
const config = configs.find((config: any) => config.key === field.key)

if (!config) return
if (!existingConfig) return createConfig(config)
return updateConfig(existingConfig.id, config)
})

await Promise.all(promises)
await refetchConfigs()

setToast({
message: 'Voice was updated!',
type: 'positive',
Expand Down

0 comments on commit 3c67f16

Please sign in to comment.