From c60e222faa36171f7709dc25458d2cc0650d8576 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Thu, 1 Feb 2024 05:55:19 -0800 Subject: [PATCH] Add Alt Style support to outfit saving MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pretty straightforward, just add the field to the record, and wire it all up! I'm glad this seemed to work out pretty well all-in-all 😅 --- app/controllers/outfits_controller.rb | 2 +- .../WardrobePage/useOutfitSaving.js | 1 + .../WardrobePage/useOutfitState.js | 1 + .../wardrobe-2020/loaders/outfits.js | 23 +++++++++++++++---- app/models/outfit.rb | 4 +++- ...40201134440_add_alt_style_id_to_outfits.rb | 5 ++++ db/schema.rb | 15 +++++++----- 7 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 db/migrate/20240201134440_add_alt_style_id_to_outfits.rb diff --git a/app/controllers/outfits_controller.rb b/app/controllers/outfits_controller.rb index e8d41d6a..36e6b669 100644 --- a/app/controllers/outfits_controller.rb +++ b/app/controllers/outfits_controller.rb @@ -115,7 +115,7 @@ class OutfitsController < ApplicationController def outfit_params params.require(:outfit).permit( - :name, :starred, item_ids: {worn: [], closeted: []}, + :name, :starred, :alt_style_id, item_ids: {worn: [], closeted: []}, biology: [:species_id, :color_id, :pose]) end diff --git a/app/javascript/wardrobe-2020/WardrobePage/useOutfitSaving.js b/app/javascript/wardrobe-2020/WardrobePage/useOutfitSaving.js index 2db70218..9993fe16 100644 --- a/app/javascript/wardrobe-2020/WardrobePage/useOutfitSaving.js +++ b/app/javascript/wardrobe-2020/WardrobePage/useOutfitSaving.js @@ -69,6 +69,7 @@ function useOutfitSaving(outfitState, dispatchToOutfit) { speciesId: outfitState.speciesId, colorId: outfitState.colorId, pose: outfitState.pose, + altStyleId: outfitState.altStyleId, wornItemIds: [...outfitState.wornItemIds], closetedItemIds: [...outfitState.closetedItemIds], }) diff --git a/app/javascript/wardrobe-2020/WardrobePage/useOutfitState.js b/app/javascript/wardrobe-2020/WardrobePage/useOutfitState.js index 7ad97c8e..683b9063 100644 --- a/app/javascript/wardrobe-2020/WardrobePage/useOutfitState.js +++ b/app/javascript/wardrobe-2020/WardrobePage/useOutfitState.js @@ -447,6 +447,7 @@ function getOutfitStateFromOutfitData(outfit) { speciesId: outfit.speciesId, colorId: outfit.colorId, pose: outfit.pose, + altStyleId: outfit.altStyleId, wornItemIds: new Set(outfit.wornItemIds), closetedItemIds: new Set(outfit.closetedItemIds), }; diff --git a/app/javascript/wardrobe-2020/loaders/outfits.js b/app/javascript/wardrobe-2020/loaders/outfits.js index 2063e7c3..fbafbec2 100644 --- a/app/javascript/wardrobe-2020/loaders/outfits.js +++ b/app/javascript/wardrobe-2020/loaders/outfits.js @@ -30,7 +30,9 @@ export function useDeleteOutfitMutation(options = {}) { ...options, mutationFn: deleteOutfit, onSuccess: (emptyData, id, context) => { - queryClient.invalidateQueries({ queryKey: ["outfits", String(id)] }); + queryClient.invalidateQueries({ + queryKey: ["outfits", String(id)], + }); if (options.onSuccess) { options.onSuccess(emptyData, id, context); } @@ -42,7 +44,9 @@ async function loadSavedOutfit(id) { const res = await fetch(`/outfits/${encodeURIComponent(id)}.json`); if (!res.ok) { - throw new Error(`loading outfit failed: ${res.status} ${res.statusText}`); + throw new Error( + `loading outfit failed: ${res.status} ${res.statusText}`, + ); } return res.json().then(normalizeOutfit); @@ -54,6 +58,7 @@ async function saveOutfit({ speciesId, colorId, pose, + altStyleId, wornItemIds, closetedItemIds, }) { @@ -65,6 +70,7 @@ async function saveOutfit({ color_id: colorId, pose: pose, }, + alt_style_id: altStyleId, item_ids: { worn: wornItemIds, closeted: closetedItemIds }, }, }; @@ -91,7 +97,9 @@ async function saveOutfit({ } if (!res.ok) { - throw new Error(`saving outfit failed: ${res.status} ${res.statusText}`); + throw new Error( + `saving outfit failed: ${res.status} ${res.statusText}`, + ); } return res.json().then(normalizeOutfit); @@ -106,7 +114,9 @@ async function deleteOutfit(id) { }); if (!res.ok) { - throw new Error(`deleting outfit failed: ${res.status} ${res.statusText}`); + throw new Error( + `deleting outfit failed: ${res.status} ${res.statusText}`, + ); } } @@ -117,8 +127,11 @@ function normalizeOutfit(outfit) { speciesId: String(outfit.species_id), colorId: String(outfit.color_id), pose: outfit.pose, + altStyleId: outfit.alt_style_id ? String(outfit.alt_style_id) : null, wornItemIds: (outfit.item_ids?.worn || []).map((id) => String(id)), - closetedItemIds: (outfit.item_ids?.closeted || []).map((id) => String(id)), + closetedItemIds: (outfit.item_ids?.closeted || []).map((id) => + String(id), + ), creator: outfit.user ? { id: String(outfit.user.id) } : null, createdAt: outfit.created_at, updatedAt: outfit.updated_at, diff --git a/app/models/outfit.rb b/app/models/outfit.rb index 5dfe4eda..3ae8b564 100644 --- a/app/models/outfit.rb +++ b/app/models/outfit.rb @@ -4,6 +4,7 @@ class Outfit < ApplicationRecord class_name: 'ItemOutfitRelationship' has_many :worn_items, through: :worn_item_outfit_relationships, source: :item + belongs_to :alt_style, optional: true belongs_to :pet_state, optional: true # We validate presence below! belongs_to :user, optional: true @@ -82,7 +83,8 @@ class Outfit < ApplicationRecord def as_json(more_options={}) serializable_hash( - only: [:id, :name, :pet_state_id, :starred, :created_at, :updated_at], + only: [:id, :name, :pet_state_id, :starred, :created_at, :updated_at, + :alt_style_id], methods: [:color_id, :species_id, :pose, :item_ids, :user] ) end diff --git a/db/migrate/20240201134440_add_alt_style_id_to_outfits.rb b/db/migrate/20240201134440_add_alt_style_id_to_outfits.rb new file mode 100644 index 00000000..6693aa03 --- /dev/null +++ b/db/migrate/20240201134440_add_alt_style_id_to_outfits.rb @@ -0,0 +1,5 @@ +class AddAltStyleIdToOutfits < ActiveRecord::Migration[7.1] + def change + add_reference :outfits, :alt_style, null: true, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index aa8374b8..c90fd48e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,13 +10,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_01_29_114639) do - create_table "alt_styles", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| +ActiveRecord::Schema[7.1].define(version: 2024_02_01_134440) do + create_table "alt_styles", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| t.integer "species_id", null: false t.integer "color_id", null: false t.integer "body_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", precision: nil, null: false + t.datetime "updated_at", precision: nil, null: false t.index ["color_id"], name: "index_alt_styles_on_color_id" t.index ["species_id"], name: "index_alt_styles_on_species_id" end @@ -125,11 +125,11 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_29_114639) do t.index ["outfit_id", "is_worn"], name: "index_item_outfit_relationships_on_outfit_id_and_is_worn" end - create_table "item_translations", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| + create_table "item_translations", id: :integer, charset: "latin1", collation: "latin1_swedish_ci", force: :cascade do |t| t.integer "item_id" t.string "locale" t.string "name" - t.text "description", size: :medium + t.text "description" t.string "rarity" t.datetime "created_at", precision: nil t.datetime "updated_at", precision: nil @@ -191,6 +191,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_29_114639) do t.string "image" t.string "image_layers_hash" t.boolean "image_enqueued", default: false, null: false + t.bigint "alt_style_id" + t.index ["alt_style_id"], name: "index_outfits_on_alt_style_id" t.index ["pet_state_id"], name: "index_outfits_on_pet_state_id" t.index ["user_id"], name: "index_outfits_on_user_id" end @@ -314,4 +316,5 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_29_114639) do add_foreign_key "alt_styles", "colors" add_foreign_key "alt_styles", "species" + add_foreign_key "outfits", "alt_styles" end