-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Task-Upgrade composition api #9
base: upgrade-vue3
Are you sure you want to change the base?
Changes from all commits
3763750
3e3bba6
e9c606d
c7e6aef
9728f33
034ae3e
ef8ebc8
cbffbbf
5e5410c
d6cfe70
84abc7a
b0075dd
5e34288
f74ea2b
5f10cc8
e85c55e
6b3d65c
89409ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,12 +157,14 @@ export const updatePersonalDataMethod = new ValidatedMethod({ | |
validate({ user }: { user: Meteor.User }) { | ||
try { | ||
check(user, { | ||
_id: String, | ||
username: String, | ||
emails: [{ address: String, verified: Boolean }], | ||
profile: { | ||
profile: String, | ||
name: String, | ||
path: Match.Maybe(String) | ||
path: Match.Maybe(String), | ||
updated_at: String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use camel case, please? |
||
} | ||
}); | ||
} catch (exception) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,20 +5,38 @@ | |
<component :is="Component"/> | ||
</transition> | ||
</router-view> | ||
<alert-message/> | ||
<loader/> | ||
<alert-message ref="alertMessage"/> | ||
<loader ref="loader"/> | ||
</v-app> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import AlertMessage from '@components/Utilities/Alerts/AlertMessage.vue'; | ||
import Loader from '@components/Utilities/Loaders/Loader.vue'; | ||
import { defineComponent } from 'vue'; | ||
import { defineComponent, onMounted, provide, ref } from 'vue'; | ||
import { Injections } from '@typings/utilities'; | ||
import mitt from 'mitt'; | ||
|
||
export default defineComponent({ | ||
name: 'App', | ||
components: { AlertMessage, Loader }, | ||
setup() { | ||
const alertMessage = ref(null); | ||
const loader = ref(null); | ||
|
||
onMounted(() => { | ||
provide(Injections.AlertMessage, alertMessage.value); | ||
provide(Injections.Loader, loader.value); | ||
const emitter = mitt(); | ||
provide(Injections.Emitter, emitter); | ||
}) | ||
Comment on lines
+27
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this for? if it is to use those components in the global scope, I had implemented in their mounted hooks the code to make them globals. Besides, I added mitt in the |
||
|
||
return { alertMessage, loader }; | ||
} | ||
}); | ||
|
||
|
||
|
||
</script> | ||
|
||
<style> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,77 +64,76 @@ | |
|
||
<script lang="ts"> | ||
import profilesMixin from '@mixins/accounts/profiles'; | ||
import validateForm from '@mixins/validateForm'; | ||
import uploadImage from '@mixins/users/uploadImage'; | ||
import { Form, Field, FormContext } from 'vee-validate'; | ||
import { Meteor } from 'meteor/meteor'; | ||
import { User } from '@typings/users'; | ||
import { defineComponent } from 'vue'; | ||
import { defineComponent, inject, reactive, ref } from 'vue'; | ||
import { ResponseMessage } from '@server/utils/ResponseMessage'; | ||
import { useAuthStore } from '/imports/ui/stores/auth'; | ||
import { useFormValidation } from '/imports/ui/composables/forms'; | ||
import { Injections, MeteorError } from '@typings/utilities'; | ||
import { AlertMessageType } from '@components/Utilities/Alerts/AlertMessage.vue'; | ||
import { LoaderType } from '@components/Utilities/Loaders/Loader.vue'; | ||
import { useUploadImage } from '/imports/ui/composables/users/uploadImage'; | ||
import { Emitter, EventType } from 'mitt'; | ||
|
||
export default defineComponent({ | ||
name: 'GeneralData', | ||
mixins: [validateForm, profilesMixin, uploadImage], | ||
mixins: [profilesMixin], | ||
components: { | ||
Form, | ||
Field | ||
}, | ||
setup() { | ||
setup: function () { | ||
const authStore = useAuthStore(); | ||
return { authStore }; | ||
}, | ||
data() { | ||
return { | ||
user: { | ||
emails: [{ verified: false }], | ||
profile: {} | ||
} as User, | ||
photoFileUser: null, | ||
initialValues: { | ||
name: '', | ||
username: '', | ||
email: '' | ||
} | ||
}; | ||
}, | ||
created() { | ||
const user = this.authStore.user; | ||
if (user) { | ||
this.user = { | ||
username: user.username, | ||
emails: user.emails, | ||
profile: { | ||
profile: user.profile.profile, | ||
name: user.profile.name, | ||
path: user.profile.path | ||
} | ||
}; | ||
this.initialValues = { | ||
name: user.profile.name as string, | ||
username: user.username as string, | ||
email: user.emails[0].address as string | ||
}; | ||
} | ||
}, | ||
methods: { | ||
async saveUser() { | ||
if (await this.isFormValid(this.$refs.dataFormObserver as FormContext)) { | ||
this.$loader.activate('Updating data. . .'); | ||
Meteor.call('user.updatePersonalData', { user: this.user, photoFileUser: this.photoFileUser }, | ||
(err: Meteor.Error, response: ResponseMessage) => { | ||
this.$loader.deactivate(); | ||
if (err) { | ||
console.error('Error to save user: ', err); | ||
this.$alert.showAlertSimple('error', err.reason); | ||
const dataFormObserver = ref<FormContext | null>(null); | ||
const alert = inject<AlertMessageType>(Injections.AlertMessage); | ||
const loader = inject<LoaderType>(Injections.Loader); | ||
Comment on lines
+90
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check if we can access with global props as shown:
|
||
const emitter = inject<Emitter<Record<EventType, unknown>>>(Injections.Emitter); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check if we can use |
||
const user: User = reactive(authStore.user ? {...authStore.user} : { | ||
emails: [{verified: false}], | ||
profile: {} | ||
}); | ||
const initialValues = reactive({ | ||
name: authStore.user?.profile.name || '', | ||
username: authStore.user?.username || '', | ||
email: authStore.user?.emails[0].address || '' | ||
}) | ||
const photoFileUser = ref(null); | ||
const {file, onClickUploadButton} = useUploadImage('fileUpload', (ev) => { | ||
user.profile.path = ev.target.result; | ||
photoFileUser.value = ev.target.result; | ||
}) | ||
|
||
const saveUser = async () => { | ||
const observer = dataFormObserver.value as FormContext | null; | ||
if (observer && alert && await useFormValidation(observer, alert)) { | ||
loader?.activate('Updating data . . .'); | ||
Meteor.call('user.updatePersonalData', {user, photoFileUser: photoFileUser.value}, | ||
(error: MeteorError, response: ResponseMessage) => { | ||
loader?.deactivate(); | ||
if (error && error instanceof Meteor.Error) { | ||
console.error('Error to save user: ', error); | ||
alert?.showAlertSimple('error', error.reason + ''); | ||
} else { | ||
this.authStore.setUser(Meteor.user()); | ||
this.emitter.emit('setUserLogged'); | ||
this.$alert.showAlertSimple('success', response.message); | ||
authStore.setUser(Meteor.user() as User); | ||
emitter?.emit('setUserLogged'); | ||
alert?.showAlertSimple('success', response.message + ''); | ||
} | ||
}); | ||
}) | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
authStore, | ||
dataFormObserver, | ||
saveUser, | ||
initialValues, | ||
photoFileUser, | ||
user, | ||
file, | ||
onClickUploadButton | ||
}; | ||
} | ||
}); | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,11 @@ | |
import { Meteor } from 'meteor/meteor'; | ||
import { User } from '@typings/users'; | ||
import { LogoutHook } from '@typings/accounts'; | ||
import { defineComponent } from 'vue'; | ||
import { computed, defineComponent, inject, onMounted, reactive, ref } from 'vue'; | ||
import { useAuthStore } from '/imports/ui/stores/auth'; | ||
import { useRouter } from 'vue-router'; | ||
import { Injections } from '@typings/utilities'; | ||
import { Emitter, EventType } from 'mitt'; | ||
|
||
declare module Accounts { | ||
function onLogout(func: Function): LogoutHook; | ||
|
@@ -33,67 +36,61 @@ export default defineComponent({ | |
name: 'UserLogged', | ||
setup() { | ||
const authStore = useAuthStore(); | ||
return { authStore }; | ||
}, | ||
data() { | ||
return { | ||
user: { | ||
emails: [], | ||
profile: {} | ||
} as User, | ||
onLogoutHook: null as LogoutHook | null | ||
}; | ||
}, | ||
created() { | ||
this.setSession(); | ||
|
||
}, | ||
mounted() { | ||
this.emitter.on('setUserLogged', () => { | ||
this.setSession(); | ||
const router = useRouter(); | ||
const emitter = inject<Emitter<Record<EventType, unknown>>>(Injections.Emitter); | ||
const user: User = reactive({ | ||
emails: [], | ||
profile: {} | ||
}); | ||
this.onLogoutHook = Accounts.onLogout(() => { | ||
this.closeFrontSession(); | ||
const onLogoutHook = ref<LogoutHook | null>(null); | ||
|
||
onMounted(() => { | ||
emitter?.on('setUserLogged', setSession); | ||
onLogoutHook.value = Accounts.onLogout(closeFrontSession); | ||
}); | ||
}, | ||
methods: { | ||
closeSession() { | ||
if (this.onLogoutHook) { | ||
this.onLogoutHook.stop(); | ||
|
||
const closeSession = () => { | ||
if (onLogoutHook.value) { | ||
onLogoutHook.value?.stop(); | ||
Meteor.logout(); | ||
this.authStore.logout(); | ||
this.$router.push({ name: 'login' }); | ||
authStore.logout(); | ||
router.push({ name: 'login' }); | ||
} | ||
}, | ||
closeFrontSession() { | ||
if (this.onLogoutHook) { | ||
this.onLogoutHook.stop(); | ||
this.authStore.logout(); | ||
this.$router.push({ name: 'login' }); | ||
}; | ||
|
||
const closeFrontSession = () => { | ||
if (onLogoutHook.value) { | ||
onLogoutHook.value?.stop(); | ||
authStore.logout(); | ||
router.push({ name: 'login'}); | ||
Comment on lines
+61
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be nice to test this, only to see if it's entering to the if statement |
||
} | ||
}, | ||
setSession() { | ||
}; | ||
|
||
const setSession = () => { | ||
if (Meteor.userId() !== null) { | ||
this.user = this.authStore.user || { | ||
Object.assign(user, authStore.user || { | ||
emails: [], | ||
profile: {} | ||
}; | ||
}); | ||
} else { | ||
this.closeSession(); | ||
closeSession(); | ||
} | ||
} | ||
}, | ||
computed: { | ||
usernameInitials() { | ||
|
||
const usernameInitials = computed(() => { | ||
let initials = ''; | ||
if (this.user?.username) { | ||
const words = this.user.username.split(' '); | ||
if (user.username) { | ||
const words = user.username.split(' '); | ||
initials = words.reduce((acc, word) => { | ||
return acc + word[0]; | ||
}, ''); | ||
} | ||
return initials.toUpperCase(); | ||
} | ||
}); | ||
|
||
setSession(); | ||
|
||
return { user, usernameInitials, closeSession }; | ||
} | ||
}); | ||
</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not necessary since we can take the user id from the current connection