Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 1 | <template> |
| 2 | <div class="hub-page-container"> |
Faris Ansari | 0d08da5 | 2018-11-05 15:47:26 +0530 | [diff] [blame] | 3 | <component :is="current_page.component" :key="current_page.key"></component> |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 4 | </div> |
| 5 | </template> |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 6 | |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 7 | <script> |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 8 | |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 9 | import Home from './pages/Home.vue'; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 10 | import Search from './pages/Search.vue'; |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 11 | import Category from './pages/Category.vue'; |
Faris Ansari | 8e04487 | 2018-08-30 15:35:06 +0530 | [diff] [blame] | 12 | import SavedItems from './pages/SavedItems.vue'; |
| 13 | import PublishedItems from './pages/PublishedItems.vue'; |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 14 | import Item from './pages/Item.vue'; |
| 15 | import Seller from './pages/Seller.vue'; |
| 16 | import Publish from './pages/Publish.vue'; |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 17 | import Buying from './pages/Buying.vue'; |
Faris Ansari | 725603c | 2018-08-27 19:51:36 +0530 | [diff] [blame] | 18 | import Selling from './pages/Selling.vue'; |
| 19 | import Messages from './pages/Messages.vue'; |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 20 | import Profile from './pages/Profile.vue'; |
| 21 | import NotFound from './pages/NotFound.vue'; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 22 | |
Prateeksha Singh | 18e3c61 | 2018-09-03 15:22:13 +0530 | [diff] [blame] | 23 | function get_route_map() { |
| 24 | const read_only_routes = { |
| 25 | 'marketplace/home': Home, |
| 26 | 'marketplace/search/:keyword': Search, |
| 27 | 'marketplace/category/:category': Category, |
| 28 | 'marketplace/item/:item': Item, |
| 29 | 'marketplace/seller/:seller': Seller, |
| 30 | 'marketplace/not-found': NotFound, |
| 31 | } |
| 32 | const registered_routes = { |
| 33 | 'marketplace/profile': Profile, |
| 34 | 'marketplace/saved-items': SavedItems, |
| 35 | 'marketplace/publish': Publish, |
| 36 | 'marketplace/published-items': PublishedItems, |
| 37 | 'marketplace/buying': Buying, |
| 38 | 'marketplace/buying/:item': Messages, |
| 39 | 'marketplace/selling': Selling, |
| 40 | 'marketplace/selling/:buyer/:item': Messages |
| 41 | } |
Prateeksha Singh | bffb707 | 2018-09-03 16:39:59 +0530 | [diff] [blame] | 42 | |
| 43 | return hub.is_seller_registered() |
Prateeksha Singh | 18e3c61 | 2018-09-03 15:22:13 +0530 | [diff] [blame] | 44 | ? Object.assign({}, read_only_routes, registered_routes) |
| 45 | : read_only_routes; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | export default { |
| 49 | data() { |
| 50 | return { |
| 51 | current_page: this.get_current_page() |
| 52 | } |
| 53 | }, |
| 54 | mounted() { |
| 55 | frappe.route.on('change', () => { |
Faris Ansari | 76b31de | 2018-09-11 17:38:31 +0530 | [diff] [blame] | 56 | if (frappe.get_route()[0] === 'marketplace') { |
| 57 | this.set_current_page(); |
| 58 | frappe.utils.scroll_to(0); |
| 59 | } |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 60 | }); |
| 61 | }, |
| 62 | methods: { |
| 63 | set_current_page() { |
| 64 | this.current_page = this.get_current_page(); |
| 65 | }, |
| 66 | get_current_page() { |
Prateeksha Singh | 18e3c61 | 2018-09-03 15:22:13 +0530 | [diff] [blame] | 67 | const route_map = get_route_map(); |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 68 | const curr_route = frappe.get_route_str(); |
| 69 | let route = Object.keys(route_map).filter(route => route == curr_route)[0]; |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 70 | if (!route) { |
| 71 | // find route by matching it with dynamic part |
| 72 | const curr_route_parts = curr_route.split('/'); |
| 73 | const weighted_routes = Object.keys(route_map) |
| 74 | .map(route_str => route_str.split('/')) |
| 75 | .filter(route_parts => route_parts.length === curr_route_parts.length) |
| 76 | .reduce((obj, route_parts) => { |
| 77 | const key = route_parts.join('/'); |
| 78 | let weight = 0; |
| 79 | route_parts.forEach((part, i) => { |
| 80 | const curr_route_part = curr_route_parts[i]; |
Faris Ansari | 90265ae | 2018-08-30 14:36:05 +0530 | [diff] [blame] | 81 | if (part === curr_route_part || part.includes(':')) { |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 82 | weight += 1; |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | obj[key] = weight; |
| 87 | return obj; |
| 88 | }, {}); |
| 89 | |
| 90 | // get the route with the highest weight |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 91 | for (let key in weighted_routes) { |
| 92 | const route_weight = weighted_routes[key]; |
Faris Ansari | 90265ae | 2018-08-30 14:36:05 +0530 | [diff] [blame] | 93 | if (route_weight === curr_route_parts.length) { |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 94 | route = key; |
Faris Ansari | 90265ae | 2018-08-30 14:36:05 +0530 | [diff] [blame] | 95 | break; |
| 96 | } else { |
| 97 | route = null; |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Faris Ansari | f089dad | 2018-08-26 22:20:16 +0530 | [diff] [blame] | 102 | if (!route) { |
Faris Ansari | 0d08da5 | 2018-11-05 15:47:26 +0530 | [diff] [blame] | 103 | return { |
| 104 | key: 'not-found', |
| 105 | component: NotFound |
| 106 | }; |
Faris Ansari | f089dad | 2018-08-26 22:20:16 +0530 | [diff] [blame] | 107 | } |
| 108 | |
Faris Ansari | 0d08da5 | 2018-11-05 15:47:26 +0530 | [diff] [blame] | 109 | return { |
| 110 | key: curr_route, |
| 111 | component: route_map[route] |
| 112 | } |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | </script> |