Skip to content

Commit 7952aa7

Browse files
author
Andres Vargas
committed
Create Contact
1 parent 1dcfc44 commit 7952aa7

File tree

5 files changed

+107
-3
lines changed

5 files changed

+107
-3
lines changed

demo/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class Contact(EntityMix):
3434

3535
class Meta:
3636
ordering = ("first_name", "last_name")
37-
37+
@property
38+
def name(self):
39+
return f"{self.first_name} {self.last_name}"
3840
def __str__(self):
3941
return f"{self.first_name} {self.last_name}"
4042

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<template>
2+
<div>
3+
<h1 class="mb-8 font-bold text-3xl">
4+
<inertia-link class="text-indigo-400 hover:text-indigo-600" :href="route('demo:contacts')">Contacts</inertia-link>
5+
<span class="text-indigo-400 font-medium">/</span> Create
6+
</h1>
7+
<div class="bg-white rounded shadow overflow-hidden max-w-3xl">
8+
<form @submit.prevent="submit">
9+
<div class="p-8 -mr-6 -mb-8 flex flex-wrap">
10+
<text-input v-model="form.first_name" :errors="$page.errors.first_name" class="pr-6 pb-8 w-full lg:w-1/2" label="First name" />
11+
<text-input v-model="form.last_name" :errors="$page.errors.last_name" class="pr-6 pb-8 w-full lg:w-1/2" label="Last name" />
12+
<select-input v-model="form.organization_id" :errors="$page.errors.organization_id" class="pr-6 pb-8 w-full lg:w-1/2" label="Organization">
13+
<option :value="null" />
14+
<option v-for="organization in organizations" :key="organization.id" :value="organization.id">{{ organization.name }}</option>
15+
</select-input>
16+
<text-input v-model="form.email" :errors="$page.errors.email" class="pr-6 pb-8 w-full lg:w-1/2" label="Email" />
17+
<text-input v-model="form.phone" :errors="$page.errors.phone" class="pr-6 pb-8 w-full lg:w-1/2" label="Phone" />
18+
<text-input v-model="form.address" :errors="$page.errors.address" class="pr-6 pb-8 w-full lg:w-1/2" label="Address" />
19+
<text-input v-model="form.city" :errors="$page.errors.city" class="pr-6 pb-8 w-full lg:w-1/2" label="City" />
20+
<text-input v-model="form.region" :errors="$page.errors.region" class="pr-6 pb-8 w-full lg:w-1/2" label="Province/State" />
21+
<select-input v-model="form.country" :errors="$page.errors.country" class="pr-6 pb-8 w-full lg:w-1/2" label="Country">
22+
<option :value="null" />
23+
<option value="CA">Canada</option>
24+
<option value="US">United States</option>
25+
</select-input>
26+
<text-input v-model="form.postal_code" :errors="$page.errors.postal_code" class="pr-6 pb-8 w-full lg:w-1/2" label="Postal code" />
27+
</div>
28+
<div class="px-8 py-4 bg-gray-100 border-t border-gray-200 flex justify-end items-center">
29+
<loading-button :loading="sending" class="btn-indigo" type="submit">Create Contact</loading-button>
30+
</div>
31+
</form>
32+
</div>
33+
</div>
34+
</template>
35+
36+
<script>
37+
import Layout from '../Shared/Layout'
38+
import LoadingButton from '../Shared/LoadingButton'
39+
import SelectInput from '../Shared/SelectInput'
40+
import TextInput from '../Shared/TextInput'
41+
42+
export default {
43+
metaInfo: { title: 'Create Contact' },
44+
layout: Layout,
45+
components: {
46+
LoadingButton,
47+
SelectInput,
48+
TextInput,
49+
},
50+
props: {
51+
organizations: Array,
52+
},
53+
remember: 'form',
54+
data() {
55+
return {
56+
sending: false,
57+
form: {
58+
first_name: null,
59+
last_name: null,
60+
organization_id: null,
61+
email: null,
62+
phone: null,
63+
address: null,
64+
city: null,
65+
region: null,
66+
country: null,
67+
postal_code: null,
68+
},
69+
}
70+
},
71+
methods: {
72+
submit() {
73+
this.sending = true
74+
this.$inertia.post(this.route('demo:contacts.create'), this.form)
75+
.then(() => this.sending = false)
76+
},
77+
},
78+
}
79+
</script>

demo/static/src/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ import Contacts from "./Pages/Contacts";
2323
import Organization from "./Pages/Organizations";
2424
import ContactEdit from "./Pages/Contacts.Edit";
2525
import OrganizationEdit from "./Pages/Organizations.Edit";
26-
26+
import ContactCreate from "./Pages/Contacts.Create";
2727
const pages = {
2828
'Index': Index,
2929
'Contacts': Contacts,
3030
'Contacts.Edit': ContactEdit,
31+
'Contacts.Create': ContactCreate,
3132
'Organizations': Organization,
3233
"Organizations.Edit": OrganizationEdit,
34+
3335
}
3436

3537
new Vue({

demo/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.urls import path
33
from demo.views import index , contacts,organizations
44
from demo.views import contact_edit, organization_edit
5+
from demo.views import contact_create
56

67
app_name="demo"
78

@@ -12,7 +13,7 @@
1213
path('users/logout', index, name='logout'),
1314
path('users/edit/<int:id>', index, name='users.edit'),
1415
path('contacts/edit/<int:id>', contact_edit, name='contacts.edit'),
15-
path('contacts/create/', index, name='contacts.create'),
16+
path('contacts/create/', contact_create, name='contacts.create'),
1617
path('contacts', contacts, name='contacts'),
1718
path('organizations/edit/<int:id>', organization_edit, name='organizations.edit'),
1819
path('organizations/create/', organizations, name='organizations.create'),

demo/views.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ def contact_edit(request, id):
9292
}
9393
return render_inertia(request, "Contacts.Edit", props)
9494

95+
def contact_create(request):
96+
contact_schema = ContactSchema()
97+
org_schema = OrganizationSchema(many=True, only=("id","name"))
98+
orgs = Organization.objects.all()
99+
100+
if request.method == "POST":
101+
try:
102+
c = contact_schema.loads(request.body, unknown=INCLUDE)
103+
obj = Contact.objects.create(**c)
104+
except ValidationError as err:
105+
share_flash(request, error="Exists errors on form")
106+
print(err)
107+
share_flash(request, errors= err.messages)
108+
else:
109+
share_flash(request, success=f"Contact {obj.name} created")
110+
return redirect(reverse("demo:contacts"))
111+
props = {
112+
'organizations': org_schema.dump(orgs)
113+
}
114+
return render_inertia(request, "Contacts.Create", props)
95115

96116

97117
def index(request):

0 commit comments

Comments
 (0)