Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

URLHandler.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // @flow
  2. import {Linking} from 'react-native';
  3. /**
  4. * Class use to handle depp links scanned or clicked.
  5. */
  6. export default class URLHandler {
  7. static SCHEME = "campus-insat://"; // Urls beginning with this string will be opened in the app
  8. static CLUB_INFO_URL_PATH = "club";
  9. static EVENT_INFO_URL_PATH = "event";
  10. static CLUB_INFO_ROUTE = "club-information";
  11. static EVENT_INFO_ROUTE = "planning-information";
  12. onInitialURLParsed: Function;
  13. onDetectURL: Function;
  14. constructor(onInitialURLParsed: Function, onDetectURL: Function) {
  15. this.onInitialURLParsed = onInitialURLParsed;
  16. this.onDetectURL = onDetectURL;
  17. }
  18. /**
  19. * Parses the given url to retrieve the corresponding app path and associated arguments.
  20. *
  21. * @param url The url to parse
  22. * @returns {{path: string, queryParams: {}}}
  23. */
  24. static parseUrl(url: string) {
  25. let params = {};
  26. let path = "";
  27. let temp = url.replace(URLHandler.SCHEME, "");
  28. if (temp != null) {
  29. let array = temp.split("?");
  30. if (array != null && array.length > 0) {
  31. path = array[0];
  32. }
  33. if (array != null && array.length > 1) {
  34. let tempParams = array[1].split("&");
  35. for (let i = 0; i < tempParams.length; i++) {
  36. let paramsArray = tempParams[i].split("=");
  37. if (paramsArray.length > 1) {
  38. params[paramsArray[0]] = paramsArray[1];
  39. }
  40. }
  41. }
  42. }
  43. return {path: path, queryParams: params};
  44. }
  45. /**
  46. * Gets routing data corresponding to the given url.
  47. * If the url does not match any existing route, null will be returned.
  48. *
  49. * @param path Url path
  50. * @param queryParams Url parameters
  51. * @returns {null}
  52. */
  53. static getUrlData({path, queryParams}: { path: string, queryParams: { [key: string]: string } }) {
  54. let data = null;
  55. if (path !== null) {
  56. if (URLHandler.isClubInformationLink(path))
  57. data = URLHandler.generateClubInformationData(queryParams);
  58. else if (URLHandler.isPlanningInformationLink(path))
  59. data = URLHandler.generatePlanningInformationData(queryParams);
  60. }
  61. return data;
  62. }
  63. /**
  64. * Checks if the given url is in a valid format
  65. *
  66. * @param url The url to check
  67. * @returns {boolean}
  68. */
  69. static isUrlValid(url: string) {
  70. return this.getUrlData(URLHandler.parseUrl(url)) !== null;
  71. }
  72. /**
  73. * Check if the given path links to the club information screen
  74. *
  75. * @param path The url to check
  76. * @returns {boolean}
  77. */
  78. static isClubInformationLink(path: string) {
  79. return path === URLHandler.CLUB_INFO_URL_PATH;
  80. }
  81. /**
  82. * Check if the given path links to the planning information screen
  83. *
  84. * @param path The url to check
  85. * @returns {boolean}
  86. */
  87. static isPlanningInformationLink(path: string) {
  88. return path === URLHandler.EVENT_INFO_URL_PATH;
  89. }
  90. /**
  91. * Generates data formatted for the club information screen from the url parameters.
  92. *
  93. * @param params Url parameters to convert
  94. * @returns {null|{route: string, data: {clubId: number}}}
  95. */
  96. static generateClubInformationData(params: Object): Object | null {
  97. if (params !== undefined && params.id !== undefined) {
  98. let id = parseInt(params.id);
  99. if (!isNaN(id)) {
  100. return {route: URLHandler.CLUB_INFO_ROUTE, data: {clubId: id}};
  101. }
  102. }
  103. return null;
  104. }
  105. /**
  106. * Generates data formatted for the planning information screen from the url parameters.
  107. *
  108. * @param params Url parameters to convert
  109. * @returns {null|{route: string, data: {clubId: number}}}
  110. */
  111. static generatePlanningInformationData(params: Object): Object | null {
  112. if (params !== undefined && params.id !== undefined) {
  113. let id = parseInt(params.id);
  114. if (!isNaN(id)) {
  115. return {route: URLHandler.EVENT_INFO_ROUTE, data: {eventId: id}};
  116. }
  117. }
  118. return null;
  119. }
  120. /**
  121. * Starts listening to events.
  122. *
  123. * There are 2 types of event.
  124. *
  125. * A classic event, triggered while the app is active.
  126. * An initial event, called when the app was opened by clicking on a link
  127. *
  128. */
  129. listen() {
  130. Linking.addEventListener('url', this.onUrl);
  131. Linking.getInitialURL().then(this.onInitialUrl);
  132. }
  133. /**
  134. * Gets data from the given url and calls the classic callback with it.
  135. *
  136. * @param url The url detected
  137. */
  138. onUrl = ({url}: { url: string }) => {
  139. if (url != null) {
  140. let data = URLHandler.getUrlData(URLHandler.parseUrl(url));
  141. if (data !== null)
  142. this.onDetectURL(data);
  143. }
  144. };
  145. /**
  146. * Gets data from the given url and calls the initial callback with it.
  147. *
  148. * @param url The url detected
  149. */
  150. onInitialUrl = (url: ?string) => {
  151. if (url != null) {
  152. let data = URLHandler.getUrlData(URLHandler.parseUrl(url));
  153. if (data !== null)
  154. this.onInitialURLParsed(data);
  155. }
  156. };
  157. }