HomeImageBanner.tsx 4.4 KB

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