Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Redux-observable dispatching multiple actions

$
0
0

I'm learning to use redux-observable and RxJS in react-native then I face a problem with dispatching multiple actions after performing a Firestore get document request.

This is my epic:

const getUserEpic: Epic = action$ => {  return action$.pipe(    filter(signIn.match),    switchMap(action => {      let usecase = container.resolve<SignInUseCase>('SignInUseCase');      return concat(        of(signInBegin()),        usecase.call(action.payload).pipe(          map(res => {            if (res !== undefined) return saveUser(res);            return signInFailed();          }),        ),      );    }),  );};

Basically, I'm trying to dispatch many actions after an action named "signIn" is dispatched. The "usecase" is performing a Firestore get document. After the responseis received, I want to dispatch two actions, "saveUser" and "signInSuccess". However, if the response is undefined, action "signInFailed" must be dispatched instead of those two above.

The code should be something like this:

...        usecase.call(action.payload).pipe(          map(res => {            if (res !== undefined)                return {                   saveUser(res);                   signInSuccess();               }            return signInFailed();          }),        ),

Please help me correct the code! Thank you for spending your time!

Btw, this is the SignInUseCase:

@injectable()export class SignInUseCase implements UseCase<SignInResult, any> {  constructor(    @inject('AuthenticationRepository')    private readonly authenticationRepository: AuthenticationRepository,  ) {}  call(param?: any): Observable<SignInResult> {    return this.authenticationRepository.signIn(param);  }}

The implementation of AuthenticationRepository:

export class AuthenticationRepositoryImpl implements AuthenticationRepository {  signIn(credential: any): Observable<SignInResult> {    return from(getUser(credential.phone_number));  }}

And this is the function "getUser" which get doc from Firestore:

export const getUser = async (phoneNumber: string) => {  const doc = await firestore()    .collection('users')    .doc(phoneNumber)    .get()    .catch(e => {      Alert.alert('Error: ', e);    });  return doc.data();};

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>