-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
138 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Route } from '@lifi/sdk'; | ||
import type { RouteExecutionUpdate } from '@lifi/widget'; | ||
import { useWidgetEvents, WidgetEvent } from '@lifi/widget'; | ||
import { useEffect } from 'react'; | ||
|
||
export const WidgetEvents = () => { | ||
// const location = useLocation(); | ||
// const [urlSearchParams, setURLSearchParams] = useState<URLSearchParams>(); | ||
const widgetEvents = useWidgetEvents(); | ||
|
||
useEffect(() => { | ||
const onRouteExecutionStarted = (route: Route) => { | ||
// console.log('onRouteExecutionStarted fired.'); | ||
}; | ||
const onRouteExecutionUpdated = (update: RouteExecutionUpdate) => { | ||
// console.log('onRouteExecutionUpdated fired.'); | ||
}; | ||
const onRouteExecutionCompleted = (route: Route) => { | ||
// console.log('onRouteExecutionCompleted fired.'); | ||
}; | ||
const onRouteExecutionFailed = (update: RouteExecutionUpdate) => { | ||
// console.log('onRouteExecutionFailed fired.'); | ||
}; | ||
// const onInputValuesUpdated = (values: InputValuesUpdated) => { | ||
// console.log('InputValuesUpdated', values); | ||
|
||
// const urlSearchParams = new URLSearchParams(); | ||
// for (const key in values) { | ||
// if (Object.prototype.hasOwnProperty.call(values, key)) { | ||
// urlSearchParams.set(key, (values as any)[key] || ''); | ||
// } | ||
// } | ||
// setURLSearchParams(urlSearchParams); | ||
// }; | ||
widgetEvents.on(WidgetEvent.RouteExecutionStarted, onRouteExecutionStarted); | ||
widgetEvents.on(WidgetEvent.RouteExecutionUpdated, onRouteExecutionUpdated); | ||
widgetEvents.on( | ||
WidgetEvent.RouteExecutionCompleted, | ||
onRouteExecutionCompleted, | ||
); | ||
widgetEvents.on(WidgetEvent.RouteExecutionFailed, onRouteExecutionFailed); | ||
// widgetEvents.on(WidgetEvent.InputValuesUpdated, onInputValuesUpdated); | ||
return () => widgetEvents.all.clear(); | ||
}, [widgetEvents]); | ||
|
||
// useLayoutEffect(() => { | ||
// const url = new URL(window.location as any); | ||
// urlSearchParams?.forEach((value, key) => { | ||
// if (value) { | ||
// url.searchParams.set(key, value); | ||
// } else { | ||
// url.searchParams.delete(key); | ||
// } | ||
// }); | ||
// window.history.replaceState({}, '', url); | ||
// }, [urlSearchParams, location]); | ||
|
||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/widget/src/providers/SwapFormProvider/URLSearchParamsBuilder.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { useFormState, useWatch } from 'react-hook-form'; | ||
import { useLocation } from 'react-router-dom'; | ||
import { SwapFormKey } from './types'; | ||
|
||
const formValueKeys = [ | ||
SwapFormKey.FromAmount, | ||
SwapFormKey.FromChain, | ||
SwapFormKey.FromToken, | ||
SwapFormKey.ToAddress, | ||
SwapFormKey.ToChain, | ||
SwapFormKey.ToToken, | ||
]; | ||
|
||
const replcateUrlState = (urlSearchParams?: URLSearchParams) => { | ||
if (!urlSearchParams) { | ||
return; | ||
} | ||
const url = new URL(window.location as any); | ||
urlSearchParams.forEach((value, key) => { | ||
if (value) { | ||
url.searchParams.set(key, value); | ||
} else { | ||
url.searchParams.delete(key); | ||
} | ||
}); | ||
window.history.replaceState(null, '', url); | ||
}; | ||
|
||
export const URLSearchParamsBuilder = () => { | ||
const { pathname } = useLocation(); | ||
const urlSearchParamsRef = useRef<URLSearchParams>(); | ||
const { dirtyFields } = useFormState(); | ||
const values = useWatch({ name: formValueKeys }); | ||
|
||
useEffect(() => { | ||
const urlSearchParams = new URLSearchParams(); | ||
formValueKeys.forEach((key, index) => { | ||
if (dirtyFields[key]) { | ||
urlSearchParams.set(key, values[index] || ''); | ||
} | ||
}); | ||
replcateUrlState(urlSearchParams); | ||
urlSearchParamsRef.current = urlSearchParams; | ||
}, [dirtyFields, values]); | ||
|
||
useEffect(() => { | ||
replcateUrlState(urlSearchParamsRef.current); | ||
}, [pathname]); | ||
|
||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters