1+ <template >
2+ <div class =" container" >
3+ <h1
4+ class =" text-primary"
5+ >Edit {{ $store.state.customerLoading ? 'Customer' : $store.state.currentCustomer.firstName }}</h1 >
6+ <hr class =" my-2" />
7+ <h3 v-if =" $store.state.customerLoading" class =" text-center text-success my-3" >Loading ...</h3 >
8+ <form v-else @submit.prevent =" handleSubmit" >
9+ <div class =" jumbotron my-3 p-4" >
10+ <h4 >Customer Info</h4 >
11+ <div class =" form-group" >
12+ <label >First Name</label >
13+ <input
14+ required
15+ v-model =" customer.firstName"
16+ type =" text"
17+ class =" form-control"
18+ placeholder =" Enter Fisrt Name"
19+ />
20+ </div >
21+ <div class =" form-group" >
22+ <label >Last Name</label >
23+ <input
24+ required
25+ v-model =" customer.lastName"
26+ type =" text"
27+ class =" form-control"
28+ placeholder =" Enter Last Name"
29+ />
30+ </div >
31+ </div >
32+ <div class =" jumbotron my-3 p-4" >
33+ <h4 >Customer Contact</h4 >
34+ <div class =" form-group" >
35+ <label >Email Address</label >
36+ <input
37+ required
38+ v-model =" customer.email"
39+ type =" email"
40+ class =" form-control"
41+ placeholder =" Enter Email Address"
42+ />
43+ </div >
44+ <div class =" form-group" >
45+ <label >Phone Number</label >
46+ <input
47+ required
48+ v-model =" customer.phone"
49+ type =" tel"
50+ class =" form-control"
51+ placeholder =" Enter Phone Number"
52+ />
53+ </div >
54+ </div >
55+ <div class =" jumbotron my-3 p-4" >
56+ <h4 >Customer location</h4 >
57+ <div class =" form-group" >
58+ <label >Address</label >
59+ <input
60+ required
61+ v-model =" customer.address"
62+ type =" text"
63+ class =" form-control"
64+ placeholder =" Enter Full Address"
65+ />
66+ </div >
67+ <div class =" form-group" >
68+ <label >City</label >
69+ <input
70+ required
71+ v-model =" customer.city"
72+ type =" text"
73+ class =" form-control"
74+ placeholder =" Enter City"
75+ />
76+ </div >
77+ <div class =" form-group" >
78+ <label >State</label >
79+ <input
80+ v-model =" customer.state"
81+ type =" text"
82+ class =" form-control"
83+ placeholder =" Enter State"
84+ />
85+ </div >
86+ <div class =" text-center" >
87+ <button class =" btn btn-success" >{{ submitted ? 'Loading ...' : 'Submit' }}</button >
88+ </div >
89+ </div >
90+ </form >
91+ </div >
92+ </template >
93+
94+ <script >
95+ export default {
96+ name: " AppAddPage" ,
97+ data () {
98+ return {
99+ customer: {
100+ firstName: " " ,
101+ lastName: " " ,
102+ email: " " ,
103+ phone: " " ,
104+ city: " " ,
105+ address: " " ,
106+ state: " " ,
107+ },
108+ submitted: false ,
109+ };
110+ },
111+ created : async function () {
112+ if (! this .$store .state .currentCustomer ) {
113+ const err = await this .$store .dispatch (" customer" , this .$route .params .id );
114+ if (err) {
115+ return this .$store .dispatch (" setAlert" , {
116+ type: " Error" ,
117+ msg: err[0 ].message ,
118+ });
119+ }
120+ }
121+ this .customer = this .$store .state .currentCustomer ;
122+ },
123+ methods: {
124+ handleSubmit : async function () {
125+ this .submitted = true ;
126+ if (! this .validateEmail (this .customer .email )) {
127+ this .submitted = false ;
128+ return this .$store .dispatch (" setAlert" , {
129+ type: " Error" ,
130+ msg: " Please enter a valid Email Address" ,
131+ });
132+ }
133+ const err = await this .$store .dispatch (" updateCustomer" , {
134+ customer: this .customer ,
135+ _id: this .$route .params .id ,
136+ });
137+ if (err) {
138+ this .submitted = false ;
139+ return this .$store .dispatch (" setAlert" , {
140+ type: " Error" ,
141+ msg: err[0 ].message ,
142+ });
143+ }
144+ this .submitted = false ;
145+ this .$store .dispatch (" setAlert" , {
146+ type: " Notification" ,
147+ msg: ` ${ this .customer .firstName } is Successfully Updated with Email <em>${ this .customer .email } </em>` ,
148+ });
149+ this .$router .push (" /" );
150+ },
151+ validateEmail (email ) {
152+ const reg = new RegExp (
153+ / ^ (([^ <>()[\]\\ . ,;:\s @"] + (\. [^ <>()[\]\\ . ,;:\s @"] + )* )| (". + "))@((\[ [0-9 ] {1,3} \. [0-9 ] {1,3} \. [0-9 ] {1,3} \. [0-9 ] {1,3} \] )| (([a-zA-Z\-0 -9] + \. )+ [a-zA-Z ] {2,} ))$ /
154+ );
155+ return reg .test (email);
156+ },
157+ },
158+ beforeRouteLeave (to , from , next ) {
159+ this .$store .commit (" CLEAR_CURRENT_CUSTOMER" );
160+ next ();
161+ },
162+ };
163+ </script >
164+
165+ <style >
166+ </style >
0 commit comments