Skip to content

Commit b8ce494

Browse files
author
Andres Vargas
committed
Organization CRUD
1 parent 38f4497 commit b8ce494

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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:organizations')">Organizations</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.name" :errors="$page.errors.name" class="pr-6 pb-8 w-full lg:w-1/2" label="Name" />
11+
<text-input v-model="form.email" :errors="$page.errors.email" class="pr-6 pb-8 w-full lg:w-1/2" label="Email" />
12+
<text-input v-model="form.phone" :errors="$page.errors.phone" class="pr-6 pb-8 w-full lg:w-1/2" label="Phone" />
13+
<text-input v-model="form.address" :errors="$page.errors.address" class="pr-6 pb-8 w-full lg:w-1/2" label="Address" />
14+
<text-input v-model="form.city" :errors="$page.errors.city" class="pr-6 pb-8 w-full lg:w-1/2" label="City" />
15+
<text-input v-model="form.region" :errors="$page.errors.region" class="pr-6 pb-8 w-full lg:w-1/2" label="Province/State" />
16+
<select-input v-model="form.country" :errors="$page.errors.country" class="pr-6 pb-8 w-full lg:w-1/2" label="Country">
17+
<option :value="null" />
18+
<option value="CA">Canada</option>
19+
<option value="US">United States</option>
20+
</select-input>
21+
<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" />
22+
</div>
23+
<div class="px-8 py-4 bg-gray-100 border-t border-gray-200 flex justify-end items-center">
24+
<loading-button :loading="sending" class="btn-indigo" type="submit">Create Organization</loading-button>
25+
</div>
26+
</form>
27+
</div>
28+
</div>
29+
</template>
30+
31+
<script>
32+
import Layout from '../Shared/Layout'
33+
import LoadingButton from '../Shared/LoadingButton'
34+
import SelectInput from '../Shared/SelectInput'
35+
import TextInput from '../Shared/TextInput'
36+
37+
export default {
38+
metaInfo: { title: 'Create Organization' },
39+
layout: Layout,
40+
components: {
41+
LoadingButton,
42+
SelectInput,
43+
TextInput,
44+
},
45+
remember: 'form',
46+
data() {
47+
return {
48+
sending: false,
49+
form: {
50+
name: null,
51+
email: null,
52+
phone: null,
53+
address: null,
54+
city: null,
55+
region: null,
56+
country: null,
57+
postal_code: null,
58+
},
59+
}
60+
},
61+
methods: {
62+
submit() {
63+
this.sending = true
64+
this.$inertia.post(this.route('demo:organizations.create'), this.form)
65+
.then(() => this.sending = false)
66+
},
67+
},
68+
}
69+
</script>

demo/static/src/app.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ import Organization from "./Pages/Organizations";
2424
import ContactEdit from "./Pages/Contacts.Edit";
2525
import OrganizationEdit from "./Pages/Organizations.Edit";
2626
import ContactCreate from "./Pages/Contacts.Create";
27+
import OrganizationCreate from "./Pages/Organizations.Create";
28+
29+
2730
const pages = {
2831
'Index': Index,
2932
'Contacts': Contacts,
3033
'Contacts.Edit': ContactEdit,
3134
'Contacts.Create': ContactCreate,
3235
'Organizations': Organization,
3336
"Organizations.Edit": OrganizationEdit,
34-
37+
"Organizations.Create": OrganizationCreate
3538
}
3639

40+
3741
new Vue({
3842
render: h => h(InertiaApp, {
3943
props: {

demo/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +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
5+
from demo.views import contact_create, organization_create
66

77
app_name="demo"
88

@@ -16,7 +16,7 @@
1616
path('contacts/create/', contact_create, name='contacts.create'),
1717
path('contacts', contacts, name='contacts'),
1818
path('organizations/edit/<int:id>', organization_edit, name='organizations.edit'),
19-
path('organizations/create/', organizations, name='organizations.create'),
19+
path('organizations/create/', organization_create, name='organizations.create'),
2020
path('organizations', organizations, name='organizations'),
2121
path('dashboard', index, name='dashboard'),
2222
path('', index, name='index'),

demo/views.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,30 @@
1212
from marshmallow import INCLUDE, ValidationError
1313

1414

15+
def organization_create(request):
16+
props = {}
17+
org_schema = OrganizationSchema()
18+
19+
if request.method == "POST":
20+
try:
21+
data = org_schema.loads(request.body)
22+
obj = Organization.objects.create(**data)
23+
except ValidationError as err:
24+
share_flash(request, error="Exists errors on form")
25+
share_flash(request, errors= err.messages)
26+
else:
27+
share_flash(request, success=f"Organization {obj.name} created")
28+
return redirect(reverse("demo:organizations"))
29+
return render_inertia(request, "Organizations.Create", {})
30+
31+
1532
def organization_edit(request, id):
1633
organization = Organization.objects.get(id=id)
1734
schema = OrganizationSchema()
1835

1936
if request.method == "POST":
2037
try:
21-
schema.loads(request.body)
38+
data = schema.loads(request.body)
2239
except ValidationError as err:
2340
share_flash(request, error="Exists errors on form")
2441
share_flash(request, errors= err.messages)
@@ -124,7 +141,7 @@ def contact_create(request):
124141
obj = Contact.objects.create(**c)
125142
except ValidationError as err:
126143
share_flash(request, error="Exists errors on form")
127-
print(err)
144+
128145
share_flash(request, errors= err.messages)
129146
else:
130147
share_flash(request, success=f"Contact {obj.name} created")

0 commit comments

Comments
 (0)