Skip to content

Commit

Permalink
sreach functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
SMNAFI committed Nov 24, 2022
1 parent 7b3a807 commit 6975ee9
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 109 deletions.
11 changes: 7 additions & 4 deletions src/components/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const Donar = ({ request }) => {
date,
numBag,
contact,
location,
area,
district,
numManaged,
response,
// response,
} = request
return (
<Card className='my-3 p-3 rounded'>
Expand All @@ -37,9 +38,11 @@ const Donar = ({ request }) => {
<Card.Text>Date: {date}</Card.Text>
<Card.Text>Number of bages: {numBag}</Card.Text>
<Card.Text>Contuct Number: {contact}</Card.Text>
<Card.Text>Location: {location}</Card.Text>
<Card.Text>
Location: {area}, {district}
</Card.Text>
<Card.Text>Bag managed: {numManaged}</Card.Text>
<Card.Text>Total response: {response.length}</Card.Text>
{/* <Card.Text>Total response: {response.length}</Card.Text> */}
</Card.Body>
<Link to={`/feed/${id}`}>
<Button>Details</Button>
Expand Down
83 changes: 62 additions & 21 deletions src/screens/DonarsScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { collection, onSnapshot, query, where } from 'firebase/firestore'
import { db } from '../firebase'
import Loader from './../components/Loader'
import Message from './../components/Message'
import { Col, Row } from 'react-bootstrap'
import { Col, Form, Row } from 'react-bootstrap'
import Donar from './../components/Donar'

function DonarsScreen() {
Expand All @@ -13,23 +13,6 @@ function DonarsScreen() {
const [error, setError] = useState(null)

useEffect(() => {
// const unsub = onSnapshot(
// collection(db, 'users'),
// (snapShot) => {
// let list = []

// snapShot.docs.forEach((doc) => {
// list.push({ id: doc.id, ...doc.data() })
// })
// setDonars(list)
// setLoading(false)
// // console.log(list)
// },
// (error) => {
// setError(error)
// // console.log(error)
// }
// )
const q = query(collection(db, 'users'), where('isDonar', '==', true))
const unsub = onSnapshot(
q,
Expand All @@ -38,13 +21,13 @@ function DonarsScreen() {
querySnapshot.forEach((doc) => {
list.push({ id: doc.id, ...doc.data() })
})
// console.log(list)

setDonars(list)
setLoading(false)
},
(error) => {
setError(error)
// console.log(error)
console.log(error.message)
}
)

Expand All @@ -53,16 +36,74 @@ function DonarsScreen() {
}
}, [])

// searching by bloodGroup and area
const [queryByGroup, setQueryByGroup] = useState('')
const [queryByArea, setQueryByArea] = useState('')

const handleSearch = (donars) => {
if (queryByArea && queryByGroup) {
return donars.filter(
(donar) =>
donar.bloodGroup === queryByGroup &&
(donar.area.toLowerCase().includes(queryByArea.toLowerCase()) ||
donar.district.toLowerCase().includes(queryByArea.toLowerCase()))
)
}
if (queryByGroup) {
return donars.filter((donar) => donar.bloodGroup === queryByGroup)
}
if (queryByArea) {
return donars.filter(
(donar) =>
donar.area.toLowerCase().includes(queryByArea.toLowerCase()) ||
donar.district.toLowerCase().includes(queryByArea.toLowerCase())
)
}
return donars
}

return (
<>
<h1 className='my-5'>Donars</h1>
<Row>
<Col md={6}>
<Form.Group controlId='name'>
<Form.Label>Find donars by area</Form.Label>
<Form.Control
type='text'
value={queryByArea}
onChange={(e) => setQueryByArea(e.target.value)}
></Form.Control>
</Form.Group>
</Col>
<Col md={6}>
<Form.Group controlId='group'>
<Form.Label>Find donars by blood group</Form.Label>
<Form.Select
value={queryByGroup}
onChange={(e) => setQueryByGroup(e.target.value)}
>
<option></option>
<option>A+</option>
<option>A-</option>
<option>B+</option>
<option>B-</option>
<option>AB+</option>
<option>AB-</option>
<option>O+</option>
<option>O-</option>
</Form.Select>
</Form.Group>
</Col>
</Row>

{loading ? (
<Loader />
) : error ? (
<Message variant='danger'>{error}</Message>
) : (
<Row>
{donars.map((donar) => (
{handleSearch(donars).map((donar) => (
<Col key={donar.id} sm={12} lg={6}>
<Donar donar={donar} />
</Col>
Expand Down
38 changes: 19 additions & 19 deletions src/screens/LoginScreen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react'
import { signInWithEmailAndPassword } from 'firebase/auth'
import { auth } from '../firebase'
import { signInWithEmailAndPassword, signInWithPopup } from 'firebase/auth'
import { auth, provider } from '../firebase'
import { Button, Col, Form, Row } from 'react-bootstrap'
import { Link, useNavigate } from 'react-router-dom'
import FormContainer from './../components/FormContainer'
Expand Down Expand Up @@ -54,24 +54,24 @@ const LoginScreen = () => {
}
}

// const googleSignIn = async () => {
// try {
// setError(null)
// const data = await signInWithPopup(auth, provider)
// const { user } = data
const googleSignIn = async () => {
try {
setError(null)
const data = await signInWithPopup(auth, provider)
const { user } = data

// setLoading(true)
setLoading(true)

// const res = await getDoc(doc(db, 'users', user.uid))
const res = await getDoc(doc(db, 'users', user.uid))

// dispatch(setUser({ uid: user.uid, ...res.data() }))
// setLoading(false)
// } catch (error) {
// setLoading(false)
// setError(error.error)
// console.log(error.code, error.error)
// }
// }
dispatch(setUser({ uid: user.uid, ...res.data() }))
setLoading(false)
} catch (error) {
setLoading(false)
setError(error.error)
console.log(error.code, error.error)
}
}

return (
<FormContainer>
Expand Down Expand Up @@ -117,11 +117,11 @@ const LoginScreen = () => {
</Row>
</Form>

{/* <div className='text-center my-5'>
<div className='text-center my-5'>
<button className='btn-google' onClick={googleSignIn}>
<i className='fa-brands fa-google'></i> Sign in with google
</button>
</div> */}
</div>
</FormContainer>
)
}
Expand Down
1 change: 0 additions & 1 deletion src/screens/ProfileScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ const ProfileScreen = () => {
<Form.Label>Status</Form.Label>
<Form.Select
value={status}
size='sm'
onChange={(e) => setStatus(e.target.value)}
required={true}
>
Expand Down
114 changes: 57 additions & 57 deletions src/screens/RegisterScreen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react'
import { createUserWithEmailAndPassword } from 'firebase/auth'
import { createUserWithEmailAndPassword, signInWithPopup } from 'firebase/auth'
import { doc, serverTimestamp, setDoc } from 'firebase/firestore'
import { auth } from '../firebase'
import { auth, provider } from '../firebase'
import { Link, useNavigate } from 'react-router-dom'
import FormContainer from '../components/FormContainer'
import Message from './../components/Message'
Expand Down Expand Up @@ -97,59 +97,59 @@ const RegisterScreen = () => {
}
}

// const googleSignUp = async () => {
// try {
// setError(null)
// const res = await signInWithPopup(auth, provider)

// const { user } = res
// const { uid, displayName, email, photoURL } = user

// setLoading(true)

// // storing user into firestore
// await setDoc(doc(db, 'users', uid), {
// name: displayName,
// email,
// photoURL,
// phone: '',
// bloodGroup: '',
// status: '',
// isDonar: false,
// isAdmin: false,
// numDonation: 0,
// area: '',
// district: '',
// lastDonation: '',
// response: 0,
// timeStamp: serverTimestamp(),
// })

// dispatch(
// setUser({
// uid,
// email,
// name: displayName,
// photoURL,
// phone: '',
// bloodGroup: '',
// status: '',
// isDonar: false,
// isAdmin: false,
// numDonation: 0,
// area: '',
// district: '',
// lastDonation: '',
// response: 0,
// })
// )
// setLoading(false)
// } catch (error) {
// setLoading(false)
// setError(error.message)
// console.log(error.code, error.message)
// }
// }
const googleSignUp = async () => {
try {
setError(null)
const res = await signInWithPopup(auth, provider)

const { user } = res
const { uid, displayName, email, photoURL } = user

setLoading(true)

// storing user into firestore
await setDoc(doc(db, 'users', uid), {
name: displayName,
email,
photoURL,
phone: '',
bloodGroup: '',
status: '',
isDonar: false,
isAdmin: false,
numDonation: 0,
area: '',
district: '',
lastDonation: '',
response: 0,
timeStamp: serverTimestamp(),
})

dispatch(
setUser({
uid,
email,
name: displayName,
photoURL,
phone: '',
bloodGroup: '',
status: '',
isDonar: false,
isAdmin: false,
numDonation: 0,
area: '',
district: '',
lastDonation: '',
response: 0,
})
)
setLoading(false)
} catch (error) {
setLoading(false)
setError(error.message)
console.log(error.code, error.message)
}
}

return (
<FormContainer>
Expand Down Expand Up @@ -222,11 +222,11 @@ const RegisterScreen = () => {
</Row>
</Form>

{/* <div className='text-center my-5'>
<div className='text-center my-5'>
<button className='btn-google' onClick={googleSignUp}>
<i className='fa-brands fa-google'></i> Sign up with google
</button>
</div> */}
</div>
</FormContainer>
)
}
Expand Down
10 changes: 6 additions & 4 deletions src/screens/RequestDetailsScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function RequestDetailsScreen() {
date,
numBag,
contact,
location,
area,
district,
numManaged,
response,
isManaged,
uid,
} = requestInfo
Expand Down Expand Up @@ -73,9 +73,11 @@ function RequestDetailsScreen() {
<Card.Text>Date: {date}</Card.Text>
<Card.Text>Number of bages: {numBag}</Card.Text>
<Card.Text>Contuct Number: {contact}</Card.Text>
<Card.Text>Location: {location}</Card.Text>
<Card.Text>
Location: {area}, {district}
</Card.Text>
<Card.Text>Bag managed: {numManaged}</Card.Text>
<Card.Text>Total response: {response.length}</Card.Text>
{/* <Card.Text>Total response: {response.length}</Card.Text> */}
</Card.Body>
{uid === userInfo.uid ? (
<Link to={`/feed/${id}/edit`}>
Expand Down
Loading

0 comments on commit 6975ee9

Please sign in to comment.