1313 <v-card-text >
1414 <v-subheader class =" pa-0" > Where do you live? </v-subheader >
1515
16- <v-form @submit.prevent = " CreateRepList() " ref =" form" >
16+ <v-form ref =" form" >
1717 <v-text-field
18- v-model =" postalCode "
18+ v-model =" searchText "
1919 label =" Postal Code"
2020 required
2121 @keyup =" CheckInputContent"
5353 State
5454 </v-btn >
5555
56- <v-btn
57- rounded
58- dark
59- class =" ui button toggle search-reps-button"
60- :style =" {
61- backgroundColor:
62- currentFilter === 'local' && isActive ? 'blue' : 'gray'
63- }"
64- @click =" FilterList('local')"
65- >
66- Local
67- </v-btn >
68-
6956 <v-btn
7057 class =" search-reps-button"
7158 rounded
8168 </v-btn >
8269
8370 <v-btn
84- class =" search-reps-button"
8571 rounded
8672 dark
87- : class =" { active: isActive } "
73+ class =" ui button toggle search-reps-button "
8874 :style =" {
8975 backgroundColor:
90- currentFilter === 'school' && isActive ? 'blue' : 'gray'
76+ currentFilter === 'municipality' && isActive
77+ ? 'blue'
78+ : 'gray'
9179 }"
9280 @click =" FilterList('school')"
9381 >
94- School
82+ Local
9583 </v-btn >
9684 </v-col >
9785 </v-row >
10189 <v-btn
10290 :to =" {
10391 name: 'Reps',
104- params: { postalCode: postalCode }
92+ params: { searchText: searchText }
10593 }"
10694 clickclass =" mr-4"
10795 @click =" CreateRepList()"
153141 <take-action
154142 :letter-body =" letterBody"
155143 :selected-rep =" selectedRep"
144+ :rep-name =" selectedRepName"
156145 />
157146 </v-col >
158147 </v-row >
168157import RepresentativeCard from ' @/components/RepresentativeCard.vue'
169158import TakeAction from ' @/components/TakeAction.vue'
170159import axios from ' axios'
171-
172160export default {
173161 name: ' SearchReps' ,
174162 components: {
@@ -179,10 +167,11 @@ export default {
179167 return {
180168 letterBody: ' ' ,
181169 selectedRep: {},
170+ selectedRepName: ' ' ,
182171 congressMembers: [],
183172 currentFilter: ' ' ,
184173 hasContent: true ,
185- postalCode : this .$route .params .postalCode || ' ' ,
174+ searchText : this .$route .params .searchText || ' ' ,
186175 listVisible: false ,
187176 isActive: false ,
188177 myHeight: this .$vuetify .breakpoint .mobile ? false : " 100vh"
@@ -192,26 +181,49 @@ export default {
192181 handleRepSelected (letterBody , selectedRep , step2 ) {
193182 this .letterBody = letterBody
194183 this .selectedRep = selectedRep
184+ this .selectedRepName = selectedRep .name
195185 this .step2 = step2
196186 },
197187 CheckInputContent : function () {
198- if (this .postalCode !== ' ' ) {
199- this .hasContent = true
200- } else {
201- this .hasContent = false
202- }
188+
189+ if (this .searchText !== ' ' ) {
190+ this .hasContent = true
191+ } else {
192+ this .hasContent = false
193+ }
203194 },
204195 async CreateRepList () {
205196 try {
206- this .$store .commit (' setGenericValue' , { key: ' zipcode' , value: this .postalCode })
207- const res = await axios .get (
208- ' /api/representatives/' + this .postalCode
209- )
197+ this .$store .commit (' setGenericValue' , { key: ' searchText' , value: this .searchText })
198+ // check postal code is valid with regex
199+ let res = ' '
200+ let params = {streetAddress: this .searchText }
201+ let isPostalCodeValid = / (^ \d {5} $ )| (^ \d {5} -\d {4} $ )/ .test (this .searchText );
202+ let streetAddressValid = / ^ (\d {3,} )\s ? (\w {0,5} )\s ([a-zA-Z ] {2,30} )\s ([a-zA-Z ] {2,15} )\. ? \s ? (\w {0,5} )$ / .test (this .searchText );
203+
204+
205+ if (isPostalCodeValid) {
206+ // console.log('valid input to get representatives with postal code')
207+ res = await axios .get (
208+ ' /api/representatives/' + this .searchText
209+ )
210+ }
211+
212+ else if (streetAddressValid) {
213+ // console.log('valid input to get representatives with street address')
214+ res = await axios .get (
215+ ' /api/representatives/ ' , { params }
216+ )
217+ }
218+
210219 this .isActive = false
211220 this .congressMembers = res .data
212221 this .hasContent = true
213222 this .listVisible = true
214- } catch (e) {
223+ }
224+
225+
226+ catch (e) {
215227 console .error (e)
216228 }
217229 },
@@ -222,27 +234,31 @@ export default {
222234 if (! this .isActive ) {
223235 this .isActive = true
224236 const params = {}
237+
225238 if (this .currentFilter != null ) {
226239 params .filter = this .currentFilter
227- }
228240
241+
242+ params .streetAddress = this .searchText
229243 const res = await axios .get (
230- ' /api/representatives/' + this .postalCode ,
244+ ' /api/representatives/' + this .searchText ,
231245 {
232246 params
233247 }
234248 )
235249
236250 console .log (res)
237251 this .congressMembers = res .data
252+
238253 } else {
239254 this .isActive = false ;
240255 const res = await axios .get (
241- ' /api/representatives/' + this .postalCode
256+ ' /api/representatives/' + this .searchText
242257 )
243258
244259 this .congressMembers = res .data
245260 }
261+ }
246262 } catch (e) {
247263 console .error (e)
248264 }
0 commit comments