-
-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Jack Sleight <jacksleight@gmail.com>
- Loading branch information
1 parent
63be821
commit 7ad5877
Showing
6 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
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
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
152 changes: 152 additions & 0 deletions
152
resources/js/components/field-actions/FieldActionModal.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 |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<template> | ||
|
||
<div> | ||
<confirmation-modal | ||
:title="title" | ||
:danger="dangerous" | ||
:buttonText="buttonText" | ||
:busy="resolving || processing" | ||
@confirm="confirm" | ||
@cancel="cancel" | ||
> | ||
<div class="min-h-20"> | ||
|
||
<div v-if="bodyText" v-text="bodyText" :class="{ 'mb-4': warningText || hasFields }" /> | ||
|
||
<div v-if="warningText" v-text="warningText" class="text-red-500" :class="{ 'mb-4': hasFields }" /> | ||
|
||
<publish-container | ||
v-if="hasFields && !resolving" | ||
:name="containerName" | ||
:blueprint="blueprint" | ||
:values="values" | ||
:meta="meta" | ||
:errors="errors" | ||
@updated="values = $event" | ||
> | ||
<publish-fields | ||
slot-scope="{ setFieldValue, setFieldMeta }" | ||
:fields="blueprint.tabs[0].fields" | ||
@updated="setFieldValue" | ||
@meta-updated="setFieldMeta" | ||
/> | ||
</publish-container> | ||
|
||
</div> | ||
</confirmation-modal> | ||
</div> | ||
|
||
</template> | ||
|
||
<script> | ||
import uniqid from "uniqid"; | ||
export default { | ||
props: { | ||
fields: { | ||
type: Object, | ||
}, | ||
title: { | ||
type: String, | ||
}, | ||
buttonText: { | ||
type: String, | ||
}, | ||
text: { | ||
type: String, | ||
}, | ||
warningText: { | ||
type: String, | ||
}, | ||
dangerous: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
resolving: this.hasFields, | ||
processing: false, | ||
blueprint: [], | ||
values: {}, | ||
meta: {}, | ||
error: null, | ||
errors: {}, | ||
bodyText: null, | ||
containerName: `field-action-modal-${uniqid()}`, | ||
} | ||
}, | ||
mounted() { | ||
this.bodyText = this.initializeBodyText(); | ||
this.initialize(); | ||
}, | ||
computed: { | ||
hasFields() { | ||
return this.fields && Object.keys(this.fields).length > 0; | ||
} | ||
}, | ||
methods: { | ||
initialize() { | ||
if (!this.hasFields) { | ||
return; | ||
} | ||
this.resolving = true; | ||
this.$axios.post(cp_url(`field-action-modal/resolve`), { | ||
fields: this.fields, | ||
}).then(response => { | ||
this.blueprint = { tabs: [{ fields: response.data.fields }]}; | ||
this.values = response.data.values; | ||
this.meta = response.data.meta; | ||
this.resolving = false; | ||
}); | ||
}, | ||
confirm() { | ||
if (!this.hasFields) { | ||
this.$emit('confirm'); | ||
return; | ||
} | ||
this.processing = true; | ||
this.$axios.post(cp_url('field-action-modal/process'), { | ||
fields: this.fields, | ||
values: this.values, | ||
}).then(response => { | ||
this.$emit('confirm', response.data); | ||
}).catch(e => { | ||
if (e.response && e.response.status === 422) { | ||
const { message, errors } = e.response.data; | ||
this.error = message; | ||
this.errors = errors; | ||
this.$toast.error(message); | ||
} else if (e.response) { | ||
this.$toast.error(e.response.data.message); | ||
} else { | ||
this.$toast.error(__('Something went wrong')); | ||
} | ||
}).finally(() => this.processing = false); | ||
}, | ||
cancel() { | ||
this.$emit('cancel') | ||
}, | ||
initializeBodyText() { | ||
if (this.text) return this.text; | ||
if (this.warningText || this.hasFields) return null; | ||
return __('Are you sure?'); | ||
} | ||
}, | ||
} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default function(props) { | ||
return new Promise((resolve) => { | ||
const component = Statamic.$components.append('field-action-modal', { props }); | ||
const close = () => Statamic.$components.destroy(component.id); | ||
|
||
component.on('confirm', (data = {}) => { | ||
resolve({ ...data, confirmed: true }); | ||
close(); | ||
}); | ||
|
||
component.on('cancel', () => { | ||
resolve({ confirmed: false }); | ||
close(); | ||
}); | ||
}); | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Statamic\Http\Controllers\CP; | ||
|
||
use Illuminate\Http\Request; | ||
use Statamic\Fields\Fields; | ||
|
||
class FieldActionModalController extends CpController | ||
{ | ||
public function resolve(Request $request) | ||
{ | ||
$fields = $this | ||
->getFields($request->fields) | ||
->preProcess(); | ||
|
||
return [ | ||
'fields' => $fields->toPublishArray(), | ||
'values' => $fields->values(), | ||
'meta' => $fields->meta(), | ||
]; | ||
} | ||
|
||
public function process(Request $request) | ||
{ | ||
$fields = $this | ||
->getFields($request->fields) | ||
->addValues($request->values); | ||
|
||
$fields->validate(); | ||
|
||
$processed = $fields->process()->values(); | ||
|
||
$fields->preProcess(); | ||
|
||
return [ | ||
'values' => $fields->values(), | ||
'meta' => $fields->meta(), | ||
'processed' => $processed, | ||
]; | ||
} | ||
|
||
private function getFields($fieldItems) | ||
{ | ||
return new Fields( | ||
collect($fieldItems)->map(fn ($field, $handle) => compact('handle', 'field')) | ||
); | ||
} | ||
} |