diff --git a/src/components/country/CountrySettings.jsx b/src/components/country/CountrySettings.jsx index f9d6532..c397480 100644 --- a/src/components/country/CountrySettings.jsx +++ b/src/components/country/CountrySettings.jsx @@ -1,13 +1,17 @@ -import { useState } from "react"; -import { useQuery } from '@tanstack/react-query' +import { useEffect, useState } from "react"; +import { useQuery, useQueryClient, useMutation } from '@tanstack/react-query' import queryKeys from '../../services/queryKeys' import BreadcrumbCom from "../breadcrumb/BreadcrumbCom"; -import { getCountry } from "../../services/siteServices"; +import { getCountry, setCountry } from "../../services/siteServices"; import TableWrapper from "../tableWrapper/TableWrapper"; export default function CountrySettings(){ - const {data, isFetching, isError, error} = useQuery({ + const [selected, setSelected] = useState('') + + const queryClient = useQueryClient() + + const {data, isFetching, status, isError, error} = useQuery({ queryKey: queryKeys.country_list, queryFn: () => { const reqData = { @@ -16,19 +20,52 @@ export default function CountrySettings(){ } return getCountry(reqData) }, - staleTime: 0 //0 mins + // placeholderData: (previousData, previousQuery) => previousData, + staleTime: 0 // 0 mins }) const countryData = data?.data?.country_data // COUNTRY LIST + const statusChange = useMutation({ + mutationFn: (fields) => { + return setCountry(fields) + }, + onError: (error) => { + }, + onSuccess: (res) => { + queryClient.refetchQueries({ + queryKey: [...queryKeys.country_list], + // type: 'active', + // exact: true, + }) + }, + onSettled: () => { + setSelected('') + } + }) + + //FUNCTION TO CHANGE STATUS OR SIGNUP + const handleStatusChange = (event, details) => { + setSelected(event.target.id) + const name = event.target.name + const val = name.toLowerCase() == 'STATUS' ? details.status : details.signup + const reqData = { + 'val_type': name.toUpperCase(), + 'country_uid': details?.country_uid, + 'code': details?.code, + 'val': val == 0 ? 1 : 0 + } + statusChange.mutate(reqData) + }; + return (
-
+
<> - {isFetching ? + {status === 'pending' ?

Loading...

: isError ?

{error.message}

@@ -67,25 +104,52 @@ export default function CountrySettings(){
-
- +
+ <> + {selected == 'status' + index ? +
+ + Loading... +
+ : + handleStatusChange(e, item)} + type="checkbox" + checked={item?.status} + name="status" + id={'status' + index} + /> + } +
-
- +
+ <> + {selected == `signup${index}` + ? +
+ + Loading... +
+ : + handleStatusChange(e, item)} + type="checkbox" + checked={item?.signup} + name="signup" + id={'signup' + index } + /> + } +
@@ -105,6 +169,11 @@ export default function CountrySettings(){ )} } + {/* {(isFetching && status != 'pending') && +
+

Loading...

+
+ } */}
diff --git a/src/services/siteServices.js b/src/services/siteServices.js index b833286..42b128b 100644 --- a/src/services/siteServices.js +++ b/src/services/siteServices.js @@ -68,6 +68,14 @@ export const getCountry = (reqData) => { return getAuxEnd(`/country`, postData) } +// FUNCTION TO SET COUNTRY +export const setCountry = (reqData) => { + let postData = { + ...reqData + } + return postAuxEnd('/set-country', postData, false) +} + // FUNCTION TO GET BILLINGS export const getBillings = (reqData) => { const postData = { ...reqData }