Guest User

Untitled

a guest
Mar 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.54 KB | None | 0 0
  1. <template>
  2. <complex-page
  3. :collapse="true"
  4. v-if="handlers"
  5. :bulk="state[view].bulk"
  6. :schema="state[view].schema"
  7. :records="state[view].records"
  8. :handlers="handlers"
  9. :table-columns="state[view].tableColumns"
  10. :search-term="state[view].searchTerm"
  11. :id="componentId"
  12. >
  13. <template slot="body">
  14. <!--<loading :loading="loading.records"></loading>-->
  15. <router-view
  16. :handlers="handlers"
  17. :state="state[view]"
  18. :handle-load-records="loadRecords()[view]"
  19. :view="view"
  20. @contact-destroyed="resetContact"
  21. ></router-view>
  22. <client-add-modal v-if="isClientOrProspect" ref="addClientModal" @success="clientAddSuccess"></client-add-modal>
  23. <client-import-modal
  24. v-if="isClientOrProspect"
  25. :type="view"
  26. :columns="state.clients.importColumns"
  27. @download="templateDownloadHandler"
  28. @complete="loadCurrentView"
  29. :import-handler="importHandler"
  30. ref="clientImportModal"></client-import-modal>
  31. <web-lead-assignee-modal
  32. v-if="user && view === 'webLeads'"
  33. ref="webLeadApproveModal"
  34. :users="branchUsers"
  35. :user="user"
  36. :approve-handler="approveHandler"
  37. @select-assignee="selectAssignee"
  38. ></web-lead-assignee-modal>
  39. </template>
  40. </complex-page>
  41. </template>
  42. <script>
  43. import { uniqueKey } from 'utils';
  44. import EventBus from 'qta/EventBus';
  45. import ComplexPage from 'containers/ComplexPage.vue';
  46. import Loading from 'components/Loading.vue';
  47. import ClientImportModal from 'components/ImportModal.vue';
  48. import { importProspectAndClient, fetchUsers, fetchUserInformation } from 'script/api/calls';
  49. import ClientAddModal from '../../components/ClientAddModal.vue';
  50. import Listener from '../../ComplexPageListeners';
  51. import state from '../../store/index/state';
  52. import {
  53. getClients,
  54. getProspects,
  55. getWebLeads,
  56. getArchived,
  57. archive,
  58. unArchive,
  59. getArchivedWebLeads,
  60. approveWebLead,
  61. } from '../../api/calls';
  62. import { CLIENT_TEMPLATE_IMPORT_URL, PROSPECT_TEMPLATE_IMPORT_URL } from '../../api/index';
  63. import { urlQueryBuilderMixin } from '../../../mixins/index';
  64. import EmailMixin from '../../../mixins/EmailMixin';
  65. import WebLeadAssigneeModal from '../../../webleads/components/modal/WebLeadAssigneeModal.vue';
  66.  
  67. const clientsMenuId = 'clientsMenu';
  68. const enquiriesMenuId = 'enquiriesMenu';
  69. const tabActiveClass = 'active';
  70.  
  71. export default {
  72. menus: {
  73. clientsMenu: '',
  74. enquiriesMenu: '',
  75. },
  76. tabs: {
  77. clients: clientsMenuId,
  78. prospects: clientsMenuId,
  79. archived: clientsMenuId,
  80. webLeads: enquiriesMenuId,
  81. archivedWebLeads: enquiriesMenuId,
  82. },
  83. mixins: [urlQueryBuilderMixin, EmailMixin],
  84. data() {
  85. return {
  86. loading: {
  87. records: false,
  88. },
  89. state,
  90. handlers: null,
  91. sortableAlias: {
  92. name: 'first_name',
  93. last_updated: 'updated_at',
  94. preferred_intake: 'preferred_intake',
  95. applications: 'application_count',
  96. },
  97. user: null,
  98. branchUsers: [],
  99. bulk: {
  100. ids: [],
  101. },
  102. assignee: null,
  103. componentId: null,
  104. };
  105. },
  106. components: {
  107. Loading,
  108. ClientAddModal,
  109. ClientImportModal,
  110. ComplexPage,
  111. WebLeadAssigneeModal,
  112. },
  113. watch: {
  114. $route() {
  115. this.makeMenuActive();
  116. },
  117. },
  118. computed: {
  119. view() {
  120. const route = this.$route.name;
  121. if (route === 'client:index') {
  122. return 'clients';
  123. }
  124. if (route === 'client:archived') {
  125. return 'archived';
  126. }
  127. if (route === 'client:prospects') {
  128. return 'prospects';
  129. }
  130. if (route === 'client:webLeads') {
  131. return 'webLeads';
  132. }
  133. if (route === 'client:archivedWebLeads') {
  134. return 'archivedWebLeads';
  135. }
  136. return null;
  137. },
  138. isClientOrProspect() {
  139. return this.view && (this.view === 'clients' || this.view === 'prospects');
  140. },
  141. },
  142. methods: {
  143. uniqueKey,
  144. makeMenuActive() {
  145. Object.keys(this.$options.menus).forEach((menu) => {
  146. const menuElement = this.$options.menus[menu];
  147. if (menuElement) {
  148. menuElement.classList.remove(tabActiveClass);
  149. }
  150. });
  151. const tabElement = this.$options.menus[this.$options.tabs[this.view]];
  152. if (tabElement) {
  153. tabElement.classList.add(tabActiveClass);
  154. }
  155. },
  156. fetchClients(query) {
  157. this.loading.records = true;
  158. return getClients(query).then(({ data: { data, meta } }) => {
  159. this.state.clients.records = Object.assign({ data }, meta);
  160. this.$nextTick(() => {
  161. $(`#${this.componentId} .js-dropdown`).dropdown();
  162. });
  163. }).catch(() => {
  164. this.state.clients.records.data = [];
  165. Agentcis.alert('Unable to fetch clients.', 'error');
  166. }).finally(() => {
  167. this.loading.records = false;
  168. });
  169. },
  170. fetchProspects(query) {
  171. this.loading.records = true;
  172. return getProspects(query).then(({ data: { data, meta } }) => {
  173. this.state.prospects.records = Object.assign({ data }, meta);
  174. this.$nextTick(() => {
  175. $(`#${this.componentId} .js-dropdown`).dropdown();
  176. });
  177. }).catch(() => {
  178. this.state.prospects.records.data = [];
  179. Agentcis.alert('Unable to fetch prospects.', 'error');
  180. }).finally(() => {
  181. this.loading.records = false;
  182. });
  183. },
  184. fetchWebLeads(query) {
  185. this.loading.records = true;
  186. return getWebLeads(query).then(({ data: { data, meta, permissions } }) => {
  187. this.state.webLeads.records = Object.assign({ data }, meta, { permissions });
  188. if (!permissions.viewArchivedWebLeads) {
  189. const subPages = this.state.webLeads.schema.components.subPages;
  190. const index = subPages.findIndex(page => page.id === 'client:archivedWebLeads');
  191. if (index > -1) {
  192. subPages.splice(index, 1);
  193. }
  194. }
  195. this.fetchUserInformation();
  196. this.fetchBranchUsers();
  197. this.$nextTick(() => {
  198. $(`#${this.componentId} .js-dropdown`).dropdown();
  199. });
  200. }).catch((err) => {
  201. this.state.webLeads.records.data = [];
  202. if (err.response.status === 403) {
  203. Agentcis.alert('You do not have the correct privileges.', 'danger');
  204. return this.$router.push({ name: 'client:index' });
  205. }
  206. Agentcis.alert('Unable to fetch webleads.', 'error');
  207. return true;
  208. }).finally(() => {
  209. this.loading.records = false;
  210. });
  211. },
  212. fetchArchivedWebLeads(query) {
  213. this.loading.records = true;
  214. return getArchivedWebLeads(query).then(({ data: { data, meta } }) => {
  215. this.state.archivedWebLeads.records = Object.assign({ data }, meta);
  216. this.$nextTick(() => {
  217. $(`#${this.componentId} .js-dropdown`).dropdown();
  218. });
  219. }).catch((err) => {
  220. this.state.archivedWebLeads.records.data = [];
  221. if (err.response.status === 403) {
  222. Agentcis.alert('You do not have the correct privileges.', 'danger');
  223. return this.$router.push({ name: 'client:index' });
  224. }
  225. Agentcis.alert('Unable to fetch webleads.', 'error');
  226. return true;
  227. }).finally(() => {
  228. this.loading.records = false;
  229. });
  230. },
  231. fetchArchived(query) {
  232. this.loading.records = true;
  233. return getArchived(query).then(({ data: { data, meta } }) => {
  234. this.state.archived.records = Object.assign({ data }, meta);
  235. this.$nextTick(() => {
  236. $(`#${this.componentId} .js-dropdown`).dropdown();
  237. });
  238. }).catch(() => {
  239. this.state.archived.records.data = [];
  240. Agentcis.alert('Unable to fetch archived.', 'error');
  241. }).finally(() => {
  242. this.loading.records = false;
  243. });
  244. },
  245. loadRecords() {
  246. return {
  247. clients: this.fetchClients,
  248. prospects: this.fetchProspects,
  249. webLeads: this.fetchWebLeads,
  250. archivedWebLeads: this.fetchArchivedWebLeads,
  251. archived: this.fetchArchived,
  252. };
  253. },
  254. archive(client, index) {
  255. Agentcis.confirm({
  256. message: `Do you want to archive ${client.full_name} ?`,
  257. onAccept: () => {
  258. archive(client.id).then(() => {
  259. this.state[this.view].records.data.splice(index, 1);
  260. Agentcis.alert(`Successfully archived ${client.full_name}`);
  261. }).catch((error) => {
  262. Agentcis.alert(error.response.data.detail || `Unable to archive ${client.full_name}, please try again.`, 'error');
  263. });
  264. },
  265. });
  266. },
  267. restore(client) {
  268. Agentcis.confirm({
  269. message: `Do you want to restore ${client.full_name} ?`,
  270. onAccept: () => {
  271. unArchive(client.id).then(() => {
  272. if (client.status === 'Lead') {
  273. this.fetchArchivedWebLeads();
  274. } else {
  275. this.fetchArchived();
  276. }
  277. Agentcis.alert(`Successfully restored ${client.full_name}`);
  278. }).catch((error) => {
  279. Agentcis.alert(error.response.data.detail || `Unable to restored ${client.full_name}, please try again.`, 'error');
  280. });
  281. },
  282. });
  283. },
  284. approve(clients) {
  285. if (clients.constructor !== Array) {
  286. this.bulk.ids.push(clients);
  287. } else {
  288. this.bulk.ids = clients;
  289. }
  290. this.$refs.webLeadApproveModal.show();
  291. return true;
  292. },
  293. selectAssignee(assignee) {
  294. this.assignee = assignee;
  295. },
  296. importHandler(payload) {
  297. return importProspectAndClient(payload, {
  298. for: this.view === 'clients' ? 'client' : null,
  299. });
  300. },
  301. templateDownloadHandler() {
  302. const url = this.view === 'clients' ? CLIENT_TEMPLATE_IMPORT_URL : PROSPECT_TEMPLATE_IMPORT_URL;
  303. window.location.href = url;
  304. },
  305. loadCurrentView() {
  306. this.loadRecords()[this.view]();
  307. },
  308. clientAddSuccess(view) {
  309. if (this.view === view) {
  310. this.loadCurrentView();
  311. } else {
  312. const route = view === 'clients' ? 'client:index' : 'client:prospects';
  313. this.$router.replace({ name: route });
  314. }
  315. },
  316. fetchBranchUsers() {
  317. fetchUsers()
  318. .then((response) => {
  319. this.branchUsers = response.data;
  320. })
  321. .catch(() => {
  322. Agentcis.alert({
  323. type: 'danger',
  324. message: 'Error on fetching branch users.',
  325. display: 'show',
  326. });
  327. });
  328. },
  329. fetchUserInformation() {
  330. fetchUserInformation()
  331. .then((response) => {
  332. this.user = Object.assign({}, response.data);
  333. })
  334. .catch(() => {
  335. Agentcis.alert({
  336. type: 'danger',
  337. message: 'Error on fetching branch users.',
  338. display: 'show',
  339. });
  340. });
  341. },
  342. approveHandler(callback) {
  343. approveWebLead({ ids: this.bulk.ids, assignee: this.assignee })
  344. .then(() => {
  345. Agentcis.alert('Successfully approved the webleads.', 'success');
  346. this.fetchWebLeads();
  347. this.bulk.ids = [];
  348. this.state[this.view].bulk = {};
  349. this.$refs.webLeadApproveModal.hide();
  350. })
  351. .catch(() => {
  352. Agentcis.alert('Unable to approve the weblead\'s', 'error');
  353. })
  354. .finally(() => {
  355. if (typeof (callback) === 'function') callback();
  356. });
  357. },
  358. resetContact(view) {
  359. this.state[view].searchTerm = '';
  360. Object.values(this.state[view].tableColumns).forEach((col) => {
  361. col.sort.asc = null; // eslint-disable-line no-param-reassign
  362. });
  363. },
  364. },
  365. created() {
  366. const query = this.$route.query;
  367. const lastPage = this.state[this.view].records.last_page;
  368. const page = query.page > lastPage ? lastPage : query.page;
  369. this.state[this.view].records.current_page = page || 1;
  370. this.state[this.view].records.per_page = query.per_page || 10;
  371. this.state[this.view].searchTerm = query.search || '';
  372. const sort = query.sort || 'first_name';
  373. this.$route.query.sort = sort;
  374. if (sort) {
  375. const isAsc = sort.startsWith('-');
  376. const sortTerm = isAsc ? sort.substr(1) : sort;
  377. const sortKey = Object.keys(this.sortableAlias).filter(key => this.sortableAlias[key] === sortTerm);
  378. Object.keys(this.state[this.view].tableColumns).forEach((column) => {
  379. this.state[this.view].tableColumns[column].sort.asc = null;
  380. });
  381. if (sortKey && this.state[this.view].tableColumns[sortKey]) {
  382. this.state[this.view].tableColumns[sortKey].sort.asc = !isAsc;
  383. } else {
  384. this.state[this.view].tableColumns[sortTerm].sort.asc = !isAsc;
  385. }
  386. }
  387. },
  388. mounted() {
  389. this.$options.menus.clientsMenu = document.getElementById(clientsMenuId);
  390. this.$options.menus.enquiriesMenu = document.getElementById(enquiriesMenuId);
  391. this.makeMenuActive();
  392. this.componentId = `clientList_${this.uniqueKey()}`;
  393. this.handlers = new Listener(this);
  394. this.urlQueryListenerAndBuilder();
  395. EventBus.$on('GLOBAL_MAIL_SENT', () => {
  396. this.state[this.view].bulk = {};
  397. });
  398. },
  399. };
  400. </script>
Add Comment
Please, Sign In to add comment