Skip to content

Commit

Permalink
feat: edit life events (monicahq/chandler#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored May 8, 2023
1 parent 2324f87 commit 123d532
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function execute(array $data): LifeEvent
$this->data = $data;
$this->validate();
$this->update();
$this->dissociateParticipants();
$this->associateParticipants();
$this->updateLastEditedDate();

Expand Down Expand Up @@ -131,6 +132,11 @@ private function updateLastEditedDate(): void
}
}

private function dissociateParticipants(): void
{
$this->lifeEvent->participants()->detach();
}

private function associateParticipants(): void
{
foreach ($this->partipantsCollection as $participant) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Domains\Contact\ManageLifeEvents\Services\CreateLifeEvent;
use App\Domains\Contact\ManageLifeEvents\Services\DestroyLifeEvent;
use App\Domains\Contact\ManageLifeEvents\Services\UpdateLifeEvent;
use App\Domains\Contact\ManageLifeEvents\Web\ViewHelpers\ModuleLifeEventViewHelper;
use App\Http\Controllers\Controller;
use App\Models\Contact;
Expand Down Expand Up @@ -56,6 +57,50 @@ public function store(Request $request, string $vaultId, string $contactId, int
], 201);
}

public function edit(Request $request, string $vaultId, string $contactId, int $timelineEventId, int $lifeEventId)
{
// we need to add the current contact to the list of participants
// finally, just so we are sure that we don't have the same participant
// twice in the list, we need to remove duplicates
$participants = collect($request->input('participants'))
->push(['id' => $contactId])
->unique('id')
->pluck('id')
->toArray();

$carbonDate = Carbon::parse($request->input('started_at'));

$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::id(),
'vault_id' => $vaultId,
'timeline_event_id' => $timelineEventId,
'life_event_id' => $lifeEventId,
'label' => $request->input('label'),
'life_event_type_id' => $request->input('lifeEventTypeId'),
'summary' => $request->input('summary'),
'description' => $request->input('description'),
'happened_at' => $carbonDate->format('Y-m-d'),
'costs' => $request->input('costs'),
'currency_id' => $request->input('currency_id'),
'paid_by_contact_id' => $request->input('paid_by_contact_id'),
'duration_in_minutes' => $request->input('duration_in_minutes'),
'distance' => $request->input('distance'),
'distance_unit' => $request->input('distance_unit'),
'from_place' => $request->input('from_place'),
'to_place' => $request->input('to_place'),
'place' => $request->input('place'),
'participant_ids' => $participants,
];

$lifeEvent = (new UpdateLifeEvent())->execute($data);
$contact = Contact::find($contactId);

return response()->json([
'data' => ModuleLifeEventViewHelper::dtoLifeEvent($lifeEvent, Auth::user(), $contact),
]);
}

public function destroy(Request $request, string $vaultId, string $contactId, int $timelineEventId, int $lifeEventId)
{
$data = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public static function dtoLifeEvent(LifeEvent $lifeEvent, User $user, Contact $c
'summary' => $lifeEvent->summary,
'description' => $lifeEvent->description,
'happened_at' => DateHelper::format($lifeEvent->happened_at, $user),
'started_at' => $lifeEvent->happened_at->format('Y-m-d'),
'costs' => $lifeEvent->costs,
'currency_id' => $lifeEvent->currency_id,
'paid_by_contact_id' => $lifeEvent->paid_by_contact_id,
Expand Down Expand Up @@ -138,6 +139,12 @@ public static function dtoLifeEvent(LifeEvent $lifeEvent, User $user, Contact $c
'timelineEvent' => $lifeEvent->timelineEvent->id,
'lifeEvent' => $lifeEvent->id,
]),
'edit' => route('contact.life_event.edit', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
'timelineEvent' => $lifeEvent->timelineEvent->id,
'lifeEvent' => $lifeEvent->id,
]),
'destroy' => route('contact.life_event.destroy', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
Expand Down
1 change: 1 addition & 0 deletions lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@
"Type": "Typ",
"Type:": "Typ:",
"Type anything in the conversation with the Monica bot. It can be `start` for instance.": "Geben Sie etwas in das Gespräch mit dem Monica-Bot ein. Es kann zum Beispiel starten.",
"Types": "Typen",
"Type something": "Geben Sie etwas ein",
"Unarchive contact": "Kontakt wiederherstellen",
"unarchived the contact": "hat den Kontakt wiederhergestellt",
Expand Down
1 change: 1 addition & 0 deletions lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@
"Type": "Tipo",
"Type:": "Tipo:",
"Type anything in the conversation with the Monica bot. It can be `start` for instance.": "Escribe cualquier cosa en la conversación con el bot de Mónica. Puede ser start, por ejemplo.",
"Types": "Tipos",
"Type something": "Escribe algo",
"Unarchive contact": "Desarchivar contacto",
"unarchived the contact": "desarchivó el contacto",
Expand Down
1 change: 1 addition & 0 deletions lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@
"Type": "Type",
"Type:": "Type :",
"Type anything in the conversation with the Monica bot. It can be `start` for instance.": "Tapez n’importe quoi dans la conversation avec le bot Monica. Cela peut être `start` par exemple.",
"Types": "Types",
"Type something": "Tapez quelque chose",
"Unarchive contact": "Désarchiver le contact",
"unarchived the contact": "a désarchivé le contact",
Expand Down
1 change: 1 addition & 0 deletions lang/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@
"Type": "Tipo",
"Type:": "Tipo:",
"Type anything in the conversation with the Monica bot. It can be `start` for instance.": "Digita qualsiasi cosa nella conversazione con il bot di Monica. Puoi scrivere 'start' per esempio.",
"Types": "Tipi",
"Type something": "Digita qualcosa",
"Unarchive contact": "Annulla archiviazione contatto",
"unarchived the contact": "ha disarchiviato il contatto",
Expand Down
1 change: 1 addition & 0 deletions lang/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@
"Type": "Tipo",
"Type:": "Tipo:",
"Type anything in the conversation with the Monica bot. It can be `start` for instance.": "Digite qualquer coisa na conversa com o bot do Monica. Pode ser start, por exemplo.",
"Types": "Tipos",
"Type something": "Digite algo",
"Unarchive contact": "Desarquivar contato",
"unarchived the contact": "desarquivou o contato",
Expand Down
1 change: 1 addition & 0 deletions lang/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@
"Type": "Тип",
"Type:": "Тип:",
"Type anything in the conversation with the Monica bot. It can be `start` for instance.": "Введите что-нибудь в разговоре с ботом Моника. Например, это может быть слово `start`.",
"Types": "Типы",
"Type something": "Введите что-нибудь",
"Unarchive contact": "Разархивировать контакт",
"unarchived the contact": "разархивировал контакт",
Expand Down
54 changes: 41 additions & 13 deletions resources/js/Shared/Modules/CreateLifeEvent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const props = defineProps({
openModal: Boolean,
createTimelineEvent: Boolean,
timelineEvent: Object,
lifeEvent: Object,
});
const emit = defineEmits(['closeModal', 'timelineEventCreated', 'lifeEventCreated']);
Expand Down Expand Up @@ -50,6 +51,28 @@ watch(
onMounted(() => {
resetModal();
if (props.lifeEvent) {
form.label = props.lifeEvent.label;
form.started_at = props.lifeEvent.started_at;
form.lifeEventTypeId = props.lifeEvent.life_event_type.id;
selectedLifeEventCategory.value = props.lifeEvent.life_event_type.category;
selectedLifeEventType.value = props.lifeEvent.life_event_type;
if (props.lifeEvent.summary) {
form.summary = props.lifeEvent.summary;
addSummaryFieldShown.value = true;
}
if (props.lifeEvent.description) {
form.description = props.lifeEvent.description;
addDescriptionFieldShown.value = true;
}
if (props.lifeEvent.distance) {
form.distance = props.lifeEvent.distance;
form.distance_unit = props.lifeEvent.distance_unit;
addDistanceFieldShown.value = true;
}
form.participants = props.lifeEvent.participants;
selectedLifeEventType.value = props.lifeEvent.life_event_type;
}
});
const resetModal = () => {
Expand Down Expand Up @@ -115,15 +138,19 @@ const store = () => {
// this changes the url we post to as we need to pass the right info back to
// the parent (ie. if it needs to refresh a specific timeline event, or the entire
// timeline)
var url = '';
if (props.createTimelineEvent) {
url = props.data.url.store;
let request = {
method: 'post',
data: form,
};
if (props.lifeEvent) {
request.url = props.lifeEvent.url.edit;
request.method = 'put';
} else {
url = props.timelineEvent.url.store;
request.url = props.createTimelineEvent ? props.data.url.store : props.timelineEvent.url.store;
}
axios
.post(url, form)
.request(request)
.then((response) => {
loadingState.value = '';
emit('closeModal');
Expand Down Expand Up @@ -186,7 +213,7 @@ const store = () => {

<!-- list of life event types -->
<div>
<p class="mb-1 text-xs font-semibold">Types</p>
<p class="mb-1 text-xs font-semibold">{{ $t('Types') }}</p>
<ul class="rounded-lg border border-gray-200 bg-white dark:border-gray-700 dark:bg-gray-900">
<li
v-for="lifeEventType in selectedLifeEventCategory.life_event_types"
Expand All @@ -205,13 +232,13 @@ const store = () => {
<div v-else class="flex items-center justify-between border-b border-gray-200 p-3 dark:border-gray-700">
<div>
<span class="text-sm">{{ $t('Chosen type:') }}</span>
<span class="rounded border bg-white px-2 py-1 font-mono text-sm dark:bg-gray-800">{{
selectedLifeEventCategory.label
}}</span>
<span class="rounded border bg-white px-2 py-1 font-mono text-sm dark:bg-gray-800">
{{ selectedLifeEventCategory.label }}
</span>
>
<span class="rounded border bg-white px-2 py-1 font-mono text-sm dark:bg-gray-800">{{
selectedLifeEventType.label
}}</span>
<span class="rounded border bg-white px-2 py-1 font-mono text-sm dark:bg-gray-800">
{{ selectedLifeEventType.label }}
</span>
</div>

<p @click="resetType()" class="cursor-pointer text-sm text-blue-500 hover:underline">{{ $t('Change') }}</p>
Expand All @@ -222,7 +249,8 @@ const store = () => {
<!-- default date -->
<div v-if="!editDate" class="flex items-center justify-between">
<div>
<span class="text-sm">{{ $t('Date of the event:') }}</span> {{ props.data.current_date_human_format }}
<span class="text-sm">{{ $t('Date of the event:') }}</span>
{{ props.lifeEvent ? props.lifeEvent.happened_at : props.data.current_date_human_format }}
</div>

<p @click="editDate = true" class="cursor-pointer text-sm text-blue-500 hover:underline">
Expand Down
Loading

0 comments on commit 123d532

Please sign in to comment.