HomeImageBanner.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. "use client";
  2. import { useRouter } from 'next/navigation';
  3. import { Swiper, SwiperSlide } from 'swiper/react';
  4. import 'swiper/css';
  5. import {CurrencySwitch} from "@/components/common/CurrencySwitch/CurrencySwitch";
  6. import makeClient from "@/lib/ApolloClientBrowser";
  7. import { GET_CART_ITEM,GET_FILTER_ATTRIBUTES,CREATE_APPLY_COUPON, GET_COUNTRY_STATES} from "@/graphql";
  8. import { useMutation, useLazyQuery, useQuery } from "@apollo/client/react";
  9. import {handleApolloBusinessError} from "@/lib/ApolloErrorHandler";
  10. import { useCustomToast } from "@/utils/hooks/useToast";
  11. // interface ImageCarouselProps {
  12. // options: {
  13. // images: {
  14. // image: string;
  15. // link: string;
  16. // title?: string;
  17. // }[];
  18. // };
  19. // }
  20. export default function HomeImageBanner() {
  21. const router = useRouter();
  22. const { showToast } = useCustomToast();
  23. const apolloClient = makeClient();
  24. const res = useQuery(GET_COUNTRY_STATES, {
  25. variables: {
  26. countryId: 1000
  27. }
  28. });
  29. console.log('query state --- res', res);
  30. const ttt = async () => {
  31. try {
  32. const res = await apolloClient.mutate({
  33. mutation: GET_CART_ITEM,
  34. fetchPolicy: 'network-only', // 按需调整
  35. });
  36. console.log('----------',res);
  37. } catch(error) {
  38. console.log('ttt----------',error);
  39. handleApolloBusinessError(error,(err) => {
  40. showToast(err.message, "danger");
  41. });
  42. }
  43. }
  44. // 76|nlq9qGztR7qmSS72Kal56NCjhRRVrLCeVOdZdLLYa2c769e9
  45. const [getContent] = useLazyQuery(GET_FILTER_ATTRIBUTES,{
  46. fetchPolicy: 'network-only'
  47. });
  48. const testLazyQuery = async () => {
  49. try {
  50. const result = await getContent();
  51. console.log('lazy query res---', result);
  52. } catch(err) {
  53. console.log('lazy query ---- ', err);
  54. }
  55. }
  56. // useMutation
  57. const [mutateAsync] = useMutation(
  58. GET_CART_ITEM,
  59. {
  60. onCompleted: (res) => {
  61. console.log('useMutation onCompleted res ----- 0',res);
  62. },
  63. onError: (err) => {
  64. console.log('useMutation onCompleted err ----- 0',err);
  65. },
  66. },
  67. );
  68. const testMutation = async () => {
  69. const res = await mutateAsync();
  70. console.log('testMutation ---',res)
  71. };
  72. const testFilter= () => {
  73. return apolloClient.query({
  74. query: GET_FILTER_ATTRIBUTES,
  75. fetchPolicy: "no-cache",
  76. context: {
  77. fetchOptions: {
  78. cache: 'no-store',
  79. },
  80. },
  81. // variables: {
  82. // orderId: Number(orderid)
  83. // }
  84. }).then((res) => {
  85. console.log('testFilter res ---- ',res);
  86. }).catch((err) => {
  87. console.error('testFilter error ---- ',err);
  88. handleApolloBusinessError(err,(error) => {
  89. showToast(error.message, "danger");
  90. });
  91. });
  92. };
  93. const testApplyCoupon = async () => {
  94. try {
  95. await apolloClient.mutate({
  96. mutation: CREATE_APPLY_COUPON,
  97. variables: {
  98. couponCode: 'BIRTHDAY20'
  99. },
  100. fetchPolicy: 'network-only', // 按需调整
  101. });
  102. } catch (err) {
  103. console.log('testApplyCoupon error ---- ',err);
  104. }
  105. };
  106. const toggleLoginModal = () => {
  107. // openLoginModal();
  108. router.refresh();
  109. };
  110. return (
  111. <>
  112. <p>
  113. <button onClick={ttt}>get cart detail</button>
  114. </p>
  115. <p><button onClick={testLazyQuery}>testLazyQuery</button></p>
  116. <p><button onClick={testMutation}>testMutation</button></p>
  117. <p><button onClick={testFilter}>testFilter</button></p>
  118. <p><button onClick={testApplyCoupon}>testApplyCoupon</button></p>
  119. <Swiper
  120. spaceBetween={50}
  121. slidesPerView={3}
  122. onSlideChange={() => console.log('slide change')}
  123. onSwiper={(swiper) => console.log(swiper)}
  124. >
  125. <SwiperSlide>Slide 1</SwiperSlide>
  126. <SwiperSlide>Slide 2</SwiperSlide>
  127. <SwiperSlide>Slide 3</SwiperSlide>
  128. <SwiperSlide>Slide 4</SwiperSlide>
  129. </Swiper>
  130. <CurrencySwitch/>
  131. <p><button onClick={() => toggleLoginModal()}>show Login Modal</button></p>
  132. </>
  133. )
  134. }