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 1 commit
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
26 changes: 12 additions & 14 deletions src/modules/AccountSettings/SettingNotif/SettingTelegram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -56,6 +54,7 @@ export default function SettingTelegram({
}) {
const { toast } = useToast()
const [isSubmitting, setIsSubmitting] = useState<boolean>(false)
mazipan marked this conversation as resolved.
Show resolved Hide resolved
const { mutate: mutateUpdateChannelNotif } = usePatchUpdateChannelNotif()
madjiebimaa marked this conversation as resolved.
Show resolved Hide resolved

const form = useForm<FormValues>({
resolver: valibotResolver(schema),
Expand All @@ -74,15 +73,14 @@ export default function SettingTelegram({
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 Down
19 changes: 19 additions & 0 deletions src/modules/AccountSettings/hooks/usePatchUpdateChannelNotif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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!`,
})
},
})
}