vol1ura/Sat_9am_5km

View on GitHub
app/views/admin/activities/_protocol_validation.html.erb

Summary

Maintainability
Test Coverage
<script>
  function parseTimeString(timeStr) {
    const timeParts = timeStr.split(':')
    let seconds = 0

    if (timeParts.length === 2) {
        // Format 'MM:SS'
        seconds += parseInt(timeParts[0]) * 60 // Minutes
        seconds += parseInt(timeParts[1]) // Seconds
    } else if (timeParts.length === 3) {
        // Format 'HH:MM:SS'
        seconds += parseInt(timeParts[0]) * 3600 // Hours
        seconds += parseInt(timeParts[1]) * 60 // Minutes
        seconds += parseInt(timeParts[0]) // Seconds
    }

    return seconds
  }

  function validateProtocol() {
    const rows = $('tr[id*="result_"]')
    Array.prototype.forEach.call(rows, (row, idx) => {
      const position_cell = $(row).find('td.col-position')
      if(parseInt(position_cell.html()) === idx + 1) {
        position_cell.removeClass('error')
      } else {
        position_cell.addClass('error')
      }

      const next_row = rows[idx+1]
      if(!next_row) { return }

      const current_total_time = $(row).find('td.col-total_time').html()
      const next_total_time_cell = $(next_row).find('td.col-total_time')

      if(parseTimeString(current_total_time) <= parseTimeString(next_total_time_cell.html())) {
        next_total_time_cell.removeClass('error')
      } else {
        next_total_time_cell.addClass('error')
      }
    })
  }

  $(window).on('load', validateProtocol)
</script>