Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.64 KB | None | 0 0
  1. <template>
  2. <div id="app">
  3. <v-app id="inspire">
  4. <div>
  5. <v-toolbar flat color="white">
  6. <v-toolbar-title>Zajęcia indywidualne</v-toolbar-title>
  7. <v-divider
  8. class="mx-2"
  9. inset
  10. vertical
  11. ></v-divider>
  12. <v-spacer></v-spacer>
  13. <v-dialog v-model="dialog" max-width="500px">
  14. <template v-slot:activator="{ on }">
  15. <v-text-field
  16. v-model="search"
  17. append-icon="search"
  18. label="Search"
  19. single-line
  20. hide-details
  21. ></v-text-field>
  22. <v-btn color="orange" dark class="mb-2" v-on="on">Dodaj zajęcia indywidualne</v-btn>
  23. </template>
  24. <v-card>
  25. <v-card-title>
  26. <span class="headline">{{ formTitle }}</span>
  27. </v-card-title>
  28.  
  29. <v-card-text>
  30. <v-container grid-list-md>
  31. <v-layout wrap>
  32. <v-flex xs12 sm6 md4>
  33. <v-text-field v-model="editedItem.name" label="Nazwa"></v-text-field>
  34. </v-flex>
  35. <v-flex xs12 sm6 md4>
  36. <v-text-field v-model="editedItem.description" label="Opis"></v-text-field>
  37. </v-flex>
  38. <v-flex xs12 sm6 md4>
  39. <v-text-field v-model="editedItem.price" label="Cena"></v-text-field>
  40. </v-flex>
  41. <v-flex xs12 sm6 md4>
  42. <v-dialog
  43. ref="modal2"
  44. v-model="dateDialog"
  45. persistent
  46. lazy
  47. full-width
  48. width="290px"
  49. >
  50. <template v-slot:activator="{ on }">
  51. <v-text-field
  52. v-model="editedItem.date"
  53. label="Data"
  54. prepend-icon="event"
  55. readonly
  56. v-on="on"
  57. ></v-text-field>
  58. </template>
  59. <v-date-picker color="orange" locale="pl-pl" v-model="editedItem.date">
  60. <v-spacer></v-spacer>
  61. <v-btn flat color="orange" @click="dateDialog = false">Anuluj</v-btn>
  62. <v-btn flat color="orange" @click="$refs.modal2.save(editedItem.date)">Zapisz</v-btn>
  63. </v-date-picker>
  64. </v-dialog>
  65. </v-flex>
  66. <v-flex xs12 sm6 md4>
  67. <v-dialog
  68. ref="dialog"
  69.  
  70. v-model="timeDialog"
  71. persistent
  72. lazy
  73. full-width
  74. width="290px"
  75. >
  76. <template v-slot:activator="{ on }">
  77. <v-text-field
  78. v-model="editedItem.startingTime"
  79. label="Godzina rozpoczecia"
  80. prepend-icon="event"
  81. readonly
  82. v-on="on"
  83. ></v-text-field>
  84. </template>
  85. <v-time-picker color="orange" v-model="editedItem.startingTime" >
  86. <v-spacer></v-spacer>
  87. <v-btn flat color="orange" @click="timeDialog = !timeDialog">Cancel</v-btn>
  88. <v-btn flat color="orange" @click="$refs.dialog.save(editedItem.startingTime)">OK</v-btn>
  89. </v-time-picker>
  90. </v-dialog>
  91. </v-flex>
  92. <v-flex xs12 sm6 md4>
  93. <v-text-field v-model="editedItem.durationInMinutes" label="Czas trwania"></v-text-field>
  94. </v-flex>
  95.  
  96. <v-flex xs12 sm6 md4>
  97. <v-select v-model="editedItem.intensity"
  98. :items="selectintensity"
  99. label="Intensywnosc"
  100. :placeholder="editedItem.intensity"
  101. ></v-select>
  102. </v-flex>
  103. <v-flex xs12 sm6 md4>
  104. <v-select v-model="editedItem.trainerId"
  105. :items="selectTrainer"
  106. label="Trener"
  107. :placeholder="trainerIdToFullname(editedItem.trainerId)"
  108. ></v-select>
  109. </v-flex>
  110. <v-flex xs12 sm6 md4>
  111. <v-select v-model="editedItem.clientId"
  112. :items="selectClient"
  113. label="Klient"
  114. :placeholder="clientIdToFullname(editedItem.clientId)"
  115. ></v-select>
  116. </v-flex>
  117. </v-layout>
  118. </v-container>
  119. </v-card-text>
  120.  
  121. <v-card-actions>
  122. <v-spacer></v-spacer>
  123. <v-btn color="orange" flat @click="close">Cancel</v-btn>
  124. <v-btn color="orange" flat @click="save">Save</v-btn>
  125. </v-card-actions>
  126. </v-card>
  127. </v-dialog>
  128. </v-toolbar>
  129. <v-data-table
  130. :headers="headers"
  131. :search="search"
  132. :items="individualTrainings"
  133. class="elevation-1"
  134. >
  135. <template v-slot:items="props" >
  136. <td @click="editItem(props.item)" class="text-xs-left">{{ props.item.name }}</td>
  137. <td @click="editItem(props.item)"class="text-xs-left">{{ props.item.description }}</td>
  138. <td @click="editItem(props.item)"class="text-xs-left">{{ props.item.price }}</td>
  139. <td @click="editItem(props.item)"class="text-xs-left">{{ toDate(props.item.date) }}</td>
  140. <td @click="editItem(props.item)"class="text-xs-left">{{ props.item.startingTime[0]+':'+props.item.startingTime[1] }}</td>
  141. <td @click="editItem(props.item)"class="text-xs-left">{{ props.item.durationInMinutes }}</td>
  142. <td @click="editItem(props.item)"class="text-xs-left">{{ props.item.intensity }}</td>
  143. <td @click="editItem(props.item)"class="text-xs-left">{{ trainerIdToFullname(props.item.trainerId) }}</td>
  144. <td @click="editItem(props.item)"class="text-xs-left">{{ clientIdToFullname(props.item.client.id) }}</td>
  145. <td class="justify-center layout px-0">
  146. <v-icon
  147. small
  148. @click="deleteItem(props.item)"
  149. >
  150. delete
  151. </v-icon>
  152. </td>
  153. </template>
  154. <template v-slot:no-data>
  155. <v-btn color="orange" @click="initialize">Reset</v-btn>
  156. </template>
  157. </v-data-table>
  158. </div>
  159. </v-app>
  160. <v-snackbar
  161. v-model="notification"
  162. top
  163. :color="type"
  164. :timeout="timeout"
  165. >
  166. {{ snackbarText }}
  167. </v-snackbar>
  168. </div>
  169. </template>
  170.  
  171. <script>
  172. import axios from 'axios'
  173. import {mapMutations, mapState} from 'vuex'
  174. import {BASE_URL} from "../../constants";
  175.  
  176.  
  177. export default {
  178. data: () => ({
  179. dialog: false,
  180. dateDialog: false,
  181. timeDialog: false,
  182. authCookie: '',
  183. search: '',
  184. selectTrainer:[],
  185. selectClient:[],
  186. checkbox: false,
  187. date: new Date().toISOString().substr(0, 10),
  188. modal: false,
  189. modal2: false,
  190. buttonColor: "WHITE",
  191. snackbarText: "Wystąpił błąd.",
  192. buttonText: "OK",
  193. hasButton: true,
  194. notification: false,
  195. timeout: 4000,
  196. type: null,
  197. selectboolean: ['true','false'],
  198. selectgender: ['MAN','WOMAN'],
  199. selectintensity: [ '1','2','3','4','5'],
  200. headers: [
  201. { text: 'Nazwa', value: 'name' },
  202. { text: 'Opis', value: 'description' },
  203. { text: 'Cena', value: 'price' },
  204. { text: 'Data', value: 'date' },
  205. { text: 'Godzina rozpoczęcia', value: 'startingTime' },
  206. { text: 'Czas trwania', value: 'durationInMinutes' },
  207. { text: 'Intensywność', value: 'intensity' },
  208. { text: 'Trener', value: 'trainerId' },
  209. { text: 'Klient', value: 'client' },
  210. { text: 'Akcje', value: 'actions' }
  211. ],
  212. individualTrainings: [],
  213. trainers:[
  214. { id:''},
  215. { name: '' },
  216. { surname: ''}
  217. ],
  218. clients:[
  219. {id:''},
  220. { name: ''},
  221. { surname: ''}
  222. ],
  223. editedIndex: -1,
  224. editedItem: {
  225. id: '',
  226. name: '',
  227. description: '',
  228. price: '',
  229. date: '',
  230. startingTime: '',
  231. durationInMinutes: '',
  232. intensity: '',
  233. createdBy: '',
  234. creationDate: '',
  235. removed: '',
  236. trainerId: '',
  237. clientId: ''
  238. },
  239. defaultItem: {
  240. id: '',
  241. name: '',
  242. description: '',
  243. price: '',
  244. date: '',
  245. startingTime: '',
  246. durationInMinutes: '',
  247. intensity: '',
  248. createdBy: '',
  249. creationDate: '',
  250. removed: '',
  251. trainerId: '',
  252. clientId: ''
  253. },
  254.  
  255. test: '',
  256.  
  257. dict: {
  258. "MAN": {
  259. pl: "Mężczyzna"
  260. }
  261. }
  262.  
  263. }),
  264.  
  265. computed: {
  266. ...mapState(['token']),
  267. formTitle () {
  268. return this.editedIndex === -1 ? 'Dodaj grupowy trening' : 'Edytuj grupowy trening'
  269.  
  270. },
  271. actionType()
  272. {
  273. return this.editedIndex !== -1
  274. }
  275. },
  276.  
  277. watch: {
  278. dialog (val) {
  279. val || this.close()
  280. }
  281. },
  282.  
  283. created () {
  284. var cookies = document.cookie
  285. var tempCookies = cookies.split(';')
  286. for(var x=0; x< tempCookies.length; x++)
  287. {
  288. var cookie = tempCookies[x].split('=')
  289. console.log(cookie[0].trim(' '))
  290. if(cookie[0].trim(' ') === 'access_token_worker'){
  291. this.authCookie = cookie[1];
  292.  
  293. }}
  294. console.log(this.authCookie)
  295. if(this.authCookie==='')
  296. window.location.href = ''
  297. else
  298. this.initialize()
  299. },
  300.  
  301. methods: {
  302. async initialize() {
  303.  
  304.  
  305. axios.get(BASE_URL + '/api/trainings/individual', {headers: {Authorization: this.authCookie}}).then(Response => {
  306. var data = Object.values(Response.data)
  307. this.individualTrainings = data;
  308. console.log(this.individualTrainings)
  309. });
  310.  
  311. await axios.get(BASE_URL + '/api/trainers/all', {headers: {Authorization: this.authCookie}}).then(Response => {
  312. var data = Object.values(Response.data)
  313. this.trainers = data;
  314.  
  315. });
  316.  
  317. await axios.get(BASE_URL + '/api/clients', {headers: {Authorization: this.authCookie}}).then(Response => {
  318. var data = Object.values(Response.data)
  319. this.clients = data;
  320.  
  321. });
  322.  
  323.  
  324. this.setTrainersForPick();
  325. this.setClientsForPick();
  326. console.log(this.clients)
  327. },
  328.  
  329. editItem(item) {
  330. this.editedIndex = this.individualTrainings.indexOf(item)
  331. this.editedItem = Object.assign({}, item)
  332. this.editedItem.date = this.toDate(this.editedItem.date)
  333. this.dialog = true
  334. },
  335.  
  336. deleteItem(item) {
  337. const index = this.individualTrainings.indexOf(item)
  338. if(confirm('Czy na pewno chcesz usunąć?') && this.individualTrainings.splice(index, 1)) {
  339. axios.delete(BASE_URL + '/api/trainings/individual/remove/' + item.id, {headers: {Authorization: this.authCookie}}).then(Response => {
  340. var data = Object.values(Response.data)
  341. }).catch(error => {
  342. this.notification=true;
  343. this.snackbarText='Wystąpił błąd podczas usuwania zajęć'
  344. this.type='red'
  345. });
  346. }
  347. },
  348.  
  349. close() {
  350. this.dialog = false
  351. setTimeout(() => {
  352. this.editedItem = Object.assign({}, this.defaultItem)
  353. this.editedIndex = -1
  354. }, 300)
  355. },
  356.  
  357. async save() {
  358. if (this.editedIndex > -1) {
  359. Object.assign(this.individualTrainings[this.editedIndex], this.editedItem)
  360. this.editedItem.trainerId = this.getTrainerIdByFullname(this.editedItem.trainerId)
  361. this.editedItem.clientId = this.getClientIdByFullname(this.editedItem.clientId)
  362. axios.patch(BASE_URL + '/api/trainings/individual', this.editedItem, {headers: {Authorization: this.authCookie}}).then(Response => {
  363. var data = Object.values(Response.data)
  364. }).catch(error => {
  365. this.notification=true;
  366. this.snackbarText='Wystąpił błąd podczas aktualizacji zajęć.'
  367. this.type='red'
  368. });
  369. } else {
  370.  
  371. this.editedItem.trainerId = this.getTrainerIdByFullname(this.editedItem.trainerId)
  372. this.editedItem.clientId = this.getClientIdByFullname(this.editedItem.clientId)
  373. axios.post(BASE_URL + '/api/trainings/individual', this.editedItem, {headers: {Authorization: this.authCookie}}).then(Response => {
  374. var data = Object.values(Response.data)
  375. this.initialize()
  376.  
  377. }).catch(error => {
  378. this.notification=true;
  379. this.snackbarText='Wystąpił błąd podczas tworzenia zajęć.'
  380. this.type='red'
  381. });
  382.  
  383. }
  384. this.close()
  385. },
  386.  
  387. toDate(timeStamp) {
  388. var currentDate = new Date(timeStamp.toString());
  389. console.log(currentDate)
  390. var date = currentDate.getDate();
  391. var month = currentDate.getMonth(); //Be careful! January is 0 not 1
  392. var year = currentDate.getFullYear();
  393.  
  394. var dateString = year + "-" + (month + 1) + "-" + date;
  395. return dateString;
  396. },
  397.  
  398. toTimeStamp(stringDate) {
  399. var date = new Date(stringDate)
  400. return date.getTime() / 1000
  401.  
  402. },
  403.  
  404. trainerIdToFullname(id) {
  405. for (var i = 0; i < this.trainers.length; i++) {
  406. if (id === this.trainers[i].id) {
  407. return this.trainers[i].name + ' ' + this.trainers[i].surname
  408. }
  409.  
  410. }
  411. },
  412.  
  413. clientIdToFullname(id) {
  414. for (var i = 0; i < this.clients.length; i++) {
  415. if (id === this.clients[i].id) {
  416. return this.clients[i].name + ' ' + this.clients[i].surname
  417. }
  418.  
  419. }
  420. },
  421.  
  422. getClientIdByFullname(fullname) {
  423. for (var i = 0; i < this.clients.length; i++) {
  424. if (fullname === this.clients[i].name + ' ' + this.clients[i].surname) {
  425. return this.clients[i].id
  426. }
  427. }
  428. },
  429.  
  430. setClientsForPick() {
  431. for (var i = 0; i < this.clients.length; i++) {
  432. this.selectClient.push(this.clients[i].name + ' ' + this.clients[i].surname)
  433. console.log(this.selectClient)
  434. }
  435. },
  436.  
  437. setTrainersForPick() {
  438. for (var i = 0; i < this.trainers.length; i++) {
  439. this.selectTrainer.push(this.trainers[i].name + ' ' + this.trainers[i].surname)
  440. }
  441. },
  442.  
  443. getTrainerIdByFullname(fullname) {
  444. for (var i = 0; i < this.trainers.length; i++) {
  445. if (fullname === this.trainers[i].name + ' ' + this.trainers[i].surname) {
  446. return this.trainers[i].id
  447. }
  448. }
  449.  
  450. }
  451. }
  452. }
  453. </script>
  454.  
  455. <style>
  456. #app {
  457. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  458. -webkit-font-smoothing: antialiased;
  459. -moz-osx-font-smoothing: grayscale;
  460. text-align: center;
  461. color: #2c3e50;
  462. margin-top: 60px;
  463. }
  464. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement