1
0
mirror of https://github.com/danbee/my-images synced 2025-03-04 08:49:05 +00:00

Ended up not using Rails UJS after all

This commit is contained in:
Daniel Barber 2018-09-12 09:14:26 -04:00
parent f5bc989048
commit f134388fac
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
8 changed files with 22 additions and 25 deletions

View File

@ -9,7 +9,7 @@ class TagsController < ApplicationController
image.save
render partial: "tags/tag", locals: { image: image, tag: tag }
else
head 200, content_type: "text/html"
head :no_content, content_type: "text/html"
end
end
@ -19,6 +19,6 @@ class TagsController < ApplicationController
image.tags.delete(tag)
image.save
render json: { status: :ok }
head :no_content
end
end

View File

@ -5,7 +5,6 @@ export default class extends Controller {
static targets = ["name"];
delete(event) {
const imageId = this.element.dataset.imageId;
const tag = this.nameTarget.innerText;
event.preventDefault();

View File

@ -1,11 +1,20 @@
import { Controller } from "stimulus";
import ajaxService from "../services/ajax_service";
export default class extends Controller {
static targets = ["tag", "tagList"]
onPostSuccess(event) {
let [data, status, xhr] = event.detail;
this.tagListTarget.innerHTML += xhr.response;
this.tagTarget.value = "";
create(event) {
const tag = this.tagTarget.value;
event.preventDefault();
ajaxService.createTag(tag)
.then(response => {
if (response.status == 200) {
this.tagListTarget.innerHTML += response.data;
};
this.tagTarget.value = "";
})
}
}

View File

@ -7,12 +7,9 @@
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb
import Rails from "rails-ujs";
import { Application } from "stimulus";
import { definitionsFromContext } from "stimulus/webpack-helpers";
Rails.start();
const application = Application.start();
const context = require.context("controllers", true, /.js$/);
application.load(definitionsFromContext(context));

View File

@ -18,12 +18,15 @@ const config = () => ({
headers: { "X-CSRF-Token": csrfToken() }
});
export default {
createTag: (tag) => {
return axios.post(`/user/images/${imageId()}/tags`, { tag }, config());
},
deleteTag: (tag) => {
return axios.delete(`/user/images/${imageId()}/tags/${tag}.json`, config());
return axios.delete(
`/user/images/${imageId()}/tags/${encodeURIComponent(tag)}`,
config()
);
}
}

View File

@ -3,15 +3,9 @@
<%= render "tags/tags", image: image, tags: image.tags %>
<div class="new-tag-form">
<%= form_with url: user_image_tags_path(image),
<%= form_tag user_image_tags_path(image),
method: :post,
html: {
data: {
remote: true,
type: "html",
action: "ajax:success->tags#onPostSuccess"
}
} do %>
data: { action: "tags#create" } do %>
<%= text_field_tag :tag, nil, data: { target: "tags.tag" } %>
<%= submit_tag "Add Tag" %>
<% end %>

View File

@ -2,7 +2,6 @@
"dependencies": {
"@rails/webpacker": "3.5",
"axios": "^0.18.0",
"rails-ujs": "^5.2.1",
"stimulus": "^1.1.0"
},
"devDependencies": {

View File

@ -4563,10 +4563,6 @@ querystringify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.0.0.tgz#fa3ed6e68eb15159457c89b37bc6472833195755"
rails-ujs@^5.2.1:
version "5.2.1"
resolved "https://registry.yarnpkg.com/rails-ujs/-/rails-ujs-5.2.1.tgz#2869c6d54fdfefac3aaa257f4efe211d8f5a7169"
randomatic@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-3.1.0.tgz#36f2ca708e9e567f5ed2ec01949026d50aa10116"