|
| 1 | +import { useMutation, useQueryClient } from '@tanstack/react-query' |
| 2 | +import axios from 'axios' |
| 3 | +import { API_ROUTES, API_URL, SUCCESS_TIMEOUT } from 'data-services/constants' |
| 4 | +import { getAuthHeader } from 'data-services/utils' |
| 5 | +import { useUser } from 'utils/user/userContext' |
| 6 | + |
| 7 | +const convertToServerFieldValues = (fieldValues: any) => ({ |
| 8 | + settings: { |
| 9 | + session_time_gap_seconds: fieldValues.sessionTimeGapSeconds, |
| 10 | + default_filters_score_threshold: fieldValues.scoreThreshold, |
| 11 | + default_filters_include_taxa_ids: fieldValues.includeTaxa.map( |
| 12 | + (taxon: any) => taxon.id |
| 13 | + ), |
| 14 | + default_filters_exclude_taxa_ids: fieldValues.excludeTaxa.map( |
| 15 | + (taxon: any) => taxon.id |
| 16 | + ), |
| 17 | + }, |
| 18 | +}) |
| 19 | + |
| 20 | +export const useUpdateProjectSettings = (id: string) => { |
| 21 | + const { user } = useUser() |
| 22 | + const queryClient = useQueryClient() |
| 23 | + |
| 24 | + const { mutateAsync, isLoading, isSuccess, reset, error } = useMutation({ |
| 25 | + mutationFn: (fieldValues: any) => |
| 26 | + axios.patch( |
| 27 | + `${API_URL}/${API_ROUTES.PROJECTS}/${id}/`, |
| 28 | + convertToServerFieldValues(fieldValues), |
| 29 | + { |
| 30 | + headers: { |
| 31 | + ...getAuthHeader(user), |
| 32 | + }, |
| 33 | + } |
| 34 | + ), |
| 35 | + onSuccess: () => { |
| 36 | + queryClient.invalidateQueries([API_ROUTES.PROJECTS, id]) |
| 37 | + setTimeout(reset, SUCCESS_TIMEOUT) |
| 38 | + }, |
| 39 | + }) |
| 40 | + |
| 41 | + return { updateProjectSettings: mutateAsync, isLoading, isSuccess, error } |
| 42 | +} |
0 commit comments