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.

ServicesManager.ts 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import i18n from 'i18n-js';
  20. import {StackNavigationProp} from '@react-navigation/stack';
  21. import AvailableWebsites from '../constants/AvailableWebsites';
  22. import ConnectionManager from './ConnectionManager';
  23. import type {FullDashboardType} from '../screens/Home/HomeScreen';
  24. import getStrippedServicesList from '../utils/Services';
  25. // AMICALE
  26. const CLUBS_IMAGE =
  27. 'https://etud.insa-toulouse.fr/~amicale_app/images/Clubs.png';
  28. const PROFILE_IMAGE =
  29. 'https://etud.insa-toulouse.fr/~amicale_app/images/ProfilAmicaliste.png';
  30. const EQUIPMENT_IMAGE =
  31. 'https://etud.insa-toulouse.fr/~amicale_app/images/Materiel.png';
  32. const VOTE_IMAGE = 'https://etud.insa-toulouse.fr/~amicale_app/images/Vote.png';
  33. const AMICALE_IMAGE =
  34. 'https://etud.insa-toulouse.fr/~amicale_app/images/WebsiteAmicale.png';
  35. // STUDENTS
  36. const PROXIMO_IMAGE =
  37. 'https://etud.insa-toulouse.fr/~amicale_app/images/Proximo.png';
  38. const WIKETUD_IMAGE =
  39. 'https://etud.insa-toulouse.fr/~amicale_app/images/Wiketud.png';
  40. const EE_IMAGE = 'https://etud.insa-toulouse.fr/~amicale_app/images/EEC.png';
  41. const TUTORINSA_IMAGE =
  42. 'https://etud.insa-toulouse.fr/~amicale_app/images/TutorINSA.png';
  43. // INSA
  44. const BIB_IMAGE = 'https://etud.insa-toulouse.fr/~amicale_app/images/Bib.png';
  45. const RU_IMAGE = 'https://etud.insa-toulouse.fr/~amicale_app/images/RU.png';
  46. const ROOM_IMAGE =
  47. 'https://etud.insa-toulouse.fr/~amicale_app/images/Salles.png';
  48. const EMAIL_IMAGE =
  49. 'https://etud.insa-toulouse.fr/~amicale_app/images/Bluemind.png';
  50. const ENT_IMAGE = 'https://etud.insa-toulouse.fr/~amicale_app/images/ENT.png';
  51. const ACCOUNT_IMAGE =
  52. 'https://etud.insa-toulouse.fr/~amicale_app/images/Account.png';
  53. // SPECIAL
  54. const WASHER_IMAGE =
  55. 'https://etud.insa-toulouse.fr/~amicale_app/images/ProxiwashLaveLinge.png';
  56. const DRYER_IMAGE =
  57. 'https://etud.insa-toulouse.fr/~amicale_app/images/ProxiwashSecheLinge.png';
  58. const AMICALE_LOGO = require('../../assets/amicale.png');
  59. export const SERVICES_KEY = {
  60. CLUBS: 'clubs',
  61. PROFILE: 'profile',
  62. EQUIPMENT: 'equipment',
  63. AMICALE_WEBSITE: 'amicale_website',
  64. VOTE: 'vote',
  65. PROXIMO: 'proximo',
  66. WIKETUD: 'wiketud',
  67. ELUS_ETUDIANTS: 'elus_etudiants',
  68. TUTOR_INSA: 'tutor_insa',
  69. RU: 'ru',
  70. AVAILABLE_ROOMS: 'available_rooms',
  71. BIB: 'bib',
  72. EMAIL: 'email',
  73. ENT: 'ent',
  74. INSA_ACCOUNT: 'insa_account',
  75. WASHERS: 'washers',
  76. DRYERS: 'dryers',
  77. };
  78. export const SERVICES_CATEGORIES_KEY = {
  79. AMICALE: 'amicale',
  80. STUDENTS: 'students',
  81. INSA: 'insa',
  82. SPECIAL: 'special',
  83. };
  84. export type ServiceItemType = {
  85. key: string;
  86. title: string;
  87. subtitle: string;
  88. image: string;
  89. onPress: () => void;
  90. badgeFunction?: (dashboard: FullDashboardType) => number;
  91. };
  92. export type ServiceCategoryType = {
  93. key: string;
  94. title: string;
  95. subtitle: string;
  96. image: string | number;
  97. content: Array<ServiceItemType>;
  98. };
  99. export default class ServicesManager {
  100. navigation: StackNavigationProp<any>;
  101. amicaleDataset: Array<ServiceItemType>;
  102. studentsDataset: Array<ServiceItemType>;
  103. insaDataset: Array<ServiceItemType>;
  104. specialDataset: Array<ServiceItemType>;
  105. categoriesDataset: Array<ServiceCategoryType>;
  106. constructor(nav: StackNavigationProp<any>) {
  107. this.navigation = nav;
  108. this.amicaleDataset = [
  109. {
  110. key: SERVICES_KEY.CLUBS,
  111. title: i18n.t('screens.clubs.title'),
  112. subtitle: i18n.t('screens.services.descriptions.clubs'),
  113. image: CLUBS_IMAGE,
  114. onPress: (): void => this.onAmicaleServicePress('club-list'),
  115. },
  116. {
  117. key: SERVICES_KEY.PROFILE,
  118. title: i18n.t('screens.profile.title'),
  119. subtitle: i18n.t('screens.services.descriptions.profile'),
  120. image: PROFILE_IMAGE,
  121. onPress: (): void => this.onAmicaleServicePress('profile'),
  122. },
  123. {
  124. key: SERVICES_KEY.EQUIPMENT,
  125. title: i18n.t('screens.equipment.title'),
  126. subtitle: i18n.t('screens.services.descriptions.equipment'),
  127. image: EQUIPMENT_IMAGE,
  128. onPress: (): void => this.onAmicaleServicePress('equipment-list'),
  129. },
  130. {
  131. key: SERVICES_KEY.AMICALE_WEBSITE,
  132. title: i18n.t('screens.websites.amicale'),
  133. subtitle: i18n.t('screens.services.descriptions.amicaleWebsite'),
  134. image: AMICALE_IMAGE,
  135. onPress: (): void =>
  136. nav.navigate('website', {
  137. host: AvailableWebsites.websites.AMICALE,
  138. title: i18n.t('screens.websites.amicale'),
  139. }),
  140. },
  141. {
  142. key: SERVICES_KEY.VOTE,
  143. title: i18n.t('screens.vote.title'),
  144. subtitle: i18n.t('screens.services.descriptions.vote'),
  145. image: VOTE_IMAGE,
  146. onPress: (): void => this.onAmicaleServicePress('vote'),
  147. },
  148. ];
  149. this.studentsDataset = [
  150. {
  151. key: SERVICES_KEY.PROXIMO,
  152. title: i18n.t('screens.proximo.title'),
  153. subtitle: i18n.t('screens.services.descriptions.proximo'),
  154. image: PROXIMO_IMAGE,
  155. onPress: (): void => nav.navigate('proximo'),
  156. badgeFunction: (dashboard: FullDashboardType): number =>
  157. dashboard.proximo_articles,
  158. },
  159. {
  160. key: SERVICES_KEY.WIKETUD,
  161. title: 'Wiketud',
  162. subtitle: i18n.t('screens.services.descriptions.wiketud'),
  163. image: WIKETUD_IMAGE,
  164. onPress: (): void =>
  165. nav.navigate('website', {
  166. host: AvailableWebsites.websites.WIKETUD,
  167. title: 'Wiketud',
  168. }),
  169. },
  170. {
  171. key: SERVICES_KEY.ELUS_ETUDIANTS,
  172. title: 'Élus Étudiants',
  173. subtitle: i18n.t('screens.services.descriptions.elusEtudiants'),
  174. image: EE_IMAGE,
  175. onPress: (): void =>
  176. nav.navigate('website', {
  177. host: AvailableWebsites.websites.ELUS_ETUDIANTS,
  178. title: 'Élus Étudiants',
  179. }),
  180. },
  181. {
  182. key: SERVICES_KEY.TUTOR_INSA,
  183. title: "Tutor'INSA",
  184. subtitle: i18n.t('screens.services.descriptions.tutorInsa'),
  185. image: TUTORINSA_IMAGE,
  186. onPress: (): void =>
  187. nav.navigate('website', {
  188. host: AvailableWebsites.websites.TUTOR_INSA,
  189. title: "Tutor'INSA",
  190. }),
  191. badgeFunction: (dashboard: FullDashboardType): number =>
  192. dashboard.available_tutorials,
  193. },
  194. ];
  195. this.insaDataset = [
  196. {
  197. key: SERVICES_KEY.RU,
  198. title: i18n.t('screens.menu.title'),
  199. subtitle: i18n.t('screens.services.descriptions.self'),
  200. image: RU_IMAGE,
  201. onPress: (): void => nav.navigate('self-menu'),
  202. badgeFunction: (dashboard: FullDashboardType): number =>
  203. dashboard.today_menu.length,
  204. },
  205. {
  206. key: SERVICES_KEY.AVAILABLE_ROOMS,
  207. title: i18n.t('screens.websites.rooms'),
  208. subtitle: i18n.t('screens.services.descriptions.availableRooms'),
  209. image: ROOM_IMAGE,
  210. onPress: (): void =>
  211. nav.navigate('website', {
  212. host: AvailableWebsites.websites.AVAILABLE_ROOMS,
  213. title: i18n.t('screens.websites.rooms'),
  214. }),
  215. },
  216. {
  217. key: SERVICES_KEY.BIB,
  218. title: i18n.t('screens.websites.bib'),
  219. subtitle: i18n.t('screens.services.descriptions.bib'),
  220. image: BIB_IMAGE,
  221. onPress: (): void =>
  222. nav.navigate('website', {
  223. host: AvailableWebsites.websites.BIB,
  224. title: i18n.t('screens.websites.bib'),
  225. }),
  226. },
  227. {
  228. key: SERVICES_KEY.EMAIL,
  229. title: i18n.t('screens.websites.mails'),
  230. subtitle: i18n.t('screens.services.descriptions.mails'),
  231. image: EMAIL_IMAGE,
  232. onPress: (): void =>
  233. nav.navigate('website', {
  234. host: AvailableWebsites.websites.BLUEMIND,
  235. title: i18n.t('screens.websites.mails'),
  236. }),
  237. },
  238. {
  239. key: SERVICES_KEY.ENT,
  240. title: i18n.t('screens.websites.ent'),
  241. subtitle: i18n.t('screens.services.descriptions.ent'),
  242. image: ENT_IMAGE,
  243. onPress: (): void =>
  244. nav.navigate('website', {
  245. host: AvailableWebsites.websites.ENT,
  246. title: i18n.t('screens.websites.ent'),
  247. }),
  248. },
  249. {
  250. key: SERVICES_KEY.INSA_ACCOUNT,
  251. title: i18n.t('screens.insaAccount.title'),
  252. subtitle: i18n.t('screens.services.descriptions.insaAccount'),
  253. image: ACCOUNT_IMAGE,
  254. onPress: (): void =>
  255. nav.navigate('website', {
  256. host: AvailableWebsites.websites.INSA_ACCOUNT,
  257. title: i18n.t('screens.insaAccount.title'),
  258. }),
  259. },
  260. ];
  261. this.specialDataset = [
  262. {
  263. key: SERVICES_KEY.WASHERS,
  264. title: i18n.t('screens.proxiwash.washers'),
  265. subtitle: i18n.t('screens.services.descriptions.washers'),
  266. image: WASHER_IMAGE,
  267. onPress: (): void => nav.navigate('proxiwash'),
  268. badgeFunction: (dashboard: FullDashboardType): number =>
  269. dashboard.available_washers,
  270. },
  271. {
  272. key: SERVICES_KEY.DRYERS,
  273. title: i18n.t('screens.proxiwash.dryers'),
  274. subtitle: i18n.t('screens.services.descriptions.washers'),
  275. image: DRYER_IMAGE,
  276. onPress: (): void => nav.navigate('proxiwash'),
  277. badgeFunction: (dashboard: FullDashboardType): number =>
  278. dashboard.available_dryers,
  279. },
  280. ];
  281. this.categoriesDataset = [
  282. {
  283. key: SERVICES_CATEGORIES_KEY.AMICALE,
  284. title: i18n.t('screens.services.categories.amicale'),
  285. subtitle: i18n.t('screens.services.more'),
  286. image: AMICALE_LOGO,
  287. content: this.amicaleDataset,
  288. },
  289. {
  290. key: SERVICES_CATEGORIES_KEY.STUDENTS,
  291. title: i18n.t('screens.services.categories.students'),
  292. subtitle: i18n.t('screens.services.more'),
  293. image: 'account-group',
  294. content: this.studentsDataset,
  295. },
  296. {
  297. key: SERVICES_CATEGORIES_KEY.INSA,
  298. title: i18n.t('screens.services.categories.insa'),
  299. subtitle: i18n.t('screens.services.more'),
  300. image: 'school',
  301. content: this.insaDataset,
  302. },
  303. {
  304. key: SERVICES_CATEGORIES_KEY.SPECIAL,
  305. title: i18n.t('screens.services.categories.special'),
  306. subtitle: i18n.t('screens.services.categories.special'),
  307. image: 'star',
  308. content: this.specialDataset,
  309. },
  310. ];
  311. }
  312. /**
  313. * Redirects the user to the login screen if he is not logged in
  314. *
  315. * @param route
  316. * @returns {null}
  317. */
  318. onAmicaleServicePress(route: string) {
  319. if (ConnectionManager.getInstance().isLoggedIn()) {
  320. this.navigation.navigate(route);
  321. } else {
  322. this.navigation.navigate('login', {nextScreen: route});
  323. }
  324. }
  325. /**
  326. * Gets the list of amicale's services
  327. *
  328. * @param excludedItems Ids of items to exclude from the returned list
  329. * @returns {Array<ServiceItemType>}
  330. */
  331. getAmicaleServices(excludedItems?: Array<string>): Array<ServiceItemType> {
  332. if (excludedItems != null) {
  333. return getStrippedServicesList(excludedItems, this.amicaleDataset);
  334. }
  335. return this.amicaleDataset;
  336. }
  337. /**
  338. * Gets the list of students' services
  339. *
  340. * @param excludedItems Ids of items to exclude from the returned list
  341. * @returns {Array<ServiceItemType>}
  342. */
  343. getStudentServices(excludedItems?: Array<string>): Array<ServiceItemType> {
  344. if (excludedItems != null) {
  345. return getStrippedServicesList(excludedItems, this.studentsDataset);
  346. }
  347. return this.studentsDataset;
  348. }
  349. /**
  350. * Gets the list of INSA's services
  351. *
  352. * @param excludedItems Ids of items to exclude from the returned list
  353. * @returns {Array<ServiceItemType>}
  354. */
  355. getINSAServices(excludedItems?: Array<string>): Array<ServiceItemType> {
  356. if (excludedItems != null) {
  357. return getStrippedServicesList(excludedItems, this.insaDataset);
  358. }
  359. return this.insaDataset;
  360. }
  361. /**
  362. * Gets the list of special services
  363. *
  364. * @param excludedItems Ids of items to exclude from the returned list
  365. * @returns {Array<ServiceItemType>}
  366. */
  367. getSpecialServices(excludedItems?: Array<string>): Array<ServiceItemType> {
  368. if (excludedItems != null) {
  369. return getStrippedServicesList(excludedItems, this.specialDataset);
  370. }
  371. return this.specialDataset;
  372. }
  373. /**
  374. * Gets all services sorted by category
  375. *
  376. * @param excludedItems Ids of categories to exclude from the returned list
  377. * @returns {Array<ServiceCategoryType>}
  378. */
  379. getCategories(excludedItems?: Array<string>): Array<ServiceCategoryType> {
  380. if (excludedItems != null) {
  381. return getStrippedServicesList(excludedItems, this.categoriesDataset);
  382. }
  383. return this.categoriesDataset;
  384. }
  385. }