Skip to content

Instantly share code, notes, and snippets.

@madandrija
Last active May 26, 2019 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madandrija/5fa9eb7f8ab981b79b19977d29651795 to your computer and use it in GitHub Desktop.
Save madandrija/5fa9eb7f8ab981b79b19977d29651795 to your computer and use it in GitHub Desktop.
Wix navigation and Dropdown helper
// Register screens
function registerComponentWithAlert(Comp: any, id: string) {
return (props: any) => (
<Provider store={store}>
<Comp {...props} />
<DropdownAlert ref={ref => DropDownHolder.setDropDown(ref, id)} closeInterval={6000} />
</Provider>
);
// handle setting the reference to the Dropdown Alert for the current screen
export class DropdownAlertHelper {
public static dropdownAlert: {
[id: string]: DropdownAlert | null;
} = {};
public static setDropDown(dropdownAlert: DropdownAlert | null, screenName: string) {
this.dropdownAlert[screenName] = dropdownAlert;
}
public static setAlert(alert: DropdownAlertHelperType, screenName: string) {
if (!this.dropdownAlert[screenName]) {
throw new Error(`Error container for ${screenName} not found`);
}
this.dropdownAlert[screenName]!.alertWithType(alert.type, alert.title, alert.message);
}
}
// Saga that binds this together
function actionIsSetImmediateAlert(action: ReduxAction): boolean {
return ActionHasPayload(action) && action.type === SET_IMMEDIATE_ALERT;
}
function newScreenIsShown(action: ReduxAction): boolean {
return ActionHasPayload(action) && action.type === SET_CURRENT_SCREEN;
}
export function* watchDropdownAlertEvents() {
yield fork(processPendingDropdownAlerts);
yield fork(processCurrentDropdownAlert);
}
function* processCurrentDropdownAlert() {
const requestChan = yield actionChannel(actionIsSetImmediateAlert);
while (true) {
const action: IFSAWithPayload<DropdownAlertHelperType> = yield take(requestChan);
const currentScreenName = yield select(getCurrentScreenName);
DropdownAlertHelper.setAlert(action.payload, currentScreenName);
}
}
function* processPendingDropdownAlerts() {
const requestChan = yield actionChannel(newScreenIsShown);
while (true) {
const action: IFSAWithPayload<string> = yield take(requestChan);
const currentScreenName = action.payload;
const pendingAlerts: DropdownAlertHelperType[] = yield select(getPendingAlerts);
if (!!pendingAlerts && pendingAlerts.length > 0) {
const alert = pendingAlerts[0];
DropdownAlertHelper.setAlert(alert, currentScreenName);
yield put(removePendingAlert(alert));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment