@reevit/react 0.2.7 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -277,8 +277,19 @@ function mapToPaymentIntent(response, config) {
277
277
  }
278
278
  function useReevit(options) {
279
279
  const { config, onSuccess, onError, onClose, onStateChange, apiBaseUrl } = options;
280
- const [state, dispatch] = useReducer(reevitReducer, initialState);
280
+ const [state, dispatch] = useReducer(reevitReducer, {
281
+ ...initialState,
282
+ status: config.initialPaymentIntent ? "ready" : "idle",
283
+ paymentIntent: config.initialPaymentIntent || null
284
+ });
281
285
  const apiClientRef = useRef(null);
286
+ const initializingRef = useRef(!!config.initialPaymentIntent);
287
+ useEffect(() => {
288
+ if (config.initialPaymentIntent && (!state.paymentIntent || state.paymentIntent.id !== config.initialPaymentIntent.id)) {
289
+ dispatch({ type: "INIT_SUCCESS", payload: config.initialPaymentIntent });
290
+ initializingRef.current = true;
291
+ }
292
+ }, [config.initialPaymentIntent, state.paymentIntent?.id]);
282
293
  if (!apiClientRef.current) {
283
294
  apiClientRef.current = new ReevitAPIClient({
284
295
  publicKey: config.publicKey,
@@ -290,10 +301,15 @@ function useReevit(options) {
290
301
  }, [state.status, onStateChange]);
291
302
  const initialize = useCallback(
292
303
  async (method) => {
304
+ if (initializingRef.current) {
305
+ return;
306
+ }
307
+ initializingRef.current = true;
293
308
  dispatch({ type: "INIT_START" });
294
309
  try {
295
310
  const apiClient = apiClientRef.current;
296
311
  if (!apiClient) {
312
+ initializingRef.current = false;
297
313
  throw new Error("API client not initialized");
298
314
  }
299
315
  const reference = config.reference || generateReference();
@@ -317,6 +333,7 @@ function useReevit(options) {
317
333
  };
318
334
  dispatch({ type: "INIT_ERROR", payload: noDataError });
319
335
  onError?.(noDataError);
336
+ initializingRef.current = false;
320
337
  return;
321
338
  }
322
339
  const paymentIntent = mapToPaymentIntent(data, { ...config, reference });
@@ -330,6 +347,7 @@ function useReevit(options) {
330
347
  };
331
348
  dispatch({ type: "INIT_ERROR", payload: error });
332
349
  onError?.(error);
350
+ initializingRef.current = false;
333
351
  }
334
352
  },
335
353
  [config, onError, apiBaseUrl]
@@ -394,6 +412,7 @@ function useReevit(options) {
394
412
  [onError]
395
413
  );
396
414
  const reset = useCallback(() => {
415
+ initializingRef.current = false;
397
416
  dispatch({ type: "RESET" });
398
417
  }, []);
399
418
  const close = useCallback(async () => {
@@ -758,10 +777,23 @@ function ReevitCheckout({
758
777
  // UI
759
778
  children,
760
779
  autoOpen = false,
780
+ isOpen: controlledIsOpen,
781
+ onOpenChange,
761
782
  theme,
762
783
  apiBaseUrl
763
784
  }) {
764
- const [isOpen, setIsOpen] = useState(autoOpen);
785
+ const [internalIsOpen, setInternalIsOpen] = useState(autoOpen);
786
+ const isOpen = controlledIsOpen !== void 0 ? controlledIsOpen : internalIsOpen;
787
+ const setIsOpen = useCallback(
788
+ (value) => {
789
+ if (onOpenChange) {
790
+ onOpenChange(value);
791
+ } else {
792
+ setInternalIsOpen(value);
793
+ }
794
+ },
795
+ [onOpenChange]
796
+ );
765
797
  const [showPSPBridge, setShowPSPBridge] = useState(false);
766
798
  const [momoData, setMomoData] = useState(null);
767
799
  const {
@@ -799,16 +831,17 @@ function ReevitCheckout({
799
831
  }
800
832
  }, [isOpen, status, initialize]);
801
833
  const handleOpen = useCallback(() => {
834
+ if (controlledIsOpen !== void 0) return;
802
835
  setIsOpen(true);
803
836
  setShowPSPBridge(false);
804
837
  setMomoData(null);
805
- }, []);
838
+ }, [controlledIsOpen, setIsOpen]);
806
839
  const handleClose = useCallback(() => {
807
840
  closeCheckout();
808
841
  setIsOpen(false);
809
842
  setShowPSPBridge(false);
810
843
  setMomoData(null);
811
- }, [closeCheckout]);
844
+ }, [closeCheckout, setIsOpen]);
812
845
  const handleMethodSelect = useCallback(
813
846
  (method) => {
814
847
  selectMethod(method);