Skip to content

Commit

Permalink
fix(dashboard): yse ignoreUpdates for chat alerts autosave
Browse files Browse the repository at this point in the history
fixes #735
  • Loading branch information
Satont committed May 25, 2024
1 parent 6323253 commit ad79d5d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
7 changes: 2 additions & 5 deletions frontend/dashboard/src/api/chat-alerts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useQuery } from '@urql/vue'
import { createGlobalState } from '@vueuse/core'
import { computed } from 'vue'

import type { GetAllChatAlertsQuery } from '@/gql/graphql.js'

Expand All @@ -12,7 +11,7 @@ export type ChatAlerts = GetAllChatAlertsQuery['chatAlerts']
const invalidationKey = 'ChatAlertsInvalidateKey'

export const useChatAlertsApi = createGlobalState(() => {
const { data } = useQuery({
const useChatAlertsQuery = () => useQuery({
variables: {},
context: { additionalTypenames: [invalidationKey] },
query: graphql(`
Expand Down Expand Up @@ -127,8 +126,6 @@ export const useChatAlertsApi = createGlobalState(() => {
`),
})

const chatAlerts = computed<ChatAlerts>(() => data.value?.chatAlerts)

const useMutationUpdateChatAlerts = () => useMutation(graphql(`
mutation UpdateChatAlerts($input: ChatAlertsInput!) {
updateChatAlerts(input: $input) {
Expand All @@ -138,7 +135,7 @@ export const useChatAlertsApi = createGlobalState(() => {
`), [invalidationKey])

return {
chatAlerts,
useChatAlertsQuery,
useMutationUpdateChatAlerts,
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ defineSlots<{
additionalSettings: VNode
}>()
const { formValue, formInited, formRef } = useForm()
const { formValue, formRef } = useForm()
const hasAccessToManageAlerts = useUserAccessFlagChecker(ChannelRolePermissionEnum.ManageAlerts)
watch(formInited, (v) => {
watch(formValue, (v) => {
if (!v) return
if (!formValue?.value?.[props.formKey]?.messages.length) {
if (!v[props.formKey]?.messages.length) {
createMessage()
}
}, { immediate: true })
Expand Down
58 changes: 27 additions & 31 deletions frontend/dashboard/src/features/chat-alerts/composables/use-form.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createGlobalState, useDebounceFn, watchDebounced } from '@vueuse/core'
import { createGlobalState, useDebounceFn, watchIgnorable } from '@vueuse/core'
import { useNotification } from 'naive-ui'
import { ref, toRaw, watch } from 'vue'
import { useI18n } from 'vue-i18n'
Expand All @@ -17,7 +17,8 @@ export const useForm = createGlobalState(() => {
const { t } = useI18n()
const formRef = ref<HTMLFormElement>()

const { chatAlerts, useMutationUpdateChatAlerts } = useChatAlertsApi()
const { useChatAlertsQuery, useMutationUpdateChatAlerts } = useChatAlertsApi()
const { data } = useChatAlertsQuery()
const updateChatAlerts = useMutationUpdateChatAlerts()

const formValue = ref<Form>({
Expand Down Expand Up @@ -94,33 +95,6 @@ export const useForm = createGlobalState(() => {
},
})

const formInited = ref(false)

watch(chatAlerts, (v) => {
if (!v || formInited.value) return
formInited.value = false

for (const key of Object.keys(formValue.value) as FormKey[]) {
if (!v[key]) continue
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
formValue.value[key] = v[key]
}

formInited.value = true
}, { immediate: true })

watchDebounced(formValue, () => {
if (!formInited.value) {
return
}

if (!formRef.value) return
if (!formRef.value?.reportValidity()) return

save()
}, { deep: true, debounce: 500 })

async function save() {
const input = toRaw(formValue.value)
if (!input) return
Expand All @@ -135,11 +109,33 @@ export const useForm = createGlobalState(() => {
}
}

const debouncedSave = useDebounceFn(save, 500)
const debouncedSave = useDebounceFn(save, 1000)

const { ignoreUpdates } = watchIgnorable(
formValue,
() => {
if (!formRef.value) return
if (!formRef.value?.reportValidity()) return

debouncedSave()
},
{ deep: true, immediate: true },
)

watch(data, (v) => {
ignoreUpdates(() => {
if (!v?.chatAlerts) return
for (const key of Object.keys(formValue.value) as FormKey[]) {
if (!v.chatAlerts[key]) continue
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
formValue.value[key] = v.chatAlerts[key]
}
})
}, { immediate: true })

return {
formValue,
formInited,
save: debouncedSave,
formRef,
}
Expand Down

0 comments on commit ad79d5d

Please sign in to comment.