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

Add save as copy to module and chart resources #1468

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions client/web/compose/src/views/Admin/Charts/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,11 @@
:processing-delete="processingDelete"
:hide-delete="hideDelete"
:hide-save="hideSave"
hide-clone
:hide-clone="!isEdit"
:disable-save="disableSave"
@delete="handleDelete()"
@save="handleSave()"
@clone="handleClone()"
@saveAndClose="handleSave({ closeOnSuccess: true })"
@back="$router.push(previousPage || { name: 'admin.charts' })"
/>
Expand Down Expand Up @@ -677,6 +678,7 @@ export default {

if (chartID === NoID) {
let c = new compose.Chart({ namespaceID: this.namespace.namespaceID })

switch (this.category) {
case 'gauge':
c = new compose.GaugeChart(c)
Expand Down Expand Up @@ -762,7 +764,7 @@ export default {
this.processing = false
},

handleSave ({ closeOnSuccess = false } = {}) {
handleSave ({ closeOnSuccess = false, chart = this.chart } = {}) {
this.processing = true

if (closeOnSuccess) {
Expand All @@ -777,9 +779,9 @@ export default {
*/
const resourceTranslationLanguage = this.currentLanguage

const c = Object.assign({}, this.chart, resourceTranslationLanguage)
const c = Object.assign({}, chart, resourceTranslationLanguage)

if (this.chart.chartID === NoID) {
if (chart.chartID === NoID) {
this.createChart(c).then(({ chartID }) => {
this.toastSuccess(this.$t('notification:chart.saved'))
this.initialChartState = cloneDeep(chartConstructor(this.chart))
Expand Down Expand Up @@ -836,6 +838,15 @@ export default {
})
},

handleClone () {
const chart = this.chart.clone()
chart.chartID = NoID
chart.name = `${this.chart.name} (copy)`
chart.handle = this.chart.handle ? `${this.chart.handle}_copy` : ''

this.handleSave({ chart })
},

redirect () {
this.$router.push(this.previousPage || { name: 'admin.charts' })
},
Expand Down
23 changes: 17 additions & 6 deletions client/web/compose/src/views/Admin/Modules/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,13 @@
:processing-save-and-close="processingSaveAndClose"
:processing-delete="processingDelete"
:hide-delete="hideDelete"
hide-clone
:hide-clone="!isEdit"
:hide-save="hideSave"
:disable-save="disableSave"
@delete="handleDelete"
@save="handleSave()"
@saveAndClose="handleSave({ closeOnSuccess: true })"
@clone="handleClone"
@back="$router.push(previousPage || { name: 'admin.modules' })"
/>
</portal>
Expand Down Expand Up @@ -667,6 +668,7 @@ export default {
{ fields: [new compose.ModuleFieldString({ fieldID: NoID, name: this.$t('general.placeholder.sample') })] },
this.namespace,
)

this.initialModuleState = this.module.clone()
} else {
const params = {
Expand Down Expand Up @@ -770,7 +772,7 @@ export default {
this.module.config = { ...this.module.config, ...changes }
},

handleSave ({ closeOnSuccess = false } = {}) {
handleSave ({ closeOnSuccess = false, module = this.module } = {}) {
/**
* Pass a special tag alongside payload that
* instructs store layer to add content-language header to the API request
Expand All @@ -784,11 +786,11 @@ export default {
this.processingSave = true
}

if (!this.isEdit) {
if (module.moduleID === NoID) {
// Filter out record fields that reference this not yet created module
let fields = []
const toBeUpdatedFields = []
this.module.fields.forEach(f => {
module.fields.forEach(f => {
if (f.kind === 'Record' && f.options.moduleID === '-1') {
toBeUpdatedFields.push(f)
} else {
Expand All @@ -798,7 +800,7 @@ export default {

// If such fields exist , after module is created add fields, map moduleID and update module
// Unfortunately this ruins the initial field order, but we can improve this later
this.createModule({ ...this.module, fields, resourceTranslationLanguage }).then(async module => {
this.createModule({ ...module, fields, resourceTranslationLanguage }).then(async module => {
if (toBeUpdatedFields.length) {
fields = [
...module.fields,
Expand Down Expand Up @@ -831,7 +833,7 @@ export default {
}
})
} else {
this.updateModule({ ...this.module, resourceTranslationLanguage }).then(module => {
this.updateModule({ ...module, resourceTranslationLanguage }).then(module => {
this.module = new compose.Module({ ...module }, this.namespace)
this.initialModuleState = this.module.clone()

Expand Down Expand Up @@ -873,6 +875,15 @@ export default {
})
},

handleClone () {
const module = this.module.clone()
module.moduleID = NoID
module.name = `${this.module.name} (copy)`
module.handle = this.module.handle ? `${this.module.handle}_copy` : ''

this.handleSave({ module })
},

async fetchConnection (connectionID) {
if (connectionID && connectionID !== NoID) {
this.$SystemAPI.dalConnectionRead({ connectionID })
Expand Down
4 changes: 4 additions & 0 deletions lib/js/src/compose/types/chart/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ export class BaseChart {
get resourceType (): string {
return 'compose:chart'
}

clone (): BaseChart {
return new BaseChart(JSON.parse(JSON.stringify(this)))
}
}

export { chartUtil } from './util'