@reevit/react 0.2.8 → 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,9 +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);
282
- const initializingRef = useRef(false);
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]);
283
293
  if (!apiClientRef.current) {
284
294
  apiClientRef.current = new ReevitAPIClient({
285
295
  publicKey: config.publicKey,
@@ -767,10 +777,23 @@ function ReevitCheckout({
767
777
  // UI
768
778
  children,
769
779
  autoOpen = false,
780
+ isOpen: controlledIsOpen,
781
+ onOpenChange,
770
782
  theme,
771
783
  apiBaseUrl
772
784
  }) {
773
- 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
+ );
774
797
  const [showPSPBridge, setShowPSPBridge] = useState(false);
775
798
  const [momoData, setMomoData] = useState(null);
776
799
  const {
@@ -808,16 +831,17 @@ function ReevitCheckout({
808
831
  }
809
832
  }, [isOpen, status, initialize]);
810
833
  const handleOpen = useCallback(() => {
834
+ if (controlledIsOpen !== void 0) return;
811
835
  setIsOpen(true);
812
836
  setShowPSPBridge(false);
813
837
  setMomoData(null);
814
- }, []);
838
+ }, [controlledIsOpen, setIsOpen]);
815
839
  const handleClose = useCallback(() => {
816
840
  closeCheckout();
817
841
  setIsOpen(false);
818
842
  setShowPSPBridge(false);
819
843
  setMomoData(null);
820
- }, [closeCheckout]);
844
+ }, [closeCheckout, setIsOpen]);
821
845
  const handleMethodSelect = useCallback(
822
846
  (method) => {
823
847
  selectMethod(method);