Guest User

Untitled

a guest
May 28th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.27 KB | None | 0 0
  1. //@flow
  2.  
  3. import { type Saga } from 'redux-saga';
  4. import type { Reducer } from 'redux';
  5. import { put, select, call, fork } from 'redux-saga/effects';
  6. import { GoogleSignin } from 'react-native-google-signin';
  7. import Config from 'react-native-config';
  8. import idx from 'idx';
  9.  
  10. import ScreensBootstrap from 'Navigation';
  11. import { resetError, setError } from 'ActionsReducers/ErrorReducerActions';
  12. import { loginSuccess, loginFailure, googleLoginSuccess, googleLoginFailure, getUserSuccess, getUserFailure, getOfflineContentSuccess, getOfflineContentFailure, logoutSuccess, logoutFailure } from 'ActionsReducers/SessionReducerActions';
  13. import { login as loginApi, googleAuth, getUser as getUserApi, logout } from 'Api/Session';
  14. import { downloadFile } from 'Api/Download';
  15. import { type ActionSaga } from 'ActionsReducersType';
  16. import { GENERIC_ERROR } from 'Helpers/Constant';
  17. import { updateBootstrap } from 'ActionsReducers/AppConfigReducerActions';
  18. import { hash } from 'Helpers/String';
  19.  
  20. import DB from 'Store/DB';
  21. import Reactotron from 'reactotron-react-native';
  22.  
  23. const getAppConfigReducer = (state: Reducer) => state.appConfigReducer;
  24. const getSessionReducer = (state: Reducer) => state.sessionReducer;
  25.  
  26. export function* getUserFromDB(action: ActionSaga): Saga<void> {
  27. const appConfigReducer = yield select(getAppConfigReducer);
  28. const { originalDomain } = appConfigReducer;
  29.  
  30. const { username, password } = action.payload;
  31.  
  32. const user = DB.getUserByCredentials({
  33. username,
  34. password,
  35. originalDomain
  36. });
  37.  
  38. if (user) {
  39. yield put(resetError());
  40. yield put(loginSuccess(null, user));
  41.  
  42. //SET ROOT SCREEN
  43. ScreensBootstrap.myDownload();
  44. } else {
  45. yield put(
  46. setError({
  47. code: GENERIC_ERROR,
  48. message: 'boh',
  49. actionSucc: action,
  50. actionFail: loginFailure()
  51. })
  52. );
  53. }
  54. }
  55.  
  56. export function* loginSagas(action: ActionSaga): Saga<void> {
  57. try {
  58. const appConfigReducer = yield select(getAppConfigReducer);
  59. const { aliasDomain, originalDomain, bootstrap, offlineMode } = appConfigReducer;
  60.  
  61. const { username, password } = action.payload;
  62.  
  63. if (offlineMode) {
  64. yield fork(getUserFromDB, action);
  65. } else {
  66. const login = yield call(loginApi, aliasDomain, username, password);
  67. const user = yield call(getUserApi, aliasDomain, login.accessToken, login.ID);
  68.  
  69. const logoRemote = idx(bootstrap, (_) => _.brandingElements.signin.logo.remote);
  70. const logoLocale = yield call(downloadFile, originalDomain, logoRemote);
  71. bootstrap.brandingElements.signin.logo.locale = logoLocale;
  72.  
  73. yield put(updateBootstrap(bootstrap));
  74.  
  75. const userAvatarRemote = idx(user, (_) => _.avatar.remote);
  76. const userAvatarLocale = yield call(downloadFile, originalDomain, userAvatarRemote);
  77. user.avatar.locale = userAvatarLocale;
  78. user.password = hash(password);
  79. user.originalDomain = originalDomain;
  80.  
  81. yield call(DB.writeAlias, { aliasDomain, originalDomain });
  82. yield call(DB.writeLms, { originalDomain, bootstrap });
  83. yield call(DB.writeUser, user);
  84.  
  85. yield put(resetError());
  86. yield put(loginSuccess(login, user));
  87.  
  88. ScreensBootstrap.main();
  89. }
  90. } catch (error) {
  91. Reactotron.log('error: ' + JSON.stringify(error));
  92.  
  93. const status = idx(error, (_) => _.response.status) || GENERIC_ERROR;
  94. const message = idx(error, (_) => _.response.data.message[0]);
  95.  
  96. yield put(
  97. setError({
  98. code: status,
  99. message: message,
  100. actionSucc: action,
  101. actionFail: loginFailure()
  102. })
  103. );
  104. }
  105. }
  106.  
  107. const googleSignin = (): Promise<string> => {
  108. return GoogleSignin.configure({
  109. iosClientId: Config.GOOGLE_IOS_CLIENT_ID,
  110. webClientId: Config.GOOGLE_WEB_CLIENT_ID
  111. }).then(() => GoogleSignin.signIn().then((response: Object) => response.idToken));
  112. };
  113.  
  114. const googleSignout = (): Promise<string> => {
  115. return GoogleSignin.signOut();
  116. };
  117.  
  118. export function* googleLoginSagas(action: ActionSaga): Saga<void> {
  119. try {
  120. const appConfigReducer = yield select(getAppConfigReducer);
  121. const { aliasDomain, originalDomain, bootstrap } = appConfigReducer;
  122.  
  123. const googleToken = yield call(googleSignin);
  124. yield call(googleSignout);
  125.  
  126. const login = yield call(googleAuth, aliasDomain, googleToken);
  127. const user = yield call(getUserApi, aliasDomain, login.accessToken, login.ID);
  128.  
  129. const logoRemote = idx(bootstrap, (_) => _.brandingElements.signin.logo);
  130. const logoLocale = yield call(downloadFile, originalDomain, logoRemote);
  131. bootstrap.brandingElements.signin.logo = logoLocale;
  132.  
  133. yield put(updateBootstrap(bootstrap));
  134.  
  135. const userAvatarRemote = idx(user, (_) => _.avatar.remote);
  136. const userAvatarLocale = yield call(downloadFile, originalDomain, userAvatarRemote);
  137. user.avatar.locale = userAvatarLocale;
  138. user.originalDomain = originalDomain;
  139.  
  140. yield call(DB.writeAlias, { aliasDomain, originalDomain });
  141. yield call(DB.writeLms, { originalDomain, bootstrap });
  142. yield call(DB.writeUser, user);
  143.  
  144. yield put(resetError());
  145. yield put(googleLoginSuccess(login, user));
  146.  
  147. ScreensBootstrap.main();
  148. } catch (error) {
  149. const status = idx(error, (_) => _.response.status) || GENERIC_ERROR;
  150. const message = idx(error, (_) => _.response.data.message[0]);
  151.  
  152. yield put(
  153. setError({
  154. code: status,
  155. message: message,
  156. actionSucc: action,
  157. actionFail: googleLoginFailure()
  158. })
  159. );
  160. }
  161. }
  162.  
  163. export function* getUserSagas(action: ActionSaga): Saga<void> {
  164. try {
  165. const appConfigReducer = yield select(getAppConfigReducer);
  166. const { aliasDomain } = appConfigReducer;
  167.  
  168. const sessionReducer = yield select(getSessionReducer);
  169. const { accessToken } = sessionReducer.login;
  170.  
  171. const { userId } = action.payload;
  172.  
  173. const response = yield call(getUserApi, aliasDomain, accessToken, userId);
  174.  
  175. yield put(resetError());
  176. yield put(getUserSuccess(response));
  177. } catch (error) {
  178. const status = idx(error, (_) => _.response.status) || GENERIC_ERROR;
  179. const message = idx(error, (_) => _.response.data.message[0]);
  180.  
  181. yield put(
  182. setError({
  183. code: status,
  184. message: message,
  185. actionSucc: action,
  186. actionFail: getUserFailure()
  187. })
  188. );
  189. }
  190. }
  191.  
  192. export function* refreshTokenSagas(): Saga<void> {
  193. // const userReducer = yield select(getAppConfigReducer);
  194. // const loginFromRedux = appConfig.login;
  195. // try {
  196. // const { accessToken, expiresIn, refreshToken } = yield call(postRefreshTokenApi, loginFromRedux.refreshToken);
  197. // yield put(refreshTokenSuccess(accessToken, expiresIn, refreshToken));
  198. // } catch (error) {
  199. // yield put(refreshTokenFailure());
  200. // }
  201. }
  202.  
  203. export function* getOfflineContentSagas(action: ActionSaga): Saga<void> {
  204. try {
  205. const appConfigReducer = yield select(getAppConfigReducer);
  206. const { originalDomain } = appConfigReducer;
  207.  
  208. const sessionReducer = yield select(getSessionReducer);
  209. const { ID: userId } = sessionReducer.login;
  210. var coursesDownloadedByUser = [];
  211.  
  212. /*
  213. if (originalDomain && userId) {
  214. const coursesDownloaded = DB.getCoursesByOriginalDomain(originalDomain) || [];
  215. coursesDownloadedByUser = coursesDownloaded.filter((item) => DB.getCourseMetadataByKey(originalDomain, userId, item.courseId)).map((courseDownloadedByUser) => {
  216. const statusCourse = DB.getCourseStatusByKey(originalDomain, courseDownloadedByUser.courseId, userId);
  217. return { ...courseDownloadedByUser, status: statusCourse, locked };
  218. });
  219. }
  220. */
  221.  
  222. if (originalDomain && userId) {
  223. const coursesDownloaded = DB.getCoursesByOriginalDomain(originalDomain);
  224. if (coursesDownloaded) {
  225. coursesDownloadedByUser = coursesDownloaded.reduce((courseDownloadedByUser, currentValue) => {
  226. const courseId = courseDownloadedByUser.courseId;
  227. const courseMetadata = DB.getCourseMetadataByKey(originalDomain, userId, courseId);
  228. if (courseMetadata) {
  229. currentValue.concat({ ...courseDownloadedByUser, status: courseMetadata.status, locked: courseMetadata.locked });
  230. }
  231. }, []);
  232. }
  233. }
  234.  
  235. yield put(resetError());
  236. yield put(getOfflineContentSuccess(coursesDownloadedByUser));
  237. } catch (error) {
  238. const status = idx(error, (_) => _.response.status) || GENERIC_ERROR;
  239. const message = idx(error, (_) => _.response.data.message[0]);
  240.  
  241. yield put(
  242. setError({
  243. code: status,
  244. message: message,
  245. actionSucc: action,
  246. actionFail: getOfflineContentFailure()
  247. })
  248. );
  249. }
  250. }
  251.  
  252. export function* logoutSagas(action: ActionSaga): Saga<void> {
  253. try {
  254. const appConfigReducer = yield select(getAppConfigReducer);
  255. const { aliasDomain, offlineMode } = appConfigReducer;
  256.  
  257. Reactotron.log('offline: ' + JSON.stringify(offlineMode));
  258.  
  259. if (!offlineMode) {
  260. const sessionReducer = yield select(getSessionReducer);
  261. const { accessToken } = sessionReducer.login;
  262.  
  263. yield call(logout, aliasDomain, accessToken);
  264. }
  265.  
  266. yield put(resetError());
  267. yield put(logoutSuccess());
  268.  
  269. ScreensBootstrap.login();
  270. } catch (error) {
  271. const status = idx(error, (_) => _.response.status) || GENERIC_ERROR;
  272. const message = idx(error, (_) => _.response.data.message[0]);
  273.  
  274. yield put(
  275. setError({
  276. code: status,
  277. message: message,
  278. actionSucc: action,
  279. actionFail: logoutFailure()
  280. })
  281. );
  282. }
  283. }
Add Comment
Please, Sign In to add comment