| 1 |
78a3e64 |
Dawsh |
2y ago |
import { NodeType } from "@marzneshin/modules/nodes"; |
| 2 |
b15ff05 |
Mrdn |
2y ago |
import { useMutation } from "@tanstack/react-query"; |
| 3 |
d636f3f |
Dawsh |
2y ago |
import { fetch, queryClient } from "@marzneshin/common/utils"; |
| 4 |
b15ff05 |
Mrdn |
2y ago |
import { toast } from "sonner"; |
| 5 |
b15ff05 |
Mrdn |
2y ago |
import i18n from "@marzneshin/features/i18n"; |
| 6 |
b15ff05 |
Mrdn |
2y ago |
import { NodesQueryFetchKey } from "./nodes.query"; |
| 7 |
b15ff05 |
Mrdn |
2y ago |
|
| 8 |
b15ff05 |
Mrdn |
2y ago |
export async function fetchUpdateNode(node: NodeType): Promise<NodeType> { |
| 9 |
b15ff05 |
Mrdn |
2y ago |
return fetch(`/nodes/${node.id}`, { method: 'put', body: node }).then((node) => { |
| 10 |
b15ff05 |
Mrdn |
2y ago |
return node; |
| 11 |
b15ff05 |
Mrdn |
2y ago |
}); |
| 12 |
b15ff05 |
Mrdn |
2y ago |
} |
| 13 |
b15ff05 |
Mrdn |
2y ago |
|
| 14 |
b15ff05 |
Mrdn |
2y ago |
const handleError = (error: Error, value: NodeType) => { |
| 15 |
b15ff05 |
Mrdn |
2y ago |
toast.error( |
| 16 |
3b7feef |
Mrdn |
2y ago |
i18n.t('events.update.error', { name: value.name }), |
| 17 |
b15ff05 |
Mrdn |
2y ago |
{ |
| 18 |
b15ff05 |
Mrdn |
2y ago |
description: error.message |
| 19 |
b15ff05 |
Mrdn |
2y ago |
}) |
| 20 |
b15ff05 |
Mrdn |
2y ago |
} |
| 21 |
b15ff05 |
Mrdn |
2y ago |
|
| 22 |
b15ff05 |
Mrdn |
2y ago |
const handleSuccess = (value: NodeType) => { |
| 23 |
b15ff05 |
Mrdn |
2y ago |
toast.success( |
| 24 |
3b7feef |
Mrdn |
2y ago |
i18n.t('events.update.success.title', { name: value.name }), |
| 25 |
b15ff05 |
Mrdn |
2y ago |
{ |
| 26 |
3b7feef |
Mrdn |
2y ago |
description: i18n.t('events.update.success.desc') |
| 27 |
b15ff05 |
Mrdn |
2y ago |
}) |
| 28 |
b15ff05 |
Mrdn |
2y ago |
queryClient.invalidateQueries({ queryKey: [NodesQueryFetchKey] }) |
| 29 |
b15ff05 |
Mrdn |
2y ago |
} |
| 30 |
b15ff05 |
Mrdn |
2y ago |
|
| 31 |
b15ff05 |
Mrdn |
2y ago |
|
| 32 |
3b7feef |
Mrdn |
2y ago |
const NodesUpdateFetchKey = "nodes"; |
| 33 |
b15ff05 |
Mrdn |
2y ago |
|
| 34 |
b15ff05 |
Mrdn |
2y ago |
export const useNodesUpdateMutation = () => { |
| 35 |
b15ff05 |
Mrdn |
2y ago |
return useMutation({ |
| 36 |
b15ff05 |
Mrdn |
2y ago |
mutationKey: [NodesUpdateFetchKey], |
| 37 |
b15ff05 |
Mrdn |
2y ago |
mutationFn: fetchUpdateNode, |
| 38 |
b15ff05 |
Mrdn |
2y ago |
onError: handleError, |
| 39 |
b15ff05 |
Mrdn |
2y ago |
onSuccess: handleSuccess, |
| 40 |
b15ff05 |
Mrdn |
2y ago |
}) |
| 41 |
b15ff05 |
Mrdn |
2y ago |
} |