-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(setting): request user confirmation on local data import
As a part of this implement handier popup tooling refs #56
- Loading branch information
1 parent
34b1cf4
commit bf7445f
Showing
7 changed files
with
127 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,62 @@ | ||
import { Popup, PopupProps, CommonProps, ConfirmProps } from "@src/components/shared/Popup"; | ||
import React, { FC, useCallback, useMemo, useState } from "react"; | ||
import { firstValueFrom, Subject } from "rxjs"; | ||
|
||
type ConfirmPopupProps = string | (Omit<CommonProps, "onClose" | "open"> & Omit<ConfirmProps, "onValidate" | "onCancel" | "variant">); | ||
|
||
type PopupProviderContext = { | ||
confirm: (messageOrProps: ConfirmPopupProps) => Promise<boolean>; | ||
}; | ||
|
||
const PopupContext = React.createContext<PopupProviderContext | undefined>(undefined); | ||
|
||
export const PopupProvider: FC = ({ children }) => { | ||
const [popupProps, setPopupProps] = useState<PopupProps | undefined>(); | ||
|
||
const confirm = useCallback( | ||
(messageOrProps: ConfirmPopupProps) => { | ||
let subject: Subject<boolean> | undefined = new Subject<boolean>(); | ||
|
||
const closeWithResult = (result: boolean) => () => { | ||
if (subject) { | ||
subject.next(result); | ||
subject.complete(); | ||
setPopupProps(undefined); | ||
subject = undefined; | ||
} | ||
}; | ||
const reject = closeWithResult(false); | ||
const props = typeof messageOrProps === "string" ? { message: messageOrProps } : messageOrProps; | ||
|
||
setPopupProps({ | ||
title: "Confirm", | ||
...props, | ||
open: true, | ||
variant: "confirm", | ||
onValidate: closeWithResult(true), | ||
onCancel: reject, | ||
onClose: reject | ||
}); | ||
|
||
return firstValueFrom(subject); | ||
}, | ||
[setPopupProps] | ||
); | ||
|
||
const context = useMemo(() => ({ confirm }), [confirm]); | ||
|
||
return ( | ||
<PopupContext.Provider value={context}> | ||
{children} | ||
{popupProps && <Popup {...popupProps} />} | ||
</PopupContext.Provider> | ||
); | ||
}; | ||
|
||
export const usePopup = () => { | ||
const context = React.useContext(PopupContext); | ||
if (!context) { | ||
throw new Error("usePopup must be used within a PopupProvider"); | ||
} | ||
return context; | ||
}; |
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