Skip to content

Commit

Permalink
添加编辑各种 score,ranking 的功能.
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed May 3, 2024
1 parent 5246b6c commit 475628a
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 19 deletions.
12 changes: 12 additions & 0 deletions src/actions/universities/htmx/render_update_score_input.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Universities::Htmx::RenderUpdateScoreInput < HtmxAction
get "/universities/render_update_score_input" do
id = params.get("id")
column_name = params.get("column_name")
column_value = params.get("column_value")

html UpdateScoreInputPage,
column_name: column_name,
column_value: column_value,
id: id
end
end
16 changes: 14 additions & 2 deletions src/actions/universities/update.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
class Universities::Update < BrowserAction
put "/universities/:university_id" do
university = UniversityQuery.find(university_id)

SaveUniversity.update(university, params) do |operation, updated_university|
if operation.saved?
flash.success = "修改成功"
redirect Edit.with(updated_university.id)
if request.headers["HX-Trigger"] == "update_score_input"
param_value = params.nested?(:university).to_a[0]
html(
Htmx::UpdatedScoreInputPage,
id: university.id.to_s,
column_name: param_value[0],
column_value: param_value[1],
action: "/htmx/v1/universities/render_update_score_input"
)
else
flash.success = "修改成功"
redirect Edit.with(updated_university.id)
end
else
flash.failure = "出错了"
html EditPage, operation: operation, university: updated_university
Expand Down
21 changes: 21 additions & 0 deletions src/components/universities/mouseenter_td.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Universities::MouseenterTD < BaseComponent
needs id : String
needs column_value : String
needs column_name : String
needs action : String

def render
td(
"hx-trigger": "click",
"hx-swap": "outerHTML",
"hx-get": action,
"hx-vals": "{
\"column_name\":\"#{column_name}\",
\"id\":\"#{id}\",
\"column_value\": \"#{column_value}\"
}",
) do
text column_value
end
end
end
2 changes: 1 addition & 1 deletion src/css/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body {
Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue,
sans-serif;
margin: 0 auto;
max-width: 800px;
max-width: 1920px;
padding: 20px 40px;
}

Expand Down
37 changes: 21 additions & 16 deletions src/operations/save_university.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ class SaveUniversity < University::SaveOperation
attribute city_name : String

before_save do
validate_required province_code
validate_required province_name

province = SaveProvince.upsert!(
name: province_name.value.to_s,
code: province_code.value.as(Int32)
)

city = SaveCity.upsert!(
name: city_name.value.to_s,
code: city_code.value.as(Int32),
province_id: province.id
)

province_id.value = province.id
city_id.value = city.id
# 这两行造成单独 update 某一个字段失败.
# validate_required province_code
# validate_required province_name

province_name.value.try do |province_name_value|
province_code.value.try do |proinvce_code_value|
province = SaveProvince.upsert!(
name: province_name.value.to_s,
code: province_code.value.as(Int32)
)

city = SaveCity.upsert!(
name: city_name.value.to_s,
code: city_code.value.as(Int32),
province_id: province.id
)

province_id.value = province.id
city_id.value = city.id
end
end
end

before_save code_and_batch_level_must_uniq
Expand Down
22 changes: 22 additions & 0 deletions src/pages/universities/htmx/update_score_input_page.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Universities::Htmx::UpdateScoreInputPage < NoLayout
needs column_name : String
needs column_value : String
needs id : String

def content
td do
input(
type: "text",
value: "#{column_value}",
name: "university:#{column_name}",
id: "update_score_input",
"hx-put": Universities::Update.with(id).path,
"hx-include": "next input[type='hidden']",
"hx-target": "closest td",
"hx-swap": "outherHTML",
"hx-trigger": "mouseout",
style: "max-width: 60px; max-height: 30px;"
)
end
end
end
16 changes: 16 additions & 0 deletions src/pages/universities/htmx/updated_score_input_page.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Universities::Htmx::UpdatedScoreInputPage < NoLayout
needs column_name : String
needs column_value : String
needs action : String
needs id : String

def content
mount(
MouseenterTD,
id: id,
column_value: column_value,
column_name: column_name,
action: action
)
end
end
65 changes: 65 additions & 0 deletions src/pages/universities/index_page.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ class Universities::IndexPage < MainLayout
th "大学名称(点击名称编辑)"
th "录取批次"
th "补充信息"
th "2023最低分"
th "2023最低位次"
th "2022最低分"
th "2022最低位次"
th "2021最低分"
th "2021最低位次"
th "2020最低分"
th "2020最低位次"
th "修改时间"
end
end
Expand All @@ -67,9 +75,66 @@ class Universities::IndexPage < MainLayout
td do
text university.description.to_s
end
mount(
MouseenterTD,
id: university.id.to_s,
column_value: university.score_2023_min.to_s,
column_name: "score_2023_min",
action: "/htmx/v1/universities/render_update_score_input"
)
mount(
MouseenterTD,
id: university.id.to_s,
column_value: university.score_2022_min.to_s,
column_name: "score_2022_min",
action: "/htmx/v1/universities/render_update_score_input"
)
mount(
MouseenterTD,
id: university.id.to_s,
column_value: university.score_2021_min.to_s,
column_name: "score_2021_min",
action: "/htmx/v1/universities/render_update_score_input"
)
mount(
MouseenterTD,
id: university.id.to_s,
column_value: university.score_2020_min.to_s,
column_name: "score_2020_min",
action: "/htmx/v1/universities/render_update_score_input"
)
mount(
MouseenterTD,
id: university.id.to_s,
column_value: university.ranking_2023_min.to_s,
column_name: "ranking_2023_min",
action: "/htmx/v1/universities/render_update_score_input"
)
mount(
MouseenterTD,
id: university.id.to_s,
column_value: university.ranking_2022_min.to_s,
column_name: "ranking_2022_min",
action: "/htmx/v1/universities/render_update_score_input"
)
mount(
MouseenterTD,
id: university.id.to_s,
column_value: university.ranking_2021_min.to_s,
column_name: "ranking_2021_min",
action: "/htmx/v1/universities/render_update_score_input"
)
mount(
MouseenterTD,
id: university.id.to_s,
column_value: university.ranking_2020_min.to_s,
column_name: "ranking_2020_min",
action: "/htmx/v1/universities/render_update_score_input"
)
td university.updated_at.to_s("%m月%d日 %H:%M:%S")
end
end
input(type: "hidden", value: context.session.get("X-CSRF-TOKEN"), name: "_csrf")
end
end
end
Expand Down

0 comments on commit 475628a

Please sign in to comment.