Consolidated the video and channel attribute edits into one javascript function

This commit is contained in:
Gregory Ballantine 2023-03-07 11:29:10 -05:00
parent 8905bed454
commit a9c4b29935
5 changed files with 39 additions and 41 deletions

View File

@ -1,41 +1,31 @@
$(document).ready(() ->
$('.channel-path').click((e) ->
newPath = prompt('New channel directory path', $(this).find('span').text())
if newPath
payload =
directory_path: newPath
channelId = $('#channel-id').val()
$.post('/channel/' + channelId + '/edit/directory_path', payload, (data) ->
if data == 'success'
$('.channel-path span').text(newPath)
)
)
$('.channel-path').click(updateAttribute)
$('.video-status').click((e) ->
newStatus = prompt('New video status', $(this).find('span').text())
if newStatus
newStatus = newStatus.toLowerCase()
payload =
status: newStatus
videoId = $('#video-id').val()
$.post('/video/' + videoId + '/edit/status', payload, (data) ->
if data == 'success'
$('.video-status span').text(capitalizeWord(newStatus))
)
)
$('.video-status').click(updateAttribute)
$('.video-serial').click((e) ->
newSerial = prompt('New video serial number', $(this).find('span').text())
if newSerial
payload =
serial: newSerial
videoId = $('#video-id').val()
$.post('/video/' + videoId + '/edit/serial', payload, (data) ->
if data == 'success'
$('.video-serial span').text(newSerial)
)
)
$('.video-serial').click(updateAttribute)
)
updateAttribute = (e) ->
elem = $(this)
type = elem.data('type')
attr = elem.data('attribute')
newValue = prompt('Enter new ' + type + ' ' + attr + ':', elem.find('span').text())
if newValue
if attr == 'status'
newValue = newValue.toLowerCase()
payload =
value: newValue
objectId = $('#' + type + '-id').val()
url = '/' + type + '/' + objectId + '/edit/' + attr
$.post(url, payload, (data) ->
if data == 'success'
if attr == 'status'
newValue = capitalizeWord(newValue)
elem.find('span').text(newValue)
)
else
console.log('User canceled attribute "' + attr + '" update.')
capitalizeWord = (str) ->
return str.charAt(0).toUpperCase() + str.slice(1)

View File

@ -79,14 +79,14 @@ namespace '/channel' do
attrToEdit = params[:attr]
if attrToEdit == 'directory_path'
File.rename(channel.directory_path, params[attrToEdit])
File.rename(channel.directory_path, params[:value])
channel.videos.each do |v|
video_path = v.directory_path.sub(channel.directory_path, params[attrToEdit])
video_path = v.directory_path.sub(channel.directory_path, params[:value])
v.update(directory_path: video_path)
end
end
channel[attrToEdit.to_sym] = params[attrToEdit]
channel[attrToEdit.to_sym] = params[:value]
channel.save()
return "success"

View File

@ -111,7 +111,15 @@ namespace '/video' do
video = Video.where(id: params[:video_id]).first()
attrToEdit = params[:attr]
video[attrToEdit.to_sym] = params[attrToEdit]
# if we update the video's serial, we need to also update the directory path
if attrToEdit == 'serial'
old_path = video.directory_path
new_path = video.directory_path.sub("##{video.serial}", "##{params[:value]}")
File.rename(old_path, new_path)
video[:directory_path] = new_path
end
video[attrToEdit.to_sym] = params[:value]
video.save()
return "success"

View File

@ -16,7 +16,7 @@
<span><a href="/channel/<%= channel.id %>/edit">Edit <i class="fa-solid fa-pen-to-square"></i></a></span><span>
<a href="/channel/<%= channel.id %>/delete">Delete <i class="fa-solid fa-trash"></i></a></span>
</div>
<div class="channel-path">
<div class="channel-path" data-attribute="directory_path" data-type="channel">
<p><span><%= channel.directory_path %></span></p>
</div>
<div class="channel-open">

View File

@ -18,10 +18,10 @@
<div class="video-channel">
<p>Channel: <a href="/channel/<%= video.channel.id %>"><span><%= video.channel.name %></span></a></p>
</div>
<div class="video-serial">
<div class="video-serial" data-attribute="serial" data-type="video">
<p>Serial: <span><%= serialize(video.serial) %></span></p>
</div>
<div class="video-status">
<div class="video-status" data-attribute="status" data-type="video">
<p>Status: <span><%= video.status.capitalize() %></span></p>
</div>
<div class="script-controls">