import { createSlice , PayloadAction } from '@reduxjs/toolkit' ; import { DialogNames } from 'components/Dialog' ; import { AppThunk } from 'store/store' ; import { Items } from './story' ; type View = { name : 'metadata' ; } | { name : 'item' ; args : { item : Items ; } ; } | null ; export interface NavigationState { view : View ; dialog : { name : DialogNames ; props ? : any ; } | null ; dontAskConfirmationForMonetisation : boolean ; } const initialState = { view : null , dialog : null , dontAskConfirmationForMonetisation : false , } ; const dialogPromises = { } ; export const story = createSlice ( { name : 'navigation' , initialState , reducers : { goToView : ( state , action ) => { state . view = action . payload ; } , openDialog : ( state , action ) => { state . dialog = action . payload ; } , closeDialog : { reducer : ( state , action ) => { if ( state . dialog ?. name ) { let promise = dialogPromises [ state . dialog . name ] ; if ( promise ) { promise . resolve ( action . payload ) ; } } state . dialog = { ... initialState } . dialog ; } , prepare : ( returnValue ? ) => { return { payload : returnValue , } ; } , } , setDontAskConfirmationForMonetisation : ( state , action ) => { state . dontAskConfirmationForMonetisation = action . payload ; } , reset : ( ) => { return initialState ; } , } , } ) ; export const openDialogAndWait = ( dialog ) : AppThunk < Promise < any > > => ( dispatch ) => { dispatch ( openDialog ( dialog ) ) ; return new Promise ( ( resolve , reject ) => { if ( dialog ?. name ) { dialogPromises [ dialog . name ] = { resolve , reject } ; } } ) ; } ; export const { goToView , openDialog , closeDialog , setDontAskConfirmationForMonetisation , reset , } = story . actions ; export default story . reducer ;