Skip to content

Commit

Permalink
Add file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
surajp committed Apr 22, 2020
1 parent 91d4b6f commit 2eff2c0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
2 changes: 1 addition & 1 deletion config/project-scratch-def.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"orgName": "Demo company",
"edition": "Developer",
"features": [],
"features": ["AuthorApex"],
"settings":{
"lightningExperienceSettings":{
"enableS1DesktopEnabled": true
Expand Down
19 changes: 13 additions & 6 deletions force-app/main/default/classes/GetFilesController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public with sharing class GetFilesController {
public static FileVersionWrapper[] getFileVersionDetails(Id fileId) {
/*{{{*/
FileVersionWrapper[] contentversions = new List<FileVersionWrapper>{};
for (ContentVersion cv : [SELECT title, createddate, createdby.name FROM ContentVersion WHERE ContentDocumentId = :fileId]) {
contentVersions.add(new FileVersionWrapper(cv.Id, cv.title, cv.createdby.name, Date.valueOf(cv.createddate)));
for (ContentVersion cv : [SELECT title, createddate, createdby.name, VersionReason FROM ContentVersion WHERE ContentDocumentId = :fileId]) {
contentVersions.add(new FileVersionWrapper(cv.Id, cv.title, cv.createdby.name, Date.valueOf(cv.createddate), cv.VersionReason));
}
return contentVersions;
} /*}}}*/
Expand All @@ -68,11 +68,12 @@ public with sharing class GetFilesController {
}

public FilesWrapper() {
/*{{{*/
this.title = '';
this.createdBy = '';
this.createdDate = null;
this.id = '';
}
} /*}}}*/

public FilesWrapper(String id, String title, String createdBy, Date createdDate) {
this.id = id;
Expand All @@ -92,6 +93,8 @@ public with sharing class GetFilesController {
@AuraEnabled
public Date createdDate { get; set; }
@AuraEnabled
public String versionReason { get; set; }
@AuraEnabled
public String url {
get {
return '/' + this.id;
Expand All @@ -100,17 +103,21 @@ public with sharing class GetFilesController {
}

public FileVersionWrapper() {
/*{{{*/
this.title = '';
this.createdBy = '';
this.createdDate = null;
this.id = '';
}
this.versionReason = '';
} /*}}}*/

public FileVersionWrapper(String id, String title, String createdBy, Date createdDate) {
public FileVersionWrapper(String id, String title, String createdBy, Date createdDate, String versionReason) {
/*{{{*/
this.id = id;
this.title = title;
this.createdBy = createdBy;
this.createdDate = createdDate;
}
this.versionReason = versionReason;
} /*}}}*/
} /*}}}*/
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<template>
<lightning-button label="New File" onclick={newFileUpload}></lightning-button>
<lightning-datatable columns={columns} key-field="id" data={files} onrowaction={handleRowAction}></lightning-datatable>
<template if:true={showModal}>
<section class="slds-modal slds-fade-in-open">
Expand All @@ -9,7 +10,13 @@
</div>
</header>
<div class="slds-modal__content">
<lightning-datatable key-field="id" data={versionDetails} columns={versionColumns}></lightning-datatable>
<template if:false={fileUpload}>
<lightning-datatable key-field="id" data={versionDetails} columns={versionColumns}></lightning-datatable>
</template>
<template if:true={fileUpload}>
<input type="file" name="file" />
<lightning-button onclick={handleUpload} label="Submit"></lightning-button>
</template>
</div>
</div>
</section>
Expand Down
64 changes: 59 additions & 5 deletions force-app/main/default/lwc/filesRelatedList/filesRelatedList.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { LightningElement, wire, api } from "lwc";
import getRelatedFiles from "@salesforce/apex/GetFilesController.getFilesList";
import getFileVersionDetails from "@salesforce/apex/GetFilesController.getFileVersionDetails";
import { createRecord } from "lightning/uiRecordApi";
import { ShowToastEvent } from "lightning/platformShowToastEvent";

const actions = [{ label: "Version Details", name: "show_details" }];
const actions = [
{ label: "Version Details", name: "show_details" },
{ label: "Upload", name: "upload_version" }
];

const columns = [
{
Expand All @@ -23,6 +28,7 @@ const versionColumns = [
type: "url",
typeAttributes: { label: "Download" }
},
{ label: "Version Reason", fieldName: "versionReason", type: "string" },
{ label: "Uploaded Date", fieldName: "createdDate", type: "date" },
{ label: "Uploaded by", fieldName: "createdBy", type: "string" }
];
Expand All @@ -36,6 +42,8 @@ export default class FilesRelatedList extends LightningElement {
columns = columns;
versionColumns = versionColumns;
versionDetails = [];
fileUpload = false;
_currentDocId = null;

@wire(getRelatedFiles, { recordId: "$recordId" })
getFilesList({ error, data }) {
Expand All @@ -47,18 +55,32 @@ export default class FilesRelatedList extends LightningElement {

closeModal() {
this.showModal = false;
this._currentDocId = null;
this.fileUpload = false;
this.versionDetails = [];
}

handleRowAction(event) {
console.log(">> handle row action " + event.detail.action.name);
const row = event.detail.row;
this.showVersionDetails(row.id);
const action = event.detail.action.name;
this._currentDocId = row.id;
if (action == "show_details") {
const row = event.detail.row;
this.fileUpload = false;
this.showVersionDetails();
} else if (action == "upload_version") {
this.fileUpload = true;
}
}

showVersionDetails(id) {
newFileUpload() {
this.showModal = true;
this.fileUpload = true;
}

showVersionDetails() {
console.log(">> file version details");
getFileVersionDetails({ fileId: id })
getFileVersionDetails({ fileId: this._currentDocId })
.then((result) => {
console.log(">> version details " + JSON.stringify(result));
this.versionDetails = result;
Expand All @@ -68,4 +90,36 @@ export default class FilesRelatedList extends LightningElement {
console.error(JSON.stringify(err));
});
}

handleUpload(event) {
event.preventDefault();
const file = this.template.querySelector("input.file").files[0];
const reader = new FileReader();
let fileData = "";
reader.onload = () => {
fileData = reader.result;
this._uploadFile(file, fileData);
};
reader.readAsDataURL(file);
}

_uploadFile(file, fileData) {
const payload = {
Title: file.name,
PathOnClient: file.name,
VersionData: fileData.replace(BASE64EXP, "")
};
if (this._currentDocId) {
payload.ContentDocumentId = this._currentDocId;
}
createRecord(payload).then(() => {
this.closeModal();
this.dispatchEvent(
new ShowToastEvent({
variant: "success",
message: `Content Document Link created`
})
);
});
}
}

0 comments on commit 2eff2c0

Please sign in to comment.