index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <!-- Search for member structures -->
  2. <template>
  3. <LayoutContainer>
  4. <!-- Header -->
  5. <v-row>
  6. <v-layout>
  7. <h2 class="flex">
  8. {{ $t("member_companies") }}
  9. </h2>
  10. <v-btn-toggle mandatory dense :value="mapview ? 0 : 1" @change="viewChanged">
  11. <v-btn>
  12. {{ $t("map") }}
  13. </v-btn>
  14. <v-btn>
  15. {{ $t("list") }}
  16. </v-btn>
  17. </v-btn-toggle>
  18. </v-layout>
  19. </v-row>
  20. <v-row>
  21. <!-- Map Column (hidden in 'list-view' mode)-->
  22. <v-col
  23. v-show="mapview"
  24. cols="12"
  25. sm="6"
  26. >
  27. <no-ssr>
  28. <UiMapStructures
  29. ref="map"
  30. :structures="filteredStructures"
  31. @boundsUpdated="mapBoundsFilterChanged"
  32. />
  33. </no-ssr>
  34. </v-col>
  35. <!-- Results column -->
  36. <v-col
  37. cols="12"
  38. :sm="mapview ? 6 : 12"
  39. >
  40. <!-- Search form -->
  41. <v-row>
  42. <v-form method="get" class="mt-8 w100">
  43. <v-container>
  44. <v-row>
  45. <v-col cols="12" md="6" class="py-2 px-1">
  46. <v-text-field
  47. v-model="textFilter"
  48. type="text"
  49. outlined
  50. clearable
  51. hide-details
  52. append-icon="mdi-magnify"
  53. :label="$t('what') + ' ?'"
  54. @click:append="search"
  55. @keydown.enter="search"
  56. />
  57. </v-col>
  58. <v-col cols="12" md="6" class="py-2 px-1">
  59. <UiSearchAddress
  60. ref="addressSearch"
  61. type="municipality"
  62. @change="locationFilterChanged"
  63. />
  64. </v-col>
  65. </v-row>
  66. <v-row>
  67. <v-col v-if="listview && $vuetify.breakpoint.mdAndUp" cols="2" class="py-2 px-1">
  68. <v-btn class="h100" @click="reinitializeFilters">
  69. {{ $t('reinitialize') }}
  70. </v-btn>
  71. </v-col>
  72. <v-col :cols="(listview && $vuetify.breakpoint.mdAndUp) ? 8 : 12">
  73. <v-row class="filters">
  74. <v-col lg="3" :md="listview ? 3 : 6" sm="6" cols="12" class="py-2 px-1">
  75. <v-select
  76. v-model="practicesFilter"
  77. :label="$t('type')"
  78. :items="translatedPractices"
  79. item-value="id"
  80. item-text="label"
  81. filled
  82. hide-details
  83. @change="search"
  84. />
  85. </v-col>
  86. <v-col lg="3" :md="listview ? 3 : 6" sm="6" cols="12" class="py-2 px-1">
  87. <v-select
  88. v-model="departmentFilter"
  89. :items="departments"
  90. item-value="code"
  91. item-text="label"
  92. :label="$t('department')"
  93. filled
  94. hide-details
  95. @change="search"
  96. />
  97. </v-col>
  98. <v-col lg="3" :md="listview ? 3 : 6" sm="6" cols="12" class="py-2 px-1">
  99. <v-select
  100. v-model="federationFilter"
  101. :items="federations"
  102. item-value="id"
  103. item-text="name"
  104. :label="$t('federation')"
  105. filled
  106. hide-details
  107. @change="search"
  108. />
  109. </v-col>
  110. <v-col lg="3" :md="listview ? 3 : 6" sm="6" cols="12" class="py-2 px-1">
  111. <v-select
  112. v-model="distanceFilter"
  113. :label="$t('distance')"
  114. :items="[
  115. {distance: 10, label: '10km'},
  116. {distance: 30, label: '30km'},
  117. {distance: 100, label: '100km'},
  118. {distance: 200, label: '200km'}
  119. ]"
  120. item-value="distance"
  121. item-text="label"
  122. filled
  123. hide-details
  124. @change="search"
  125. />
  126. </v-col>
  127. </v-row>
  128. </v-col>
  129. <v-col v-if="listview && $vuetify.breakpoint.mdAndUp" cols="2" class="py-2 px-1 d-flex justify-end">
  130. <v-btn class="h100">
  131. {{ $t('search') }}
  132. </v-btn>
  133. </v-col>
  134. </v-row>
  135. <v-row v-show="mapview || $vuetify.breakpoint.smAndDown" class="px-2 pt-2">
  136. <v-btn @click="reinitializeFilters">
  137. {{ $t('reinitialize') }}
  138. </v-btn>
  139. <v-spacer />
  140. <v-btn @click="search">
  141. {{ $t('search') }}
  142. </v-btn>
  143. </v-row>
  144. </v-container>
  145. </v-form>
  146. </v-row>
  147. <div class="pt-4 mt-6">
  148. <!-- loading skeleton -->
  149. <v-container v-if="$fetchState.pending">
  150. <v-row v-for="i in 3" :key="i" justify="space-between" class="mt-1 mb-3">
  151. <v-col v-for="j in 2" :key="j" cols="12" :md="mapview ? 6 : 12" class="py-2 px-1">
  152. <v-skeleton-loader type="card" :loading="true" />
  153. </v-col>
  154. </v-row>
  155. </v-container>
  156. <!-- Results -->
  157. <v-data-iterator
  158. v-else
  159. :items="onMapFilteredStructures"
  160. :page.sync="page"
  161. :items-per-page="itemsPerPage"
  162. sort-by="name"
  163. hide-default-footer
  164. no-data-text=""
  165. >
  166. <template #header>
  167. <i class="results-count">{{ totalRecords }} {{ $t('results') }}</i>
  168. </template>
  169. <template #default="props">
  170. <v-row justify="space-between" class="mt-1 mb-3">
  171. <v-col
  172. v-for="structure in props.items"
  173. :key="structure.name"
  174. cols="12"
  175. sm="12"
  176. :lg="mapview ? 6 : 12"
  177. class="py-2 px-1"
  178. >
  179. <v-card
  180. elevation="1"
  181. outlined
  182. :class="'structure-card pa-3 d-flex ' + ((mapview || $vuetify.breakpoint.smAndDown) ? 'flex-column' : 'flex-row align-items-center')"
  183. >
  184. <div class="d-flex justify-center">
  185. <v-img
  186. :src="structure.logoId ? ('https://api.opentalent.fr/app.php/_internal/secure/files/' + structure.logoId) : '/images/default.jpg'"
  187. alt="poster"
  188. height="80px"
  189. min-width="160px"
  190. max-width="80%"
  191. max-height="100%"
  192. :contain="true"
  193. style="margin: 12px;"
  194. />
  195. </div>
  196. <div class="d-flex flex-column">
  197. <v-chip-group v-if="structure.practices" active-class="primary--text">
  198. <v-chip v-for="practice in structure.practices" :key="practice" outlined small pill>
  199. {{ $t(practice) }}
  200. </v-chip>
  201. </v-chip-group>
  202. <v-card-title class="title">
  203. {{ structure.name }}
  204. </v-card-title>
  205. <v-card-text class="infos">
  206. <table>
  207. <tr>
  208. <td class="py-1 pr-2">
  209. <font-awesome-icon class="icon" :icon="['fas', 'map-marker-alt']" />
  210. </td>
  211. <td class="py-1">
  212. <span v-if="structure.streetAddress">{{ structure.streetAddress }}<br></span>
  213. <span v-if="structure.postalCode" class="postalCode">{{ structure.postalCode }} </span>
  214. {{ structure.addressCity }}
  215. </td>
  216. </tr>
  217. <tr>
  218. <td class="py-1 pr-2">
  219. <font-awesome-icon class="icon" :icon="['fas', 'project-diagram']" />
  220. </td>
  221. <td class="py-1">
  222. {{ structure.n1Name }}
  223. </td>
  224. </tr>
  225. </table>
  226. </v-card-text>
  227. </div>
  228. <span class="flex-fill" />
  229. <v-card-actions :class="listview ? 'align-self-end' : ''">
  230. <v-btn
  231. class="see"
  232. :to="{path: '/structures/' + structure.id, query: { parent: parent, view: view, theme: theme }}"
  233. nuxt
  234. >
  235. <span style="margin-right: 6px;">{{ $t("see_more") }}</span>
  236. <font-awesome-icon :icon="['fa', 'caret-right']" />
  237. </v-btn>
  238. </v-card-actions>
  239. </v-card>
  240. </v-col>
  241. </v-row>
  242. </template>
  243. <template #footer>
  244. <v-pagination
  245. v-model="page"
  246. :length="pageCount"
  247. total-visible="9"
  248. color="#e4611b"
  249. />
  250. </template>
  251. </v-data-iterator>
  252. </div>
  253. </v-col>
  254. </v-row>
  255. </LayoutContainer>
  256. </template>
  257. <script lang="ts">
  258. import Vue from 'vue'
  259. import { LatLngBounds } from 'leaflet'
  260. import departments from '@/enums/departments'
  261. import practices from '@/enums/practices'
  262. import sphericDistance from '@/services/utils/geo'
  263. import StructuresProvider from '~/services/data/StructuresProvider'
  264. export default Vue.extend({
  265. validate ({ query }) {
  266. if (!/^\d+$/.test(query.parent as string ?? '')) {
  267. // eslint-disable-next-line no-console
  268. console.error('Missing parameter: parent')
  269. return false
  270. }
  271. if (query.view && !['map', 'list'].includes(query.view as string)) {
  272. // eslint-disable-next-line no-console
  273. console.error('Invalid parameter: view')
  274. return false
  275. }
  276. return true
  277. },
  278. data () {
  279. return {
  280. parent: parseInt(this.$route.query.parent as string),
  281. view: this.$route.query.view ?? 'map',
  282. theme: this.$route.query.theme ?? 'orange',
  283. structures: [] as Array<Structure>,
  284. filteredStructures: [] as Array<Structure>,
  285. federations: [] as Array<{ id: number | null, name: string | null }>,
  286. loading: true,
  287. page: 1,
  288. itemsPerPage: 8,
  289. departments: departments as {code: string, label: string}[],
  290. practices: practices as {id: string}[],
  291. textFilter: null as string | null,
  292. locationFilter: null as Coordinates | null,
  293. practicesFilter: null as string | null,
  294. departmentFilter: null as string | null,
  295. federationFilter: null as number | null,
  296. distanceFilter: null as number | null,
  297. mapBoundsFilter: null as LatLngBounds | null,
  298. mapBoundsFilterStarted: false // map bounds filter is only activated when the map bounds are updated
  299. }
  300. },
  301. async fetch () {
  302. await new StructuresProvider(this.$axios).getAll(this.parent).then(
  303. (res) => {
  304. this.structures = res
  305. this.filteredStructures = res
  306. // populate federations filter
  307. for (const s of res) {
  308. const f = {
  309. id: s.n1Id,
  310. name: s.n1Name
  311. }
  312. if (!this.federations.includes(f)) {
  313. this.federations.push(f)
  314. }
  315. }
  316. })
  317. },
  318. computed: {
  319. onMapFilteredStructures (): Array<Structure> {
  320. if (this.mapBoundsFilterStarted) {
  321. return this.filteredStructures.filter((s) => {
  322. return this.matchMapBounds(s)
  323. })
  324. } else {
  325. return this.filteredStructures
  326. }
  327. },
  328. totalRecords (): number {
  329. return this.onMapFilteredStructures.length
  330. },
  331. pageCount (): number {
  332. return Math.floor(this.totalRecords / this.itemsPerPage) + 1
  333. },
  334. mapview (): boolean {
  335. return this.view === 'map'
  336. },
  337. listview (): boolean {
  338. return this.view === 'list'
  339. },
  340. translatedPractices (): Array<{ id: string, label: string }> {
  341. const tPractices = []
  342. for (const practice of this.practices) {
  343. tPractices.push({ id: practice.id, label: this.$t(practice.id) as string })
  344. }
  345. return tPractices
  346. }
  347. },
  348. methods: {
  349. viewChanged (e: number) {
  350. this.view = (e === 0) ? 'map' : 'list'
  351. },
  352. textFilterChanged (newVal: string) {
  353. this.textFilter = newVal
  354. },
  355. locationFilterChanged (newVal: Coordinates) {
  356. this.locationFilter = newVal
  357. if (this.distanceFilter === null) {
  358. this.distanceFilter = 10
  359. }
  360. this.search()
  361. },
  362. fitMapToResults (): void {
  363. (this.$refs.map as any).zoomOnResults()
  364. },
  365. mapBoundsFilterChanged (newBounds: LatLngBounds) {
  366. this.mapBoundsFilterStarted = true
  367. this.mapBoundsFilter = newBounds
  368. },
  369. reinitializeFilters (): void {
  370. this.textFilter = null
  371. this.locationFilter = null
  372. this.practicesFilter = null
  373. this.departmentFilter = null
  374. this.federationFilter = null
  375. this.distanceFilter = null
  376. this.mapBoundsFilter = null
  377. const addressSearch = this.$refs.addressSearch as any
  378. addressSearch.clear()
  379. const map = this.$refs.map as any
  380. map.resetBounds()
  381. this.filteredStructures = this.structures
  382. },
  383. /**
  384. * Does the structure match the current textFilter
  385. * @param structure
  386. * @returns {boolean}
  387. */
  388. matchTextFilter (structure: Structure): boolean {
  389. if (!this.textFilter) { return true }
  390. return structure.name.toLowerCase().includes(this.textFilter.toLowerCase())
  391. },
  392. /**
  393. * Does the structure match the current locationFilter
  394. * @param structure
  395. * @returns {boolean}
  396. */
  397. matchLocationFilter (structure: Structure): boolean {
  398. if (!this.locationFilter) { return true }
  399. if (!structure.latitude || !structure.longitude) { return false }
  400. const radius = Number(this.distanceFilter) ?? 5
  401. return sphericDistance(
  402. this.locationFilter.latitude, this.locationFilter.longitude, structure.latitude, structure.longitude
  403. ) <= radius
  404. },
  405. /**
  406. * Does the structure match the current practicesFilter
  407. * @param structure
  408. * @returns {boolean}
  409. */
  410. matchPracticesFilter (structure: Structure): boolean {
  411. if (!this.practicesFilter) { return true }
  412. return structure.practices && structure.practices.includes(this.practicesFilter)
  413. },
  414. /**
  415. * Does the structure match the current departmentFilter
  416. * @param structure
  417. * @returns {boolean}
  418. */
  419. matchDepartmentFilter (structure: Structure): boolean {
  420. if (!this.departmentFilter) { return true }
  421. return structure.postalCode !== null && structure.postalCode.startsWith(this.departmentFilter)
  422. },
  423. /**
  424. * Does the structure match the current federationFilter
  425. * @param structure
  426. * @returns {boolean}
  427. */
  428. matchFederationFilter (structure: Structure): boolean {
  429. if (!this.federationFilter) { return true }
  430. return structure.parents.includes(this.federationFilter)
  431. },
  432. /**
  433. * Does the structure match the current federationFilter
  434. * @param structure
  435. * @returns {boolean}
  436. */
  437. matchMapBounds (structure: Structure): boolean {
  438. if (!this.mapBoundsFilter) { return true }
  439. if (!(structure.latitude && structure.longitude)) { return false }
  440. return this.mapBoundsFilter.getSouth() <= structure.latitude &&
  441. structure.latitude <= this.mapBoundsFilter.getNorth() &&
  442. this.mapBoundsFilter.getWest() <= structure.longitude &&
  443. structure.longitude <= this.mapBoundsFilter.getEast()
  444. },
  445. /**
  446. * Does the structure match each of the page filters
  447. * @param structure
  448. * @returns {boolean}
  449. */
  450. matchFilters (structure: Structure): boolean {
  451. return this.matchTextFilter(structure) &&
  452. this.matchLocationFilter(structure) &&
  453. this.matchPracticesFilter(structure) &&
  454. this.matchDepartmentFilter(structure) &&
  455. this.matchFederationFilter(structure)
  456. // this.matchMapBounds(structure)
  457. },
  458. /**
  459. * Update the filteredStructures array
  460. */
  461. search (): void {
  462. this.filteredStructures = this.structures.filter((s) => { return this.matchFilters(s) })
  463. this.fitMapToResults()
  464. }
  465. }
  466. })
  467. </script>
  468. <style scoped lang="scss">
  469. @import 'assets/style/variables.scss';
  470. h2 {
  471. color: $theme;
  472. }
  473. .structure-card {
  474. height: 100%;
  475. color: #666666;
  476. }
  477. .infos .col {
  478. padding: 6px 12px;
  479. }
  480. .title {
  481. word-break: normal;
  482. color: $theme;
  483. font-size: 18px;
  484. font-weight: 500;
  485. line-height: 1.6rem;
  486. }
  487. .icon {
  488. color: $theme;
  489. }
  490. .results-count {
  491. font-size: .8em;
  492. color: #666;
  493. }
  494. </style>