Skip to content

Commit

Permalink
Added file upload functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dpigera committed Jan 7, 2025
1 parent d239e0b commit 1489bbf
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
38 changes: 38 additions & 0 deletions app/controllers/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { inject as service } from '@ember/service';
export default class DashboardController extends Controller {
@service router;
@service session;
@service s3Upload;

@tracked isProfileOpen = false;
@tracked selectedChannelId = null;
Expand Down Expand Up @@ -189,4 +190,41 @@ export default class DashboardController extends Controller {
console.error('Error posting reply:', error);
}
}

@action
async handleFileUpload(event) {
const file = event.target.files[0];
if (!file) return;

try {
// Upload file to S3
const { url, fileName, fileEmoji } = await this.s3Upload.uploadFile(file);

// Create new message object
const newMessage = {
id: String(Date.now()),
content: `${fileEmoji} File uploaded: ${fileName}`,
fileInfo: {
name: fileName,
url: url
},
user: {
id: '3',
name: 'Devin Pigera',
avatar: 'DP'
},
timestamp: new Date().toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' }),
replyCount: 0,
reactionCount: 0
};

// Add message to messages list
this.messages = [...this.messages, newMessage];

// Clear the file input
event.target.value = '';
} catch (error) {
console.error('Error uploading file:', error);
}
}
}
17 changes: 17 additions & 0 deletions app/services/s3-upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Service from '@ember/service';

export default class S3UploadService extends Service {
async uploadFile(file) {
// Mock API call to S3
await new Promise(resolve => setTimeout(resolve, 1000)); // Simulate network delay

// Mock S3 URL response
const mockS3Url = `https://fake-s3-bucket.s3.amazonaws.com/${file.name}`;

return {
url: mockS3Url,
fileName: file.name,
fileEmoji: '📎' // Adding file emoji
};
}
}
10 changes: 9 additions & 1 deletion app/templates/dashboard.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<button
class="p-2 text-gray-500 hover:text-gray-700 hover:bg-gray-100 rounded-lg"
title="Upload files"
onclick="document.getElementById('file-upload').click()"
>
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12" />
Expand Down Expand Up @@ -296,4 +297,11 @@
</div>
{{/if}}
</div>
</div>
</div>

<input
type="file"
id="file-upload"
class="hidden"
{{on "change" this.handleFileUpload}}
>
8 changes: 8 additions & 0 deletions mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ function routes() {
return newReply;
});

// Mock file upload endpoint
this.post('/api/files/upload', (schema, request) => {
return {
url: 'https://fake-s3-bucket.s3.amazonaws.com/uploaded-file.pdf',
fileName: 'uploaded-file.pdf'
};
});

// Allow other endpoints to pass through
this.passthrough();
}

0 comments on commit 1489bbf

Please sign in to comment.