Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loaders are not visible on some pages #190

Merged
merged 4 commits into from Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/frontend/js/mixins/loads-resource.js
Expand Up @@ -4,6 +4,7 @@ import Bus from '@/js/Bus'
import Resource from '@/js/models/Resource'
import hasLoadingBus from '@/js/mixins/has-loading-bus'
import pluralize from 'pluralize'
import replace from 'lodash/replace'

export default {
mixins: [hasLoadingBus],
Expand All @@ -22,6 +23,11 @@ export default {

return `${Avo.rootPath}/avo-api/${this.resourceName}/new`
},
resourceNameFromURL() {
if (!this.resource) return replace(pluralize(this.resourceName, 1), '_', ' ')

return this.resource.singular_name
},
},
methods: {
hydrateRelatedResources(resource) {
Expand Down
16 changes: 12 additions & 4 deletions app/frontend/js/views/ResourceEdit.vue
@@ -1,9 +1,9 @@
<template>
<div v-if="resource" :resource-id="resourceId">
<div v-for="panel in resource.panels" :key="panel.name">
<div :resource-id="resourceId">
<div v-for="panel in panels" :key="panel.name">
<panel>
<template #heading>
{{ $t('avo.edit_item', { item: resourceNameSingular.toLowerCase() }) | upperFirst() }}
{{panel.name}}
</template>

<template #tools>
Expand Down Expand Up @@ -48,6 +48,7 @@ import HasForms from '@/js/mixins/has-forms'
import HasUniqueKey from '@/js/mixins/has-unique-key'
import LoadsResource from '@/js/mixins/loads-resource'
import hasUpperFirstFilter from '@/js/mixins/has-upper-first-filter'
import upperFirst from 'lodash/upperFirst'

export default {
mixins: [HasForms, LoadsResource, DealsWithResourceLabels, HasUniqueKey, hasUpperFirstFilter],
Expand All @@ -62,12 +63,17 @@ export default {
'viaResourceId',
],
computed: {
panels() {
if (!this.resource) return [{ name: upperFirst(this.$t('avo.edit_item', { item: this.resourceNameFromURL })) }]

return this.resource.panels
},
cancelActionParams() {
const action = {
name: 'show',
params: {
resourceName: this.resourceName,
resourceId: this.resource.id,
resourceId: this.resourceId,
},
}

Expand All @@ -79,6 +85,8 @@ export default {
return action
},
canUpdate() {
if (!this.resource) return false

return this.resource.authorization.update
},
},
Expand Down
21 changes: 16 additions & 5 deletions app/frontend/js/views/ResourceNew.vue
@@ -1,9 +1,9 @@
<template>
<div v-if="resource">
<div v-for="panel in resource.panels" :key="panel.name">
<div>
<div v-for="panel in panels" :key="panel.name">
<panel>
<template #heading>
{{ $t('avo.create_new_item', { item: resourceNameSingular.toLowerCase() }) | upperFirst() }}
{{panel.name}}
</template>

<template #tools>
Expand All @@ -14,7 +14,11 @@
resourceName: resourceName,
},
}"><arrow-left-icon class="h-4 mr-1"/> {{ $t('avo.cancel') }}</a-button>
<a-button color="green" @click="submitResource"><save-icon class="h-4 mr-1"/> {{ $t('avo.save') }}</a-button>
<a-button
color="green"
@click="submitResource"
v-if="resource"
><save-icon class="h-4 mr-1"/> {{ $t('avo.save') }}</a-button>
</div>
</template>

Expand Down Expand Up @@ -55,13 +59,20 @@ import LoadsResource from '@/js/mixins/loads-resource'
export default {
mixins: [HasForms, LoadsResource, DealsWithResourceLabels, HasUniqueKey],
data: () => ({
resource: {},
resource: null,
form: {},
}),
props: [
'resourceName',
'viaResourceName',
'viaResourceId',
],
computed: {
panels() {
if (!this.resource) return [{ name: (this.$t('avo.create_new_item', { item: this.resourceNameFromURL })) }]

return this.resource.panels
},
},
}
</script>
11 changes: 9 additions & 2 deletions app/frontend/js/views/ResourceShow.vue
@@ -1,5 +1,5 @@
<template>
<div v-if="resource" :resource-id="resourceId">
<div :resource-id="resourceId">
<div v-for="panel in panels" :key="panel.name">
<panel>
<template #heading>
Expand Down Expand Up @@ -75,6 +75,7 @@ import HasUpperFirstFilter from '@/js/mixins/has-upper-first-filter'
import LoadsActions from '@/js/mixins/loads-actions'
import LoadsResource from '@/js/mixins/loads-resource'
import Modal from '@/js/components/Modal.vue'
import upperFirst from 'lodash/upperFirst'

export default {
name: 'ResourceShow',
Expand Down Expand Up @@ -107,20 +108,26 @@ export default {
return action
},
fields() {
if (!this.resource) return []

return this.resource.fields
},
panels() {
if (!this.resource) return []
if (!this.resource) return [{ name: this.$t('avo.resource_details', { name: upperFirst(this.resourceNameFromURL) }) }]

return this.resource.panels
},
hasManyRelations() {
return this.fields.filter((field) => ['has_and_belongs_to_many', 'has_many'].indexOf(field.relationship) > -1)
},
canEdit() {
if (!this.resource) return false

return this.resource.authorization.edit
},
canDelete() {
if (!this.resource) return false

return this.resource.authorization.destroy
},
},
Expand Down
14 changes: 11 additions & 3 deletions lib/avo/app/resource.rb
Expand Up @@ -13,7 +13,15 @@ class Resource

class << self
def hydrate_resource(model:, resource:, view: :index, user:)
default_panel_name = I18n.t 'avo.resource_details', name: resource.name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[rubocop] reported by reviewdog 🐶
Layout/EmptyLinesAroundMethodBody: Extra empty line detected at method body beginning.

case view
when :show
panel_name = I18n.t 'avo.resource_details', name: resource.name.downcase.upcase_first
when :edit
panel_name = I18n.t('avo.edit_item', item: resource.name.downcase).upcase_first
when :create
panel_name = I18n.t('avo.create_new_item', item: resource.name.downcase).upcase_first
end

resource_with_fields = {
id: model.id,
Expand All @@ -26,7 +34,7 @@ def hydrate_resource(model:, resource:, view: :index, user:)
fields: [],
grid_fields: {},
panels: [{
name: default_panel_name,
name: panel_name,
component: 'panel',
}]
}
Expand All @@ -46,7 +54,7 @@ def hydrate_resource(model:, resource:, view: :index, user:)

next if furnished_field.blank?

furnished_field[:panel_name] = default_panel_name
furnished_field[:panel_name] = panel_name
furnished_field[:show_on_show] = field.show_on_show

if field.has_own_panel?
Expand Down