-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ConnectionForm now clears its own form tracking
Add hook to generate unique form id Update form tracker component to use new hook if no id is specified
- Loading branch information
Showing
5 changed files
with
60 additions
and
46 deletions.
There are no files selected for viewing
12 changes: 5 additions & 7 deletions
12
airbyte-webapp/src/components/FormChangeTracker/FormChangeTracker.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
35 changes: 1 addition & 34 deletions
35
airbyte-webapp/src/hooks/services/FormChangeTracker/FormChangeTrackerService.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
46 changes: 46 additions & 0 deletions
46
airbyte-webapp/src/hooks/services/FormChangeTracker/hooks.ts
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,46 @@ | ||
import { useCallback, useMemo } from "react"; | ||
import { createGlobalState } from "react-use"; | ||
import { useLocation } from "react-router-dom"; | ||
import { uniqueId } from "lodash"; | ||
|
||
import { FormChangeTrackerServiceApi } from "./types"; | ||
|
||
export const useChangedFormsById = createGlobalState<Record<string, boolean>>({}); | ||
|
||
export const useUniqueFormId = (formId?: string) => { | ||
const location = useLocation(); | ||
return useMemo( | ||
() => formId ?? `${location.pathname.toLowerCase().replace(/\//gi, "_")}__${uniqueId("form_")}`, | ||
[formId, location.pathname] | ||
); | ||
}; | ||
|
||
export const useFormChangeTrackerService = (): FormChangeTrackerServiceApi => { | ||
const [changedFormsById, setChangedFormsById] = useChangedFormsById(); | ||
|
||
const clearAllFormChanges = useCallback(() => { | ||
setChangedFormsById({}); | ||
}, [setChangedFormsById]); | ||
|
||
const clearFormChange = useCallback( | ||
(id: string) => { | ||
setChangedFormsById({ ...changedFormsById, [id]: false }); | ||
}, | ||
[changedFormsById, setChangedFormsById] | ||
); | ||
|
||
const trackFormChange = useCallback( | ||
(id: string, changed: boolean) => { | ||
if (!!changedFormsById?.[id] !== changed) { | ||
setChangedFormsById({ ...changedFormsById, [id]: changed }); | ||
} | ||
}, | ||
[changedFormsById, setChangedFormsById] | ||
); | ||
|
||
return { | ||
trackFormChange, | ||
clearFormChange, | ||
clearAllFormChanges, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./FormChangeTrackerService"; | ||
export * from "./hooks"; |
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