Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. export function loadJsonDataToLocalStorage(url, localStorageKey) {
  2. return $.ajax({
  3. type: 'Get',
  4. url,
  5. dataType: 'json',
  6. success: (data) => {
  7. localStorage.setItem(localStorageKey, JSON.stringify(data));
  8. },
  9. error: (e) => {
  10. console.log(e);
  11. },
  12. });
  13. }
  14.  
  15. export function getData(key) {
  16. const data = localStorage.getItem(key);
  17.  
  18. return data !== null ? JSON.parse(data) : null;
  19. }
  20.  
  21. function setData(key, data) {
  22. localStorage.setItem(key, JSON.stringify(data));
  23. }
  24.  
  25. function getNewId(data) {
  26. return data
  27. .map((item) => item.ID)
  28. .reduce((maxValue, currentValue) => Math.max(maxValue, currentValue), 0) + 1;
  29. }
  30.  
  31. function addNewClient() {
  32. const clients = getData('line-clients');
  33.  
  34. const maxQueueNumber = clients
  35. .map((client) => client.number)
  36. .reduce((maxValue, currentValue) => Math.max(maxValue, currentValue), 0) + 1;
  37.  
  38. const newId = getNewId(clients);
  39.  
  40. clients.push({ ID: newId, number: maxQueueNumber })
  41.  
  42. setData('line-clients', clients);
  43.  
  44. return newId;
  45. }
  46.  
  47. function addNewService(clientId, specialistId) {
  48. const service = getData('line-data');
  49. const newId = getNewId(service);
  50.  
  51. service.push({ ID: newId, client_id: clientId, specialist_id: specialistId })
  52.  
  53. setData('line-data', service);
  54.  
  55. return newId;
  56. }
  57.  
  58. export function getClientsBySpecialist(specialistId) {
  59. const serviceLine = getData('line-data');
  60.  
  61. if (serviceLine !== null) {
  62. const serviceEntries = serviceLine.filter(
  63. (service) => service.specialist_id === specialistId,
  64. );
  65.  
  66. if (serviceEntries.length > 0) {
  67. const clients = getData('line-clients');
  68.  
  69. if (clients !== null) {
  70. return clients
  71. .filter(
  72. (client) => serviceEntries.findIndex(
  73. (service) => service.client_id === client.ID,
  74. ) !== -1 && !client.serviced,
  75. );
  76. }
  77. }
  78. }
  79.  
  80. return [];
  81. }
  82.  
  83. export function addNewEntry(specialistId) {
  84. const specialists = getData('line-specialists');
  85. const specialistById = specialists.find(
  86. (specialist) => specialist.ID === specialistId,
  87. );
  88.  
  89. if (typeof specialistById !== 'undefined') {
  90. const newClientId = addNewClient();
  91.  
  92. addNewService(newClientId, specialistById.ID);
  93. }
  94. }
  95.  
  96. export function markAsServiced(clientId) {
  97. const clients = getData('line-clients');
  98. const clientListId = clients.findIndex(
  99. (client) => client.ID === clientId,
  100. );
  101.  
  102. if (clientListId !== -1) {
  103. clients[clientListId].serviced = true;
  104.  
  105. setData('line-clients', clients);
  106. }
  107. }
  108. // create service time object
  109. // insert how long it took for service (previous done === next one start till done, insert when started, update when done)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement