Created
January 27, 2021 09:32
-
-
Save ekosutrisno/a83cd0eea603a8a61d671ee01e278de4 to your computer and use it in GitHub Desktop.
Function In ExoApps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bottom = ref(null) | |
const scrollToBottom = () => { | |
bottom.value.scrollIntoView({behavior: 'smooth'}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const changeAvatar = (event) => { | |
if (event.target.files && event.target.files[0]) { | |
const fileType = event.target.files[0].type.toString(); | |
if(fileType.indexOf('image') != 0){ | |
alert('Please Choose an Image') | |
return; | |
} | |
state.newFoto = event.target.files[0]; | |
state.photoUrl = URL.createObjectURL(event.target.files[0]) | |
}else{ | |
alert('Error when change avatar!') | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createGroup = ( inputGroupName ) => { | |
let groupId = inputGroupName.toUpperCase().replace(" ","") + new Date().getTime(); | |
db.firestore().collection('exoapps') | |
.doc(state.user_id) | |
.collection('groups') | |
.doc(groupId) | |
.set({ | |
created_date: new Date(), | |
group_name: inputGroupName, | |
user_id: state.user_id, | |
}) | |
.then(ref =>{ | |
db.firestore().collection('exoapps') | |
.where('user_id', '==', state.user_id ) | |
.get().then( querySnapshot => { | |
querySnapshot.forEach(doc => { | |
db.firestore().collection('exoapps') | |
.doc(state.user_id) | |
.collection('groups') | |
.doc(groupId) | |
.collection('members') | |
.doc(state.user_id) | |
.set(doc.data()) | |
.then(ref =>{ | |
console.log("Group Added!") | |
}).catch(e =>{ | |
console.log(e) | |
}) | |
}) | |
}) | |
}).catch(e =>{ | |
console.log(e) | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getDataMessages = async () => { | |
state.isProcess = true; | |
autoStopSpinner(); | |
// Temp Current Room | |
let groupChatId = `${state.currentPeerUserId}+${state.currentUserId}`; | |
// Init DB Object | |
const messageRefw = await db.database().ref(groupChatId); | |
// Get Reference | |
let keyChek = await messageRefw.get() | |
// Checking Conditon To Check Existing Room Data | |
if (keyChek.exists()) { | |
// Set Room To Current State | |
state.groupChatId = groupChatId; | |
retrieveMessagesFromDB(groupChatId); | |
} else { | |
// Set new Room | |
state.groupChatId = `${state.currentUserId}+${state.currentPeerUserId}`; | |
retrieveMessagesFromDB(state.groupChatId); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getExampleData = async () => { | |
const data = await db.firestore().collection('exoapps').get(); | |
if(data.docs.length > 0){ | |
let listUser = []; | |
listUser = [...data.docs] | |
listUser.forEach(user =>{ | |
// Get Groups Users | |
user.ref.collection('groups').get() | |
.then(gr => { | |
if (gr.docs.length > 0) { | |
let listGroup = [] | |
listGroup = [...gr.docs] | |
//Push to Local State | |
listGroup.forEach(g =>{ | |
state.groups.push({ | |
gorupId: g.id, | |
groupName: g.data().group_name, | |
createdDate: g.data().created_date | |
}) | |
}) | |
} | |
}) | |
}) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getUserPeerData = async () => { | |
const peerUser = await db.firestore().collection('users') | |
.where('user_id', '==', localStorage.getItem("peerUserId") ) | |
.get().then( querySnapshot => { | |
querySnapshot.forEach(doc => { | |
state.currentPeerUser = doc.data() | |
}) | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const inviteFriend = async ( friendEmail ) => { | |
await db.firestore().collection('exoapps') | |
.where('email', '==', friendEmail ) | |
.get().then( querySnapshot => { | |
querySnapshot.forEach(doc => { | |
db.firestore().collection('exoapps') | |
.doc(state.user_id) | |
.collection('friends').doc(doc.data().user_id).set(doc.data()) | |
.then(ref =>{ | |
console.log("Friend Added!") | |
}).catch(e =>{ | |
console.log(e) | |
}) | |
}) | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const retrieveMessagesFromDB = async ( group ) => { | |
await db.database().ref(group).on('value', (snapshot) => { | |
const data = snapshot.val(); | |
let messages = []; | |
if ( data !== null ) { | |
Object.keys(data).forEach( key => { | |
const obejctMessage = { | |
date: key, | |
chats: data[key] | |
} | |
messages.push(obejctMessage) | |
}) | |
} | |
// Set ListMessage Data | |
state.listMessages = messages; | |
state.isProcess = false; | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sendMessage = ( inputMessageValue ) => { | |
// Check text message not null or blank | |
if(inputMessageValue.trim() === ""){ | |
return; | |
} | |
// Generate Message Object | |
const message = { | |
username: state.currentUsername, | |
isGroup: false, | |
idFrom: state.currentUserId, | |
idTo: state.currentPeerUserId, | |
timestamp: moment().valueOf().toString(), | |
sendTime:formatTime(new Date()), | |
content: inputMessageValue.trim() | |
} | |
// DB Object Init | |
const messageRef = db.database().ref(state.groupChatId); | |
// Save and Insert Data To Realtime DB Firebase | |
messageRef.child(moment() | |
.format('YYYY-MM-DD').toString()) | |
.push(message) | |
// Reset TextMessage | |
inputMessage.value = "" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const uploadAvatar = () => { | |
state.isProcess = true; | |
autoStopSpinner(); | |
if(state.newFoto){ | |
const upload = db.storage().ref() | |
.child(state.user_id) | |
.put(state.newFoto) | |
upload.on( | |
'state_changed', | |
null, | |
err => console.log(err), | |
()=>{ | |
upload.snapshot.ref.getDownloadURL().then(url => { | |
updateUserInfo(true, url); | |
}) | |
} | |
) | |
} else { | |
updateUserInfo(false, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment