Skip to content
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

Update of client.js Ejercicio 5 #18

Open
IbaiRodriguez opened this issue Nov 8, 2022 · 0 comments
Open

Update of client.js Ejercicio 5 #18

IbaiRodriguez opened this issue Nov 8, 2022 · 0 comments

Comments

@IbaiRodriguez
Copy link

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const twits = collection(db, "devits");

const mapUserFromFirebaseAuthToUser = (user) => {
const { displayName, email, photoURL, uid } = user

return {
avatar: photoURL,
username: displayName,
email,
uid,
}
}

export const onAuthStateChanged = (onChange) => {
return getAuth(app).onAuthStateChanged((user) => {
const normalizedUser = user ? mapUserFromFirebaseAuthToUser(user) : null
onChange(normalizedUser)
})
}

export const loginWithGithub = () => {
const provider = new GithubAuthProvider();
const auth = getAuth();
return signInWithPopup(auth, provider);
};

export const addDevit = ({ avatar, content, img, userId, userName }) => {
return addDoc((twits), {
avatar,
content,
img,
userId,
userName,
createdAt: Timestamp.fromDate(new Date()),
likesCount: 0,
sharedCount: 0,
});
}

export const fetchLatestDevits = async () => {

const q = query(twits, orderBy("createdAt", "desc"));
const querySnapshot = getDocs(q);

return (await querySnapshot.then(({ docs }) => {
return docs.map((doc) => {
const data = doc.data()
const id = doc.id
const { createdAt } = data

  return {
    ...data,
    id,
    createdAt: +createdAt.toDate(),
  }
})

}));

}

export const uploadImage = (file) => {
const ref = getStorage(app).ref(images/${file.name})
const task = ref.put(file)
return task
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant