2023-10-10 10:27:35 +03:00
|
|
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import { useParams } from "next/navigation";
|
|
|
|
|
|
|
|
import { ONBOARDING_DATA_KEY } from "@/lib/api/onboarding/config";
|
|
|
|
import { useOnboardingApi } from "@/lib/api/onboarding/useOnboardingApi";
|
|
|
|
|
|
|
|
import { Onboarding } from "../types/Onboarding";
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
export const useOnboarding = () => {
|
|
|
|
const { getOnboarding } = useOnboardingApi();
|
|
|
|
const params = useParams();
|
|
|
|
const { updateOnboarding } = useOnboardingApi();
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
const chatId = params?.chatId as string | undefined;
|
|
|
|
|
2023-10-11 10:46:57 +03:00
|
|
|
const { data } = useQuery({
|
2023-10-10 10:27:35 +03:00
|
|
|
queryFn: getOnboarding,
|
|
|
|
queryKey: [ONBOARDING_DATA_KEY],
|
|
|
|
});
|
|
|
|
|
2023-10-11 10:46:57 +03:00
|
|
|
const onboarding: Onboarding = data ?? {
|
|
|
|
onboarding_a: false,
|
|
|
|
onboarding_b1: false,
|
|
|
|
onboarding_b2: false,
|
|
|
|
onboarding_b3: false,
|
|
|
|
};
|
|
|
|
|
2023-10-11 16:56:28 +03:00
|
|
|
const isOnboarding = Object.values(onboarding).some((value) => value);
|
|
|
|
|
2023-10-10 10:27:35 +03:00
|
|
|
const updateOnboardingHandler = async (
|
|
|
|
newOnboardingStatus: Partial<Onboarding>
|
|
|
|
) => {
|
|
|
|
await updateOnboarding(newOnboardingStatus);
|
|
|
|
await queryClient.invalidateQueries({ queryKey: [ONBOARDING_DATA_KEY] });
|
|
|
|
};
|
|
|
|
|
2023-10-26 16:41:12 +03:00
|
|
|
const shouldDisplayWelcomeChat = onboarding.onboarding_a;
|
2023-10-10 10:27:35 +03:00
|
|
|
|
|
|
|
const shouldDisplayOnboardingAInstructions =
|
2023-10-26 16:41:12 +03:00
|
|
|
chatId === undefined && shouldDisplayWelcomeChat;
|
2023-10-10 10:27:35 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
onboarding,
|
|
|
|
shouldDisplayOnboardingAInstructions,
|
|
|
|
shouldDisplayWelcomeChat,
|
|
|
|
updateOnboarding: updateOnboardingHandler,
|
2023-10-11 16:56:28 +03:00
|
|
|
isOnboarding,
|
2023-10-10 10:27:35 +03:00
|
|
|
};
|
|
|
|
};
|