forked from SpeciesFileGroup/taxonworks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code refactor, replace v-hotkey plugin
- Loading branch information
Showing
11 changed files
with
691 additions
and
778 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
app/javascript/vue/tasks/extracts/new_extract/components/Navbar.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 89 additions & 84 deletions
173
app/javascript/vue/tasks/nomenclature/new_combination/components/saveCombination.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,118 @@ | ||
<template> | ||
<button | ||
type="button" | ||
ref="saveButton" | ||
ref="saveButtonRef" | ||
class="button normal-input button-submit create-new-combination" | ||
v-hotkey="shortcuts" | ||
:disabled="!validateCreate()" | ||
@click="save()" | ||
> | ||
{{ newCombination.hasOwnProperty('id') ? 'Update' : 'Create' }} | ||
</button> | ||
</template> | ||
<script> | ||
|
||
<script setup> | ||
import { ref } from 'vue' | ||
import { Combination } from '@/routes/endpoints' | ||
import { EXTEND_PARAMS } from '../constants/extend.js' | ||
import platformKey from '@/helpers/getPlatformKey' | ||
import useHotkey from 'vue3-hotkey' | ||
const props = defineProps({ | ||
newCombination: { | ||
type: Object, | ||
required: true | ||
} | ||
}) | ||
export default { | ||
props: { | ||
newCombination: { | ||
type: Object, | ||
required: true | ||
const emit = defineEmits(['processing', 'save', 'success']) | ||
const saveButtonRef = ref() | ||
const shortcuts = ref([ | ||
{ | ||
keys: [platformKey(), 's'], | ||
handler() { | ||
save() | ||
} | ||
}, | ||
} | ||
]) | ||
emits: ['processing', 'save', 'success'], | ||
useHotkey(shortcuts.value) | ||
computed: { | ||
shortcuts() { | ||
const keys = {} | ||
keys[`${platformKey()}+s`] = this.save | ||
function validateCreate() { | ||
return props.newCombination.protonyms.genus | ||
} | ||
return keys | ||
} | ||
}, | ||
function setFocus() { | ||
if (validateCreate()) { | ||
saveButtonRef.value.focus() | ||
} | ||
} | ||
methods: { | ||
validateCreate() { | ||
return this.newCombination.protonyms.genus | ||
}, | ||
function save() { | ||
if (validateCreate()) { | ||
props.newCombination?.id ? update(props.newCombination.id) : create() | ||
} | ||
} | ||
setFocus() { | ||
if (this.validateCreate()) { | ||
this.$refs.saveButton.focus() | ||
} | ||
}, | ||
function createRecordCombination() { | ||
const keys = Object.keys(props.newCombination.protonyms) | ||
const combination = { | ||
verbatim_name: props.newCombination.verbatim_name, | ||
origin_citation_attributes: props.newCombination?.origin_citation_attributes | ||
} | ||
save() { | ||
if (this.validateCreate()) { | ||
this.newCombination.hasOwnProperty('id') | ||
? this.update(this.newCombination.id) | ||
: this.create() | ||
} | ||
}, | ||
keys.forEach((rank) => { | ||
if (props.newCombination.protonyms[rank]) { | ||
combination[`${rank}_id`] = props.newCombination.protonyms[rank].id | ||
} | ||
}) | ||
createRecordCombination() { | ||
const keys = Object.keys(this.newCombination.protonyms) | ||
const combination = { | ||
verbatim_name: this.newCombination.verbatim_name, | ||
origin_citation_attributes: | ||
this.newCombination?.origin_citation_attributes | ||
} | ||
return combination | ||
} | ||
keys.forEach((rank) => { | ||
if (this.newCombination.protonyms[rank]) { | ||
combination[`${rank}_id`] = this.newCombination.protonyms[rank].id | ||
} | ||
}) | ||
function create() { | ||
emit('processing', true) | ||
return combination | ||
}, | ||
create() { | ||
this.$emit('processing', true) | ||
Combination.create({ | ||
combination: this.createRecordCombination(), | ||
...EXTEND_PARAMS | ||
}).then( | ||
(response) => { | ||
this.$emit('save', response.body) | ||
this.$emit('processing', false) | ||
this.$emit('success', true) | ||
TW.workbench.alert.create( | ||
'New combination was successfully created.', | ||
'notice' | ||
) | ||
}, | ||
(response) => { | ||
this.$emit('processing', false) | ||
TW.workbench.alert.create( | ||
`Something went wrong: ${JSON.stringify(response.body)}`, | ||
'error' | ||
) | ||
} | ||
Combination.create({ | ||
combination: createRecordCombination(), | ||
...EXTEND_PARAMS | ||
}).then( | ||
({ body }) => { | ||
emit('save', body) | ||
emit('processing', false) | ||
emit('success', true) | ||
TW.workbench.alert.create( | ||
'New combination was successfully created.', | ||
'notice' | ||
) | ||
}, | ||
update(id) { | ||
this.$emit('processing', true) | ||
Combination.update(id, { | ||
combination: this.createRecordCombination(), | ||
...EXTEND_PARAMS | ||
}).then((response) => { | ||
this.$emit('save', response.body) | ||
this.$emit('processing', false) | ||
this.$emit('success', true) | ||
TW.workbench.alert.create( | ||
'New combination was successfully updated.', | ||
'notice' | ||
) | ||
}) | ||
({ body }) => { | ||
emit('processing', false) | ||
TW.workbench.alert.create( | ||
`Something went wrong: ${JSON.stringify(body)}`, | ||
'error' | ||
) | ||
} | ||
} | ||
) | ||
} | ||
function update(id) { | ||
emit('processing', true) | ||
Combination.update(id, { | ||
combination: createRecordCombination(), | ||
...EXTEND_PARAMS | ||
}).then(({ body }) => { | ||
emit('save', body) | ||
emit('processing', false) | ||
emit('success', true) | ||
TW.workbench.alert.create( | ||
'New combination was successfully updated.', | ||
'notice' | ||
) | ||
}) | ||
} | ||
defineExpose({ | ||
setFocus | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.