Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem "active_model_serializers"
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
active_model_serializers (0.10.13)
actionpack (>= 4.1, < 7.1)
activemodel (>= 4.1, < 7.1)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
activejob (6.1.4.4)
activesupport (= 6.1.4.4)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -80,6 +85,8 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
case_transform (0.2)
activesupport
childprocess (4.1.0)
concurrent-ruby (1.1.9)
connection_pool (2.2.5)
Expand All @@ -98,6 +105,7 @@ GEM
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
jsonapi-renderer (0.2.2)
launchy (2.5.0)
addressable (~> 2.7)
letter_opener (1.7.0)
Expand Down Expand Up @@ -246,6 +254,7 @@ PLATFORMS
x86-mswin32

DEPENDENCIES
active_model_serializers
bootsnap (>= 1.4.4)
byebug
capybara (>= 3.26)
Expand Down
9 changes: 6 additions & 3 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ class HomeController < ApplicationController
before_action :set_todo_item, only: [:edit_todo_item]

def landing
@todos = Todo.all.order(:id)
@todos = Todo.all.order(:id).map { |record| TodoSerializer.new(record) }
end

def edit_todo_item
@todo_item.update(todo_item_params)
@todo_item.image.attach(todo_item_params[:image]) if todo_item_params[:image]
@todo_item.update(todo_item_params.except(:image))

render json: @todo_item
end

def reset_todo_items
Expand All @@ -18,7 +21,7 @@ def reset_todo_items
private

def todo_item_params
params.permit(:id, :title, :checked)
params.require(:home).permit(:id, :title, :checked, :image)
end

def set_todo_item
Expand Down
37 changes: 34 additions & 3 deletions app/javascript/components/TodoList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type TodoItem = {
id: number;
title: string;
checked: boolean;
image: string;
};

type Props = {
Expand All @@ -26,9 +27,34 @@ const TodoList: React.FC<Props> = ({ todoItems }) => {
todoItemId: number
): void => {
axios.post("/todo", {
id: todoItemId,
checked: e.target.checked,
});
home: {
id: todoItemId,
checked: e.target.checked,
}
}).then((res) => {
let foundIndex = todoItems.findIndex(todo => todo.id == res.data.id);
todoItems[foundIndex] = res.data.id;
e.target.checked = res.data.checked;
});

};

const onImageUpload = (event, id) => {
const data = new FormData();
data.append("home[image]", event.target.files[0]);
data.append("home[id]", `${id}`);

axios.post('todo/', data, {
headers: {
'Content-Type': 'multipart/form-data'
}})
.then((res) => {
let foundIndex = todoItems.findIndex(x => x.id == res.data.id);
todoItems[foundIndex] = res.data.id;

const image = document.getElementById(`img_${res.data.id}`) as HTMLImageElement | null;
image.src = res.data.image;
});
};

const resetButtonOnClick = (): void => {
Expand All @@ -47,6 +73,11 @@ const TodoList: React.FC<Props> = ({ todoItems }) => {
checked={todo.checked}
onChange={(e) => checkBoxOnCheck(e, todo.id)}
/>

<img id={`img_${todo.id}`}
src={todo.image}
style={{maxHeight: '100px', maxWidth: '100px'}}/>
<Form.Control type="file" accept="image/*" multiple={false} onChange={(e) => onImageUpload(e, todo.id)} />
</ListGroup.Item>
))}
<ResetButton onClick={resetButtonOnClick}>Reset</ResetButton>
Expand Down
1 change: 1 addition & 0 deletions app/models/todo.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Todo < ApplicationRecord
has_one_attached :image
end
9 changes: 9 additions & 0 deletions app/serializers/todo_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class TodoSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers

attributes :id, :title, :checked, :image

def image
rails_blob_path(object.image, only_path: true) if object.image.attached?
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
Rails.application.routes.draw do
root to: "home#landing"
post "todo", to: "home#edit_todo_item"
post "reset", to: "home#reset_todo_item"
post "reset", to: "home#reset_todo_items"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false

t.index [ :key ], unique: true
end

create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false

t.datetime :created_at, null: false

t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records do |t|
t.belongs_to :blob, null: false, index: false
t.string :variation_digest, null: false

t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
end
32 changes: 31 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.