-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AP-2910: Save attachment categorisation on select
Categorisation would get lost if any already uploaded files were deleted or a new file uploaded. This was due to the page being refreshed by the javascript when a new file is uploaded or existing upload deleted. This solution implements an immediate update of the attachment's type/category when a categorisation is selected from the select-list.
- Loading branch information
Showing
6 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module V1 | ||
class AttachmentsController < ApiController | ||
def update | ||
return head :not_found unless attachment | ||
|
||
attachment.update!(attachment_type: form_params[:attachment_type]) | ||
head :ok | ||
end | ||
|
||
private | ||
|
||
def attachment | ||
@attachment ||= Attachment.find_by(id: form_params[:attachment_id]) | ||
end | ||
|
||
def form_params | ||
params.permit(%i[attachment_id attachment_type]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import axios from 'axios' | ||
|
||
async function updateAttachment(attachmentId, attachmentType) { | ||
let xhr = new XMLHttpRequest(); | ||
|
||
let url = `/v1/attachments/${attachmentId}` | ||
// console.log('sending to ' + url) | ||
|
||
const response = await axios({ | ||
accept: 'application/json', | ||
responseType: 'json', | ||
method: 'patch', | ||
url, | ||
data: { | ||
attachment_id: attachmentId, | ||
attachment_type: attachmentType | ||
} | ||
}) | ||
|
||
return response.data.data | ||
} | ||
|
||
export function initFileUploadCategorisation (categorisationSelectLists) { | ||
console.log("found " + categorisationSelectLists.length + " uploaded file select lists") | ||
|
||
categorisationSelectLists.forEach((select) => { | ||
select.addEventListener('change', (e) => { | ||
/* | ||
extract attachment id and name/type from select list | ||
*/ | ||
let attachmentId = e.target.name.match(/\[(.*)\]/)[1] | ||
let attachmentType = e.target.value | ||
|
||
updateAttachment(attachmentId, attachmentType) | ||
}) | ||
}) | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', event => { | ||
const categorisationSelectLists = document.querySelectorAll('select[id^="uploaded-evidence-collection-"]') | ||
|
||
if (categorisationSelectLists.length) { | ||
initFileUploadCategorisation(categorisationSelectLists) | ||
} | ||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters