Browse Source

Refactor: Use ModelForm instead of copy-and-paste

pull/169/head
winkidney 5 years ago
committed by Isaac Bythewood
parent
commit
bd6960fcf1
5 changed files with 71 additions and 103 deletions
  1. +3
    -14
      pinry-spa/src/components/BoardEdit.vue
  2. +18
    -20
      pinry-spa/src/components/LoginForm.vue
  3. +29
    -63
      pinry-spa/src/components/SignUpForm.vue
  4. +6
    -6
      pinry-spa/src/components/pin_create/PinCreateModal.vue
  5. +15
    -0
      pinry-spa/src/components/utils/ModelForm.js

+ 3
- 14
pinry-spa/src/components/BoardEdit.vue View File

@@ -32,14 +32,14 @@


<script> <script>
import API from './api'; import API from './api';
import formUtils from './utils/form';
import ModelForm from './utils/ModelForm';


const fields = ['name']; const fields = ['name'];


export default { export default {
name: 'BoardEditModal', name: 'BoardEditModal',
data() { data() {
const model = formUtils.fromFields(fields);
const model = ModelForm.fromFields(fields);
return { return {
form: model.form, form: model.form,
helper: model, helper: model,
@@ -55,18 +55,7 @@ export default {
self.$parent.close(); self.$parent.close();
}, },
(resp) => { (resp) => {
Object.entries(resp.data).forEach(
(errorTuple) => {
const [key, error] = errorTuple;
let msg;
if (Array.isArray(error)) {
[msg] = error;
} else {
msg = error;
}
this.helper.markFieldAsDanger(key, msg);
},
);
self.helper.markFieldsAsDanger(resp.data);
}, },
); );
}, },


+ 18
- 20
pinry-spa/src/components/LoginForm.vue View File

@@ -7,11 +7,11 @@
</header> </header>
<section class="modal-card-body"> <section class="modal-card-body">
<b-field label="Username" <b-field label="Username"
:type="username.type"
:message="username.error">
:type="form.username.type"
:message="form.username.error">
<b-input <b-input
type="string" type="string"
v-model="username.value"
v-model="form.username.value"
placeholder="Your Username" placeholder="Your Username"
maxlength="30" maxlength="30"
required> required>
@@ -19,11 +19,11 @@
</b-field> </b-field>


<b-field label="Password" <b-field label="Password"
:type="password.type"
:message="password.error">
:type="form.password.type"
:message="form.password.error">
<b-input <b-input
type="password" type="password"
v-model="password.value"
v-model="form.password.value"
password-reveal password-reveal
placeholder="Your password" placeholder="Your password"
required> required>
@@ -43,36 +43,34 @@


<script> <script>
import api from './api'; import api from './api';
import form from './utils/form';
import ModelForm from './utils/ModelForm';


const fields = ['username', 'password']; const fields = ['username', 'password'];


export default { export default {
name: 'LoginForm', name: 'LoginForm',
data() { data() {
return form.createFormModel(fields);
const model = ModelForm.fromFields(fields);
return {
form: model.form,
helper: model,
};
}, },
methods: { methods: {
resetStatus() {
form.FormHelper(this, fields).resetAllFields();
},
doLogin() { doLogin() {
this.resetStatus();
this.helper.resetAllFields();
const self = this; const self = this;
const promise = api.User.logIn(self.username.value, self.password.value);
const helper = form.FormHelper(self);
const promise = api.User.logIn(
self.form.username.value,
self.form.password.value,
);
promise.then( promise.then(
(user) => { (user) => {
self.$emit('login.succeed', user); self.$emit('login.succeed', user);
self.$parent.close(); self.$parent.close();
}, },
(resp) => { (resp) => {
if (resp.data.username) {
helper.markFieldAsDanger('username', resp.data.username);
}
if (resp.data.password) {
helper.markFieldAsDanger('password', resp.data.password);
}
self.helper.markFieldsAsDanger(resp.data);
}, },
); );
}, },


+ 29
- 63
pinry-spa/src/components/SignUpForm.vue View File

@@ -7,11 +7,11 @@
</header> </header>
<section class="modal-card-body"> <section class="modal-card-body">
<b-field label="Username" <b-field label="Username"
:type="username.type"
:message="username.error">
:type="form.username.type"
:message="form.username.error">
<b-input <b-input
type="string" type="string"
v-model="username.value"
v-model="form.username.value"
placeholder="Your Username" placeholder="Your Username"
maxlength="30" maxlength="30"
required> required>
@@ -19,33 +19,33 @@
</b-field> </b-field>


<b-field label="Email" <b-field label="Email"
:type="email.type"
:message="email.error">
:type="form.email.type"
:message="form.email.error">
<b-input <b-input
type="email" type="email"
v-model="email.value"
v-model="form.email.value"
password-reveal password-reveal
placeholder="Your email" placeholder="Your email"
required> required>
</b-input> </b-input>
</b-field> </b-field>
<b-field label="Password" <b-field label="Password"
:type="password.type"
:message="password.error">
:type="form.password.type"
:message="form.password.error">
<b-input <b-input
type="password" type="password"
v-model="password.value"
v-model="form.password.value"
password-reveal password-reveal
placeholder="Your password" placeholder="Your password"
required> required>
</b-input> </b-input>
</b-field> </b-field>
<b-field label="Password repeat" <b-field label="Password repeat"
:type="password_repeat.type"
:message="password_repeat.error">
:type="form.password_repeat.type"
:message="form.password_repeat.error">
<b-input <b-input
type="password" type="password"
v-model="password_repeat.value"
v-model="form.password_repeat.value"
password-reveal password-reveal
placeholder="Your password again" placeholder="Your password again"
required> required>
@@ -65,56 +65,33 @@


<script> <script>
import api from './api'; import api from './api';
import ModelForm from './utils/ModelForm';

const fields = [
'username',
'email',
'password',
'password_repeat',
];


export default { export default {
name: 'SignUpForm', name: 'SignUpForm',
data() { data() {
const model = ModelForm.fromFields(fields);
return { return {
username: {
value: null,
error: null,
type: null,
},
email: {
value: null,
error: null,
type: null,
},
password: {
value: null,
error: null,
type: null,
},
password_repeat: {
value: null,
error: null,
type: null,
},
form: model.form,
helper: model,
}; };
}, },
methods: { methods: {
resetStatus() {
this.resetField('username');
this.resetField('email');
this.resetField('password');
this.resetField('password_repeat');
},
resetField(fieldName) {
this[fieldName].type = 'is-info';
this[fieldName].error = null;
},
markFieldAsDanger(fieldName, errorMsg) {
this[fieldName].error = errorMsg;
this[fieldName].type = 'is-danger';
},
doRegister() { doRegister() {
this.resetStatus();
this.helper.resetAllFields();
const self = this; const self = this;
const promise = api.User.signUp( const promise = api.User.signUp(
self.username.value,
self.email.value,
self.password.value,
self.password_repeat.value,
self.form.username.value,
self.form.email.value,
self.form.password.value,
self.form.password_repeat.value,
); );
promise.then( promise.then(
(user) => { (user) => {
@@ -122,18 +99,7 @@ export default {
self.$parent.close(); self.$parent.close();
}, },
(resp) => { (resp) => {
Object.entries(resp.data).forEach(
(errorTuple) => {
const [key, error] = errorTuple;
let msg;
if (Array.isArray(error)) {
[msg] = error;
} else {
msg = error;
}
this.markFieldAsDanger(key, msg);
},
);
self.helper.markFieldsAsDanger(resp.data);
}, },
); );
}, },


+ 6
- 6
pinry-spa/src/components/pin_create/PinCreateModal.vue View File

@@ -76,25 +76,25 @@
import API from '../api'; import API from '../api';
import FileUpload from './FileUpload.vue'; import FileUpload from './FileUpload.vue';
import bus from '../utils/bus'; import bus from '../utils/bus';
import ModelForm from '../utils/ModelForm';


function isURLBlank(url) { function isURLBlank(url) {
return url !== null && url !== ''; return url !== null && url !== '';
} }


const fields = ['url', 'referer', 'description', 'tags'];

export default { export default {
name: 'PinCreateModal', name: 'PinCreateModal',
components: { components: {
FileUpload, FileUpload,
}, },
data() { data() {
const form = ModelForm.createFormModel(fields);
form.tags.value = [];
return { return {
disableUrlField: false, disableUrlField: false,
form: {
url: { value: null, error: null, type: null },
referer: { value: null, error: null, type: null },
description: { value: null, error: null, type: null },
tags: { value: [], error: null, type: null },
},
form,
formUpload: { formUpload: {
imageId: null, imageId: null,
}, },


pinry-spa/src/components/utils/form.js → pinry-spa/src/components/utils/ModelForm.js View File

@@ -24,6 +24,20 @@ function FormHelper(form, fields = []) {
self[fieldName].error = errorMsg; self[fieldName].error = errorMsg;
self[fieldName].type = 'is-danger'; self[fieldName].type = 'is-danger';
} }
function markFieldsAsDanger(errorRespObjecct) {
Object.entries(errorRespObjecct).forEach(
(errorTuple) => {
const [key, error] = errorTuple;
let msg;
if (Array.isArray(error)) {
[msg] = error;
} else {
msg = error;
}
markFieldAsDanger(key, msg);
},
);
}
function resetAllFields() { function resetAllFields() {
fields.forEach( fields.forEach(
(fieldName) => { (fieldName) => {
@@ -34,6 +48,7 @@ function FormHelper(form, fields = []) {
return { return {
form, form,
fields, fields,
markFieldsAsDanger,
markFieldAsDanger, markFieldAsDanger,
resetField, resetField,
resetAllFields, resetAllFields,

Loading…
Cancel
Save