Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR: move to useMutation for update channel notification API #105

Merged
merged 6 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions src/modules/AccountSettings/SettingNotif/SettingTelegram.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { useEffect, useState } from 'react'
import { useEffect } from 'react'
import { useForm } from 'react-hook-form'

import { valibotResolver } from '@hookform/resolvers/valibot'
Expand All @@ -22,14 +22,12 @@ import {
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { useToast } from '@/components/ui/use-toast'
import {
getCheckChatId,
patchUpdateChannelNotif,
postAddNewChannelNotif,
} from '@/lib/api'
import { getCheckChatId, postAddNewChannelNotif } from '@/lib/api'
import { trackEvent } from '@/lib/firebase'
import { NotifChannel, UserProfile } from '@/lib/types'

import { usePatchUpdateChannelNotif } from '../hooks/usePatchUpdateChannelNotif'

const schema = object({
username: string('Username perlu disi terlebih dahulu.', [
minLength(2, 'Username butuh paling tidak 2 karakter.'),
Expand All @@ -55,7 +53,7 @@ export default function SettingTelegram({
existing: NotifChannel[] | null | undefined
}) {
const { toast } = useToast()
const [isSubmitting, setIsSubmitting] = useState<boolean>(false)
const { mutate: mutateUpdateChannelNotif } = usePatchUpdateChannelNotif()
madjiebimaa marked this conversation as resolved.
Show resolved Hide resolved

const form = useForm<FormValues>({
resolver: valibotResolver(schema),
Expand All @@ -70,19 +68,17 @@ export default function SettingTelegram({
async function onSubmit(data: FormValues) {
trackEvent('click update notif channel telegram')
if (user) {
setIsSubmitting(true)
try {
if (existing && existing.length > 0) {
// patch
await patchUpdateChannelNotif(user, {
uid: user?.uid,
slug: owner?.slug || '',
telegram_chat_id: data?.chatId || '',
telegram_username: data?.username,
})
toast({
title: 'Perubahan berhasil disimpan',
description: `Berhasil menyimpan perubahan setelan notifikasi ke Telegram!`,
mutateUpdateChannelNotif({
user,
param: {
uid: user?.uid,
slug: owner?.slug || '',
telegram_chat_id: data?.chatId || '',
telegram_username: data?.username,
},
})
} else {
// create
Expand All @@ -103,13 +99,11 @@ export default function SettingTelegram({
description: `Gagal saat mencoba menyimpan data, silahkan coba beberapa saat lagi!`,
})
}
setIsSubmitting(false)
}
}

async function handleCheckChatId() {
if (user) {
setIsSubmitting(true)
try {
const res = await getCheckChatId(user, form.getValues('username'))
form.setValue('chatId', `${res?.data?.message?.chat?.id || ''}`)
Expand All @@ -120,7 +114,6 @@ export default function SettingTelegram({
})
}
}
setIsSubmitting(false)
}

useEffect(() => {
Expand Down Expand Up @@ -187,7 +180,7 @@ export default function SettingTelegram({
<Button
type="button"
variant="outline"
disabled={!watchUsername || isSubmitting || isLoading}
disabled={!watchUsername || isLoading}
madjiebimaa marked this conversation as resolved.
Show resolved Hide resolved
onClick={handleCheckChatId}
className="shrink-0"
>
Expand All @@ -200,8 +193,8 @@ export default function SettingTelegram({
/>

<div className="mt-8">
<Button type="submit" disabled={isSubmitting || isLoading}>
{isSubmitting ? (
<Button type="submit" disabled={isLoading}>
madjiebimaa marked this conversation as resolved.
Show resolved Hide resolved
{isLoading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin shrink-0" />
<span>Menyimpan...</span>
Expand Down
25 changes: 25 additions & 0 deletions src/modules/AccountSettings/hooks/usePatchUpdateChannelNotif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useMutation } from '@tanstack/react-query'
import { User } from 'firebase/auth'

import { toast } from '@/components/ui/use-toast'
import { patchUpdateChannelNotif } from '@/lib/api'
import { CreateNotifChannelArgs } from '@/lib/types'

export const usePatchUpdateChannelNotif = () => {
return useMutation({
mutationFn: (body: { user: User; param: CreateNotifChannelArgs }) =>
patchUpdateChannelNotif(body.user, body.param),
onSuccess: () => {
toast({
title: 'Perubahan berhasil disimpan',
description: `Berhasil menyimpan perubahan notifikasi ke Telegram!`,
})
},
onError: () => {
toast({
title: 'Gagal menyimpan',
description: `Gagal saat mencoba menyimpan data, silahkan coba beberapa saat lagi!`,
})
},
})
}