Advertisement
Guest User

datemoment

a guest
Jan 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     import moment from 'moment'
  2.     export default {
  3.         data() {
  4.             return {
  5.                 projects: null,
  6.                 newTimerName: '',
  7.                 newProjectName: '',
  8.                 activeTimerString: 'Calculating...',
  9.                 counter: { seconds: 0, timer: null },
  10.             }
  11.         },
  12.         created() {
  13.             window.axios.get('/projects').then(response => {
  14.                 this.projects = response.data
  15.                 window.axios.get('/project/timers/active').then(response => {
  16.                     if (response.data.id !== undefined) {
  17.                         this.startTimer(response.data.project, response.data)
  18.                     }
  19.                 })
  20.             })
  21.         },
  22.         methods: {
  23.             /**
  24.              * Conditionally pads a number with "0"
  25.              */
  26.             _padNumber: number =>  (number > 9 || number === 0) ? number : "0" + number,
  27.  
  28.             /**
  29.              * Splits seconds into hours, minutes, and seconds.
  30.              */
  31.             _readableTimeFromSeconds: function(seconds) {
  32.                 const hours = 3600 > seconds ? 0 : parseInt(seconds / 3600, 10)
  33.                 return {
  34.                     hours: this._padNumber(hours),
  35.                     seconds: this._padNumber(seconds % 60),
  36.                     minutes: this._padNumber(parseInt(seconds / 60, 10) % 60),
  37.                 }
  38.             },
  39.  
  40.             /**
  41.              * Calculate the amount of time spent on the project using the timer object.
  42.              */
  43.             calculateTimeSpent: function (timer) {
  44.                 if (timer.stopped_at) {
  45.                     const started = moment(timer.started_at)
  46.                     const stopped = moment(timer.stopped_at)
  47.                     const time = this._readableTimeFromSeconds(
  48.                         parseInt(moment.duration(stopped.diff(started)).asSeconds())
  49.                     )
  50.                     return `${time.hours} Hours | ${time.minutes} mins | ${time.seconds} seconds`
  51.                 }
  52.                 return ''
  53.             },
  54.  
  55.             /**
  56.              * Determines if there is an active timer and whether it belongs to the project
  57.              * passed into the function.
  58.              */
  59.             showTimerForProject: function (project, timer) {
  60.                 return this.counter.timer &&
  61.                        this.counter.timer.id === timer.id &&
  62.                        this.counter.timer.project.id === project.id
  63.             },
  64.  
  65.             /**
  66.              * Start counting the timer. Tick tock.
  67.              */
  68.             startTimer: function (project, timer) {
  69.                 const started = moment(timer.started_at)
  70.  
  71.                 this.counter.timer = timer
  72.                 this.counter.timer.project = project
  73.                 this.counter.seconds = parseInt(moment.duration(moment().diff(started)).asSeconds())
  74.                 this.counter.ticker = setInterval(() => {
  75.                     const time = this._readableTimeFromSeconds(++vm.counter.seconds)
  76.  
  77.                     this.activeTimerString = `${time.hours} Hours | ${time.minutes}:${time.seconds}`
  78.                 }, 1000)
  79.             },
  80.  
  81.             /**
  82.              * Stop the timer from the API and then from the local counter.
  83.              */
  84.             stopTimer: function () {
  85.                 window.axios.post(`/projects/${this.counter.timer.id}/timers/stop`)
  86.                             .then(response => {
  87.                                 // Loop through the projects and get the right project...
  88.                                 this.projects.forEach(project => {
  89.                                     if (project.id === parseInt(this.counter.timer.project.id)) {
  90.                                         // Loop through the timers of the project and set the `stopped_at` time
  91.                                         return project.timers.forEach(timer => {
  92.                                             if (timer.id === parseInt(this.counter.timer.id)) {
  93.                                                 return timer.stopped_at = response.data.stopped_at
  94.                                             }
  95.                                         })
  96.                                     }
  97.                                 });
  98.  
  99.                                 // Stop the ticker
  100.                                 clearInterval(this.counter.ticker)
  101.  
  102.                                 // Reset the counter and timer string
  103.                                 this.counter = { seconds: 0, timer: null }
  104.                                 this.activeTimerString = 'Calculating...'
  105.                             })
  106.             },
  107.  
  108.             /**
  109.              * Create a new timer.
  110.              */
  111.             createTimer: function (project) {
  112.                 window.axios.post(`/projects/${project.id}/timers`, {name: this.newTimerName})
  113.                             .then(response => {
  114.                                 project.timers.push(response.data)
  115.                                 this.startTimer(response.data.project, response.data)
  116.                             })
  117.  
  118.                 this.newTimerName = ''
  119.             },
  120.  
  121.             /**
  122.              * Create a new project.
  123.              */
  124.             createProject: function () {
  125.                 window.axios.post('/projects', {name: this.newProjectName})
  126.                             .then(response => this.projects.push(response.data))
  127.  
  128.                 this.newProjectName = ''
  129.             }
  130.         },
  131.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement