From d65ab1cd05a863a63b509ed61b98c961bbcc15b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Pr=C3=A9vost?= Date: Fri, 15 Dec 2023 10:07:45 -0500 Subject: [PATCH] Fix uncorrect translation with text should update the text --- lib/graphql/mutations/translation.ex | 3 +- lib/graphql/resolvers/translation.ex | 3 +- .../translation_uncorrect_conflict.ex | 5 +- lib/movement/migration/conflict.ex | 9 ++- .../operations_update_all_dynamic.ex | 37 +++++++++- .../components/translation-edit/component.ts | 4 +- .../components/translation-edit/template.hbs | 74 +++++++------------ .../project/translation/index/controller.ts | 3 +- webapp/app/queries/uncorrect-translation.ts | 4 +- 9 files changed, 77 insertions(+), 65 deletions(-) diff --git a/lib/graphql/mutations/translation.ex b/lib/graphql/mutations/translation.ex index 37a875d7..590901c7 100644 --- a/lib/graphql/mutations/translation.ex +++ b/lib/graphql/mutations/translation.ex @@ -16,13 +16,14 @@ defmodule Accent.GraphQL.Mutations.Translation do field :uncorrect_translation, :mutated_translation do arg(:id, non_null(:id)) + arg(:text, non_null(:string)) resolve(translation_authorize(:uncorrect_translation, &TranslationResolver.uncorrect/3)) end field :update_translation, :mutated_translation do arg(:id, non_null(:id)) - arg(:text, :string) + arg(:text, non_null(:string)) resolve(translation_authorize(:update_translation, &TranslationResolver.update/3)) end diff --git a/lib/graphql/resolvers/translation.ex b/lib/graphql/resolvers/translation.ex index a7868267..9f052e1d 100644 --- a/lib/graphql/resolvers/translation.ex +++ b/lib/graphql/resolvers/translation.ex @@ -52,9 +52,10 @@ defmodule Accent.GraphQL.Resolvers.Translation do end @spec uncorrect(Translation.t(), map(), GraphQLContext.t()) :: translation_operation - def uncorrect(translation, _, info) do + def uncorrect(translation, %{text: text}, info) do %Context{} |> Context.assign(:translation, translation) + |> Context.assign(:text, text) |> Context.assign(:user_id, info.context[:conn].assigns[:current_user].id) |> TranslationUncorrectConflictBuilder.build() |> then(&fn -> BasePersister.execute(&1) end) diff --git a/lib/movement/builders/translation_uncorrect_conflict.ex b/lib/movement/builders/translation_uncorrect_conflict.ex index 067a443b..fa436402 100644 --- a/lib/movement/builders/translation_uncorrect_conflict.ex +++ b/lib/movement/builders/translation_uncorrect_conflict.ex @@ -6,8 +6,9 @@ defmodule Movement.Builders.TranslationUncorrectConflict do @action "uncorrect_conflict" - def build(%Movement.Context{assigns: %{translation: translation}, operations: operations} = context) do - operation = OperationMapper.map(@action, translation, %{text: nil}) + def build(%Movement.Context{assigns: %{translation: translation, text: text}, operations: operations} = context) do + value_type = Movement.Mappers.ValueType.from_translation_new_value(translation, text) + operation = OperationMapper.map(@action, translation, %{text: text, value_type: value_type}) %{context | operations: Enum.concat(operations, [operation])} end diff --git a/lib/movement/migration/conflict.ex b/lib/movement/migration/conflict.ex index b8f29480..72675acf 100644 --- a/lib/movement/migration/conflict.ex +++ b/lib/movement/migration/conflict.ex @@ -18,11 +18,12 @@ defmodule Movement.Migration.Conflict do def call(:uncorrect, operation) do update_all_dynamic( operation.translation, - [:text, :text, :boolean], - [:conflicted_text, :value_type, :conflicted], + [:text, :text, :text, :boolean], + [:corrected_text, :conflicted_text, :value_type, :conflicted], [ - operation.previous_translation && operation.previous_translation.conflicted_text, - operation.previous_translation && operation.previous_translation.value_type, + operation.text, + operation.previous_translation && operation.previous_translation.corrected_text, + operation.value_type, true ] ) diff --git a/lib/movement/persisters/operations_update_all_dynamic.ex b/lib/movement/persisters/operations_update_all_dynamic.ex index 7f5ce163..b664361f 100644 --- a/lib/movement/persisters/operations_update_all_dynamic.ex +++ b/lib/movement/persisters/operations_update_all_dynamic.ex @@ -7,6 +7,7 @@ defmodule Movement.Persisters.OperationsUpdateAllDynamic do @uuid_fragment "SELECT * FROM unnest(?::uuid[], ?::uuid[]) AS t(a, b)" @text_text_bool_bool_fragment "SELECT * FROM unnest(?::uuid[], ?::text[], ?::text[], ?::boolean[], ?::boolean[]) AS t(a, b, c, d, e)" @text_text_bool_fragment "SELECT * FROM unnest(?::uuid[], ?::text[], ?::text[], ?::boolean[]) AS t(a, b, c, d)" + @text_text_text_bool_fragment "SELECT * FROM unnest(?::uuid[], ?::text[], ?::text[], ?::text[], ?::boolean[]) AS t(a, b, c, d, e)" def update({{schema, [:text, :text, :boolean, :boolean], fields}, records}) do [bind_1, bind_2, bind_3, bind_4] = values_binding(records, fields) @@ -28,7 +29,8 @@ defmodule Movement.Persisters.OperationsUpdateAllDynamic do {^Enum.at(fields, 0), values_list.b}, {^Enum.at(fields, 1), values_list.c}, {^Enum.at(fields, 2), values_list.d}, - {^Enum.at(fields, 3), values_list.e} + {^Enum.at(fields, 3), values_list.e}, + updated_at: ^DateTime.utc_now(:second) ] ] ) @@ -47,7 +49,7 @@ defmodule Movement.Persisters.OperationsUpdateAllDynamic do ^bind_1 ), on: values_list.a == entries.id, - update: [set: [{^Enum.at(fields, 0), values_list.b}]] + update: [set: [{^Enum.at(fields, 0), values_list.b}, updated_at: ^DateTime.utc_now(:second)]] ) ) end @@ -70,7 +72,36 @@ defmodule Movement.Persisters.OperationsUpdateAllDynamic do set: [ {^Enum.at(fields, 0), values_list.b}, {^Enum.at(fields, 1), values_list.c}, - {^Enum.at(fields, 2), values_list.d} + {^Enum.at(fields, 2), values_list.d}, + updated_at: ^DateTime.utc_now(:second) + ] + ] + ) + ) + end + + def update({{schema, [:text, :text, :text, :boolean], fields}, records}) do + [bind_1, bind_2, bind_3, bind_4] = values_binding(records, fields) + + update_all( + from(entries in schema, + join: + values_list in fragment( + @text_text_text_bool_fragment, + ^ids_binding(records), + ^bind_1, + ^bind_2, + ^bind_3, + ^bind_4 + ), + on: values_list.a == entries.id, + update: [ + set: [ + {^Enum.at(fields, 0), values_list.b}, + {^Enum.at(fields, 1), values_list.c}, + {^Enum.at(fields, 2), values_list.d}, + {^Enum.at(fields, 3), values_list.e}, + updated_at: ^DateTime.utc_now(:second) ] ] ) diff --git a/webapp/app/pods/components/translation-edit/component.ts b/webapp/app/pods/components/translation-edit/component.ts index 2ed1807e..bd6b16be 100644 --- a/webapp/app/pods/components/translation-edit/component.ts +++ b/webapp/app/pods/components/translation-edit/component.ts @@ -11,7 +11,7 @@ interface Args { onChangeText?: (text: string) => void; onUpdateText: (text: string) => Promise; onCorrectConflict: (text: string) => Promise; - onUncorrectConflict: () => Promise; + onUncorrectConflict: (text: string) => Promise; } export default class TranslationEdit extends Component { @@ -92,7 +92,7 @@ export default class TranslationEdit extends Component { async uncorrectConflict() { this.isUncorrectingConflict = true; - await this.args.onUncorrectConflict(); + await this.args.onUncorrectConflict(this.text); this.isUncorrectingConflict = false; } diff --git a/webapp/app/pods/components/translation-edit/template.hbs b/webapp/app/pods/components/translation-edit/template.hbs index b2adb949..f28b827f 100644 --- a/webapp/app/pods/components/translation-edit/template.hbs +++ b/webapp/app/pods/components/translation-edit/template.hbs @@ -33,59 +33,35 @@ {{/if}} {{#if @translation}} - {{#if @translation.isConflicted}} - {{#unless @translation.isRemoved}} -
-
- {{#if @translation.sourceTranslation}} - - {{t 'components.translation_edit.source_translation'}} - - {{/if}} -
-
- {{#unless this.hasTextNotChanged}} - - {{inline-svg '/assets/revert.svg' class='button-icon'}} - - {{/unless}} + {{#unless @translation.isRemoved}} +
+
+ {{#if @translation.sourceTranslation}} + + {{t 'components.translation_edit.source_translation'}} + + {{/if}} +
+
+ {{#unless this.hasTextNotChanged}} + + {{inline-svg '/assets/revert.svg' class='button-icon'}} + + {{/unless}} - {{#if (get @permissions 'update_translation')}} - - {{t 'components.translation_edit.update_text'}} - - {{/if}} + {{#if (get @permissions 'update_translation')}} + + {{t 'components.translation_edit.update_text'}} + + {{/if}} + {{#if @translation.isConflicted}} {{#if (get @permissions 'correct_translation')}} {{inline-svg '/assets/check.svg' class='button-icon'}} {{t 'components.translation_edit.correct_button'}} {{/if}} -
-
- {{/unless}} - {{else}} - {{#unless @translation.isRemoved}} -
-
- {{#if @translation.sourceTranslation}} - - {{t 'components.translation_edit.source_translation'}} - - {{/if}} -
-
- {{#unless this.hasTextNotChanged}} - - {{inline-svg '/assets/revert.svg' class='button-icon'}} - - {{/unless}} - - {{#if (get @permissions 'update_translation')}} - - {{t 'components.translation_edit.update_text'}} - - {{/if}} + {{else}} {{#if (get @permissions 'uncorrect_translation')}} @@ -93,9 +69,9 @@ {{t 'components.translation_edit.uncorrect_button'}} {{/if}} -
+ {{/if}}
- {{/unless}} - {{/if}} +
+ {{/unless}} {{/if}}
\ No newline at end of file diff --git a/webapp/app/pods/logged-in/project/translation/index/controller.ts b/webapp/app/pods/logged-in/project/translation/index/controller.ts index 295b502a..33e84a4c 100644 --- a/webapp/app/pods/logged-in/project/translation/index/controller.ts +++ b/webapp/app/pods/logged-in/project/translation/index/controller.ts @@ -65,13 +65,14 @@ export default class IndexController extends Controller { } @action - async uncorrectConflict() { + async uncorrectConflict(text: string) { const conflict = this.model.translation; const response = await this.apolloMutate.mutate({ mutation: translationUncorrectQuery, variables: { translationId: conflict.id, + text, }, }); diff --git a/webapp/app/queries/uncorrect-translation.ts b/webapp/app/queries/uncorrect-translation.ts index 79ef69e3..b89d4721 100644 --- a/webapp/app/queries/uncorrect-translation.ts +++ b/webapp/app/queries/uncorrect-translation.ts @@ -1,8 +1,8 @@ import gql from 'graphql-tag'; export default gql` - mutation TranslationUncorrect($translationId: ID!) { - uncorrectTranslation(id: $translationId) { + mutation TranslationUncorrect($translationId: ID!, $text: String!) { + uncorrectTranslation(id: $translationId, text: $text) { translation { id correctedText