Class: ImageUploaderComponent

Inherits:
Funicular::Component
  • Object
show all
Defined in:
lib/components/image_uploader_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#selected_fileObject (readonly)

Returns the value of attribute selected_file.



2
3
4
# File 'lib/components/image_uploader_component.rb', line 2

def selected_file
  @selected_file
end

Instance Method Details

#clearObject



29
30
31
32
33
# File 'lib/components/image_uploader_component.rb', line 29

def clear
  @selected_file = nil
  patch(preview_url: nil, uploaded_url: nil, error: nil)
  emit_select(nil, nil)
end

#handle_file_change(event = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/components/image_uploader_component.rb', line 14

def handle_file_change(event = nil)
  Funicular::FileUpload.select_file_with_preview(input_id) do |file, preview_url|
    if file && preview_url
      @selected_file = file
      patch(preview_url: preview_url, error: nil)
      emit_select(file, preview_url)
      upload if props[:auto_upload]
    else
      @selected_file = nil
      patch(preview_url: nil)
      emit_select(nil, nil)
    end
  end
end

#handle_upload_result(result) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/components/image_uploader_component.rb', line 61

def handle_upload_result(result)
  if result.nil?
    patch(uploading: false, error: "Failed to parse response")
    emit_error("Failed to parse response")
    return
  end

  if result["error"] || result["errors"]
    message = result["error"] || result["errors"].join(", ")
    patch(uploading: false, error: message)
    emit_error(message, result)
    return
  end

  @selected_file = nil
  uploaded_url = result[response_url_key] || props[:src]
  patch(
    preview_url: nil,
    uploaded_url: uploaded_url,
    uploading: false,
    error: nil,
    cache_buster: Time.now.to_i
  )
  emit_upload(result)
end

#initialize_stateObject



4
5
6
7
8
9
10
11
12
# File 'lib/components/image_uploader_component.rb', line 4

def initialize_state
  {
    preview_url: nil,
    uploaded_url: nil,
    uploading: false,
    error: nil,
    cache_buster: Time.now.to_i
  }
end

#renderObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/components/image_uploader_component.rb', line 87

def render
  div(class: container_class) do
    render_image
    render_error
    div(class: controls_class) do
      input(
        type: "file",
        id: input_id,
        accept: props[:accept] || "image/*",
        onchange: :handle_file_change,
        class: input_class
      )
      if clearable?
        button(type: "button", class: clear_button_class, onclick: -> { clear }) do
          props[:clear_label] || "Clear"
        end
      end
      if props[:show_upload_button]
        button(
          type: "button",
          class: upload_button_class,
          disabled: state.uploading || @selected_file.nil?,
          onclick: -> { upload }
        ) do
          state.uploading ? uploading_label : upload_label
        end
      end
    end
  end
end

#upload(fields: nil, url: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/components/image_uploader_component.rb', line 35

def upload(fields: nil, url: nil)
  endpoint = url || props[:upload_url]
  unless endpoint
    patch(error: "upload_url is required")
    emit_error("upload_url is required")
    return
  end

  unless @selected_file
    patch(error: "No file selected")
    emit_error("No file selected")
    return
  end

  patch(uploading: true, error: nil)

  Funicular::FileUpload.upload_with_formdata(
    endpoint,
    fields: fields || upload_fields,
    file_field: file_field,
    file: @selected_file
  ) do |result|
    handle_upload_result(result)
  end
end