HomeImageBanner.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use client";
  2. import { Swiper, SwiperSlide } from 'swiper/react';
  3. import 'swiper/css';
  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 } from "@/graphql";
  7. import { useMutation, useLazyQuery } 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 { showToast } = useCustomToast();
  21. const apolloClient = makeClient();
  22. // const res = useQuery(GET_COUNTRY_STATES, {
  23. // variables: {
  24. // countryId: 1000
  25. // }
  26. // });
  27. // console.log('query state --- res', res);
  28. const ttt = async () => {
  29. try {
  30. const res = await apolloClient.mutate({
  31. mutation: GET_CART_ITEM,
  32. fetchPolicy: 'network-only', // 按需调整
  33. });
  34. console.log('----------',res);
  35. } catch(error) {
  36. console.log('ttt----------',error);
  37. handleApolloBusinessError(error,(err) => {
  38. showToast(err.message, "danger");
  39. });
  40. }
  41. }
  42. // 76|nlq9qGztR7qmSS72Kal56NCjhRRVrLCeVOdZdLLYa2c769e9
  43. const [getContent] = useLazyQuery(GET_FILTER_ATTRIBUTES,{
  44. fetchPolicy: 'network-only'
  45. });
  46. const testLazyQuery = async () => {
  47. try {
  48. const result = await getContent();
  49. console.log('lazy query res---', result);
  50. } catch(err) {
  51. console.log('lazy query ---- ', err);
  52. }
  53. }
  54. // useMutation
  55. const [mutateAsync] = useMutation(
  56. GET_CART_ITEM,
  57. {
  58. onCompleted: (res) => {
  59. console.log('useMutation onCompleted res ----- 0',res);
  60. },
  61. onError: (err) => {
  62. console.log('useMutation onCompleted err ----- 0',err);
  63. },
  64. },
  65. );
  66. const testMutation = async () => {
  67. const res = await mutateAsync();
  68. console.log('testMutation ---',res)
  69. };
  70. const testFilter= () => {
  71. return apolloClient.query({
  72. query: GET_FILTER_ATTRIBUTES,
  73. fetchPolicy: "no-cache",
  74. context: {
  75. fetchOptions: {
  76. cache: 'no-store',
  77. },
  78. },
  79. // variables: {
  80. // orderId: Number(orderid)
  81. // }
  82. }).then((res) => {
  83. console.log('testFilter res ---- ',res);
  84. }).catch((err) => {
  85. console.error('testFilter error ---- ',err);
  86. handleApolloBusinessError(err,(error) => {
  87. showToast(error.message, "danger");
  88. });
  89. });
  90. };
  91. const testApplyCoupon = async () => {
  92. try {
  93. await apolloClient.mutate({
  94. mutation: CREATE_APPLY_COUPON,
  95. variables: {
  96. couponCode: 'BIRTHDAY20'
  97. },
  98. fetchPolicy: 'network-only', // 按需调整
  99. });
  100. } catch (err) {
  101. console.log('testApplyCoupon error ---- ',err);
  102. }
  103. };
  104. return (
  105. <>
  106. <p>
  107. <button onClick={ttt}>get cart detail</button>
  108. </p>
  109. <p><button onClick={testLazyQuery}>testLazyQuery</button></p>
  110. <p><button onClick={testMutation}>testMutation</button></p>
  111. <p><button onClick={testFilter}>testFilter</button></p>
  112. <p><button onClick={testApplyCoupon}>testApplyCoupon</button></p>
  113. <Swiper
  114. spaceBetween={50}
  115. slidesPerView={3}
  116. onSlideChange={() => console.log('slide change')}
  117. onSwiper={(swiper) => console.log(swiper)}
  118. >
  119. <SwiperSlide>Slide 1</SwiperSlide>
  120. <SwiperSlide>Slide 2</SwiperSlide>
  121. <SwiperSlide>Slide 3</SwiperSlide>
  122. <SwiperSlide>Slide 4</SwiperSlide>
  123. </Swiper>
  124. <CurrencySwitch/>
  125. </>
  126. )
  127. }