StateFilters.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <div>
  3. <form v-if="!readonly" v-on:submit.prevent class="form-inline">
  4. <div class="form-group">
  5. <span class="glyphicon glyphicon-chevron-right"></span>
  6. <select
  7. @change="changeTeam($event)"
  8. class="form-control"
  9. v-model="filters.teamId"
  10. >
  11. <option value="">Choose Team</option>
  12. <option
  13. v-for="current in teams"
  14. v-bind:key="current.id"
  15. :value="current.id"
  16. >
  17. {{ current.name }}</option
  18. >
  19. </select>
  20. </div>
  21. <div
  22. v-if="filters.teamId && users"
  23. v-on:submit.prevent
  24. class="form-group"
  25. >
  26. <span class="glyphicon glyphicon-chevron-right"></span>
  27. <select
  28. class="form-control"
  29. v-model="filters.userId"
  30. @change="changeUser($event)"
  31. >
  32. <option
  33. v-for="current in users"
  34. v-bind:key="current.id"
  35. v-bind:value="current.id"
  36. >
  37. {{ current.name }}</option
  38. >
  39. </select>
  40. </div>
  41. <div class="form-group">
  42. <span class="glyphicon glyphicon-chevron-right"></span>
  43. <span>{{ filters.date ? getDateUsc(filters.date) : "Today" }}</span
  44. >&nbsp;
  45. <input
  46. type="date"
  47. placeholder="'mm/dd/yyyy'"
  48. v-model="filters.date"
  49. @change="changeDate($event)"
  50. class="form-control"
  51. />
  52. </div>
  53. <div class="form-group" v-show="allowDevmode">
  54. <span class="glyphicon glyphicon-chevron-right"></span>
  55. <button
  56. type="button"
  57. class="btn"
  58. :class="{ 'btn-danger': !devmode, 'btn-success': devmode }"
  59. v-on:click="toggleDevMode()"
  60. >
  61. <span class="glyphicon glyphicon-alert"></span>
  62. {{ devmode ? "Disable" : "Enable" }} Dev Mode
  63. </button>
  64. </div>
  65. <div class="form-group">
  66. <span class="glyphicon glyphicon-chevron-right"></span>
  67. <button class="btn btn-primary" v-on:click="refresh()">
  68. <span class="glyphicon glyphicon-refresh"></span>
  69. </button>
  70. </div>
  71. </form>
  72. </div>
  73. </template>
  74. <script>
  75. import { mapMutations, mapGetters } from "vuex";
  76. export default {
  77. name: "StateFilters",
  78. components: {},
  79. methods: {
  80. ...mapMutations([
  81. "updateApi",
  82. "updateUsers",
  83. "updateCurrentUser",
  84. "updateFilters",
  85. "updateRefresh",
  86. "toggleDevMode"
  87. ]),
  88. changeTeam(event) {
  89. //location.href = `./?teamId=${event.target.value}`
  90. this.runtimevals.filters.teamId = event.target.value;
  91. // TODO: STATIC-STATE.
  92. // this.getFilters(this.stateQueryString)
  93. this.runtimevals.users = "";
  94. this.getUsers();
  95. },
  96. changeUser(event) {
  97. //location.href = `./?userId=${event.target.value}`
  98. this.runtimevals.filters.userId = event.target.value;
  99. // TODO: STATIC-STATE. This state won't work with the test
  100. // APIs because they're static. We can turn them back on
  101. // later or migrate all state to the client.
  102. // this.getFilters(this.stateQueryString)
  103. },
  104. changeDate(event) {
  105. //location.href = `./?pickDay=${event.target.value}`
  106. this.runtimevals.filters.date = event.target.value;
  107. // TODO: STATIC-STATE.
  108. // this.getFilters(this.stateQueryString)
  109. },
  110. getDateUsc(val) {
  111. //NOTE: To ensure the locale and timezone match, can
  112. //alternately set the timeZone in the options to UTC.
  113. return new Date(`${val}T00:00:00`).toLocaleDateString("en-US", {
  114. weekday: "long",
  115. year: "numeric",
  116. month: "long",
  117. day: "numeric"
  118. });
  119. },
  120. getData(endpoint, that, bucket, refs, errorPrompt) {
  121. if (endpoint) {
  122. fetch(endpoint)
  123. .then(response => response.json())
  124. .then(json => {
  125. if (json) {
  126. that[bucket][refs] = json;
  127. } else {
  128. that[errorPrompt] = "Response failed.";
  129. }
  130. })
  131. .catch(error => {
  132. that[errorPrompt] = `Request failed. ${error}`;
  133. });
  134. } else {
  135. that[errorPrompt] = "No configuration foo.";
  136. }
  137. },
  138. getFilters(qstrings) {
  139. this.getData(
  140. this.api.filters + qstrings,
  141. this,
  142. "runtimevals",
  143. "filters",
  144. "errorPrompt"
  145. );
  146. },
  147. getUsers() {
  148. this.getData(this.api.users, this, "runtimevals", "users", "errorPrompt");
  149. },
  150. refresh() {
  151. this.updateRefresh(new Date());
  152. },
  153. prepend(value, array) {
  154. var newArray = array.slice();
  155. newArray.unshift(value);
  156. return newArray;
  157. }
  158. },
  159. mounted() {
  160. this.updateApi(
  161. JSON.parse(document.getElementById("frontEndWiring").textContent).api
  162. );
  163. //NOTE: StateFilters will eventually handle filters client
  164. //side and not require a request to the server.
  165. this.getFilters(""); //this.getData(this.api.filters, this, "runtimevals", "filters", "errorPrompt")
  166. this.getData(
  167. this.api.currentUser,
  168. this,
  169. "runtimevals",
  170. "currentUser",
  171. "errorPrompt"
  172. );
  173. this.getUsers();
  174. },
  175. computed: {
  176. ...mapGetters(["api", "devmode", "readonly"]),
  177. filters: function() {
  178. return this.runtimevals.filters;
  179. },
  180. users: function() {
  181. return typeof this.runtimevals.users === "object"
  182. ? this.prepend(
  183. { id: "-1", name: "Users (All)" },
  184. this.runtimevals.users
  185. )
  186. : this.runtimevals.users;
  187. },
  188. currentUser: function() {
  189. return this.runtimevals.currentUser;
  190. },
  191. stateQueryString: function() {
  192. return `?teamId=${this.runtimevals.filters.teamId}&pickOwner=${this.runtimevals.filters.userId}&pickDay=${this.runtimevals.filters.date}`;
  193. },
  194. teams: function() {
  195. return this.currentUser.teams;
  196. }
  197. },
  198. data() {
  199. return {
  200. allowDevmode: false,
  201. errorPrompt: "",
  202. runtimevals: {
  203. filters: "",
  204. currentUser: "",
  205. users: ""
  206. }
  207. };
  208. },
  209. watch: {
  210. filters() {
  211. this.updateFilters(this.filters);
  212. },
  213. users() {
  214. this.updateUsers(this.users);
  215. },
  216. currentUser() {
  217. this.updateCurrentUser(this.currentUser);
  218. }
  219. }
  220. };
  221. </script>