Skip to content

Commit 7acfd8c

Browse files
committed
feat: in case of a super long translating job, make the user wait (+ polling)
1 parent 141ce37 commit 7acfd8c

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

app/controllers/maglev/api/page_translations_controller.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33
module Maglev
44
module Api
55
class PageTranslationsController < ::Maglev::ApiController
6+
before_action :set_page
7+
8+
def index
9+
translations = @page.translations_for(:sections, params[:locale])
10+
render json: { translated: translations&.size.to_i > 0 }
11+
end
12+
613
def create
7-
@page = translate_page(resources.find(params[:page_id]))
14+
translate_page(@page)
15+
head :ok
816
end
917

1018
private
1119

20+
def set_page
21+
@page ||= resources.find(params[:page_id])
22+
end
23+
1224
def translate_page(page)
1325
services.translate_page.call(
1426
page: page,

app/frontend/editor/components/translate-dialog/index.vue

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
export default {
4040
name: 'TranslateDialog',
4141
data() {
42-
return { submitState: 'default' }
42+
return { submitState: 'default', pollingSubmission: false, pollingInterval: null }
4343
},
4444
computed: {
4545
localeName() {
@@ -49,17 +49,45 @@ export default {
4949
return this.$t(`support.locales.${this.currentDefaultLocale}`)
5050
}
5151
},
52+
unmounted() {
53+
if (this.pollingInterval) clearInterval(this.pollingInterval)
54+
},
5255
methods: {
5356
translate() {
5457
this.submitState = 'inProgress'
5558
this.services.page.translate(this.currentPage.id, this.currentLocale)
56-
.then(() => {
57-
// Force a refresh
58-
this.$nextTick(() => { this.$router.go(0) })
59+
.then(() => {
60+
this.pollingSubmission = true
61+
// Force a refresh
62+
// this.$nextTick(() => { this.$router.go(0) })
5963
})
6064
.catch(() => {
6165
this.submitState = 'fail'
6266
})
67+
},
68+
isTranslated() {
69+
this.services.page.isTranslated(this.currentPage.id, this.currentLocale)
70+
.then((translated) => {
71+
if (translated) {
72+
this.pollingSubmission = false
73+
// Force a refresh
74+
this.$nextTick(() => { this.$router.go(0) })
75+
}
76+
})
77+
.catch(() => {
78+
this.pollingSubmission = false
79+
this.submitState = 'fail'
80+
})
81+
}
82+
},
83+
watch: {
84+
pollingSubmission(newValue, oldValue) {
85+
if (!newValue) {
86+
if (this.pollingInterval) clearInterval(this.pollingInterval)
87+
return
88+
}
89+
90+
this.pollingInterval = setInterval(() => this.isTranslated(), 1000)
6391
}
6492
}
6593
}

app/frontend/editor/services/page.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ export default (api) => ({
9797
return api.post(`/pages/${id}/translations`, { locale }).then(({ data }) => data)
9898
},
9999

100+
isTranslated: (id, locale) => {
101+
console.log('[PageService] Checking if page is translated #', id, locale)
102+
return api.get(`/pages/${id}/translations`, { params: { locale } }).then(({ data }) => data.translated)
103+
},
104+
100105
destroy: (id) => {
101106
console.log('[PageService] Destroying page #', id)
102107
return api.destroy(`/pages/${id}`)

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
resource :site, only: :show
1010
resources :pages do
1111
resources :clones, controller: :page_clones, only: :create
12-
resources :translations, controller: :page_translations, only: %i[create]
12+
resources :translations, controller: :page_translations, only: %i[index create]
1313
end
1414
resources :assets
1515
resource :publication, only: %i[show create]

0 commit comments

Comments
 (0)