aliasing now move column position also
This commit is contained in:
@@ -23,7 +23,7 @@ use crate::{
|
||||
AppState,
|
||||
definitions::table_definition::{
|
||||
AddTableColumnsRequest, CopyProfileRequest, CreateInvoiceTemplateTableRequest,
|
||||
DeleteTableRequest, RenameColumnAliasRequest,
|
||||
ColumnPresentation, DeleteTableRequest, SetColumnPresentationRequest,
|
||||
},
|
||||
{i18n::Locale, tr},
|
||||
schema::{ColumnForm, proto_columns},
|
||||
@@ -34,7 +34,7 @@ use super::{
|
||||
loader::{self, load_page},
|
||||
state::{
|
||||
CopyForm, DeleteForm, GeneratedTableView, InvoiceTemplateForm, LoadError, PageInputs,
|
||||
RenameForm, Selection, TableDefinitionPageState,
|
||||
PresentationForm, Selection, TableDefinitionPageState,
|
||||
},
|
||||
ui,
|
||||
};
|
||||
@@ -303,11 +303,11 @@ pub(crate) async fn add_columns(
|
||||
}
|
||||
}
|
||||
|
||||
/// POST /admin/tables/rename — RenameColumnAlias.
|
||||
pub(crate) async fn rename_column(
|
||||
/// POST /admin/tables/presentation — SetColumnPresentation.
|
||||
pub(crate) async fn set_column_presentation(
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
Form(form): Form<RenameForm>,
|
||||
Form(form): Form<PresentationForm>,
|
||||
) -> Response {
|
||||
if let Some(rejection) = reject_cross_site(&headers) {
|
||||
return rejection;
|
||||
@@ -317,9 +317,12 @@ pub(crate) async fn rename_column(
|
||||
profile: form.profile.clone(),
|
||||
table: form.table.clone(),
|
||||
});
|
||||
inputs.rename = form.clone();
|
||||
inputs.presentation = form.clone();
|
||||
|
||||
if form.old_column_name.is_empty() || form.new_column_name.trim().is_empty() {
|
||||
if form.column_ids.is_empty()
|
||||
|| form.column_ids.len() != form.aliases.len()
|
||||
|| form.aliases.iter().any(|alias| alias.trim().is_empty())
|
||||
{
|
||||
let message = tr!(
|
||||
Locale::from_headers(&headers),
|
||||
"td-err-choose-rename"
|
||||
@@ -334,21 +337,42 @@ pub(crate) async fn rename_column(
|
||||
.await;
|
||||
}
|
||||
|
||||
let request = RenameColumnAliasRequest {
|
||||
let mut columns = form
|
||||
.column_ids
|
||||
.iter()
|
||||
.copied()
|
||||
.zip(form.aliases.iter())
|
||||
.map(|(column_id, alias)| ColumnPresentation {
|
||||
column_id,
|
||||
alias: alias.trim().to_string(),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if let Some((direction, index)) = form.action.split_once(':') {
|
||||
if let Ok(index) = index.parse::<usize>() {
|
||||
let other = match direction {
|
||||
"up" => index.checked_sub(1),
|
||||
"down" if index + 1 < columns.len() => Some(index + 1),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(other) = other {
|
||||
columns.swap(index, other);
|
||||
}
|
||||
}
|
||||
}
|
||||
let request = SetColumnPresentationRequest {
|
||||
profile_name: form.profile.clone(),
|
||||
table_name: form.table.clone(),
|
||||
old_column_name: form.old_column_name.clone(),
|
||||
new_column_name: form.new_column_name.trim().to_string(),
|
||||
columns,
|
||||
};
|
||||
let Ok(request) = authenticated_request(&headers, request) else {
|
||||
return Redirect::to("/login").into_response();
|
||||
};
|
||||
|
||||
let mut definitions = state.definitions.clone();
|
||||
match definitions.rename_column_alias(request).await {
|
||||
match definitions.set_column_presentation(request).await {
|
||||
Ok(response) if response.get_ref().success => {
|
||||
inputs.status = Some(response.into_inner().message);
|
||||
inputs.rename = RenameForm {
|
||||
inputs.presentation = PresentationForm {
|
||||
profile: form.profile,
|
||||
table: form.table,
|
||||
..Default::default()
|
||||
|
||||
Reference in New Issue
Block a user