import { assertExists } from '@ulangi/assert' ; import { SQLiteDatabase , Transaction } from '@ulangi/sqlite-adapter' ; import { Action , ActionType , createAction } from '@ulangi/ulangi-action' ; import { SetModel } from '@ulangi/ulangi-local-database' ; import { ForkEffect , call , fork , put , take , takeEvery , } from 'redux-saga/effects' ; import { PromiseType } from 'utility-types' ; import { errorConverter } from '../converters/ErrorConverter' ; import { ProtectedSaga } from './ProtectedSaga' ; export class SetSaga extends ProtectedSaga { private userDb : SQLiteDatabase ; private setModel : SetModel ; public constructor ( userDb , setModel ) { super ( ) ; this . userDb = userDb ; this . setModel = setModel ; } public * run ( ) { yield fork ( [ this , this . allowAdd ] ) ; yield fork ( [ this , this . allowEdit ] ) ; yield fork ( [ this , this . allowFetchAll ] ) ; yield fork ( [ this , this . allowFetch ] ) ; } public * allowAdd ( ) { while ( true ) { const action = yield take ( ActionType . SET__ADD ) ; const { set } = action . payload ; try { yield put ( createAction ( ActionType . SET__ADDING , { set } ) ) ; yield call ( [ this . userDb , 'transaction' ] , ( tx ) : void => { this . setModel . insertSet ( tx , set , 'local' ) ; } ) ; yield put ( createAction ( ActionType . SET__ADD_SUCCEEDED , { set } ) ) ; } catch ( error ) { yield put ( createAction ( ActionType . SET__ADD_FAILED , { set , errorCode : errorConverter . getErrorCode ( error ) , error , } ) ) ; } } } public * allowEdit ( ) { while ( true ) { const action = yield take ( ActionType . SET__EDIT ) ; const { set } = action . payload ; try { yield put ( createAction ( ActionType . SET__EDITING , { set } ) ) ; const setId = assertExists ( set . setId , 'setId should not be null or undefined' ) ; yield call ( [ this . userDb , 'transaction' ] , ( tx ) : void => { this . setModel . updateSet ( tx , set , 'local' ) ; } ) ; const result = yield call ( [ this . setModel , 'getSetById' ] , this . userDb , setId , false ) ; const { set : updatedSet } = assertExists ( result ) ; yield put ( createAction ( ActionType . SET__EDIT_SUCCEEDED , { set : updatedSet } ) ) ; } catch ( error ) { yield put ( createAction ( ActionType . SET__EDIT_FAILED , { set , errorCode : errorConverter . getErrorCode ( error ) , error , } ) ) ; } } } public * allowFetchAll ( ) { while ( true ) { yield take ( ActionType . SET__FETCH_ALL ) ; try { yield put ( createAction ( ActionType . SET__FETCHING_ALL , null ) ) ; const result = yield call ( [ this . setModel , 'getAllSets' ] , this . userDb , true ) ; const { setList } = result ; yield put ( createAction ( ActionType . SET__FETCH_ALL_SUCCEEDED , { setList } ) ) ; } catch ( error ) { yield put ( createAction ( ActionType . SET__FETCH_ALL_FAILED , { errorCode : errorConverter . getErrorCode ( error ) , error , } ) ) ; } } } public * allowFetch ( ) { yield takeEvery ( ActionType . SET__FETCH , ( action ) : IterableIterator < ForkEffect > => this . fetch ( action ) ) ; } private * fetch ( action ) { const { setStatus } = action . payload ; try { yield put ( createAction ( ActionType . SET__FETCHING , { setStatus } ) ) ; const result = yield call ( [ this . setModel , 'getSetsByStatus' ] , this . userDb , setStatus , true ) ; const { setList } = result ; yield put ( createAction ( ActionType . SET__FETCH_SUCCEEDED , { setList , setStatus , } ) ) ; } catch ( error ) { yield put ( createAction ( ActionType . SET__FETCH_FAILED , { setStatus , errorCode : errorConverter . getErrorCode ( error ) , error , } ) ) ; } } }