Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
transaction form update
  • Loading branch information
nandcoder committed Apr 14, 2023
commit 2f0216aa755be63078b1fde4b6d2c6815170fa81
226 changes: 187 additions & 39 deletions src/pages/Transaction/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { useContext, useState } from "react";
import { FormControl, FormLabel, FormErrorMessage, Input, Box, Heading, useToast } from "@chakra-ui/react";
import { Button, Container, Modal } from "react-bootstrap";
import React, { useContext, useEffect, useState } from "react";
import { FormControl, FormLabel, FormErrorMessage, Input, Box, Heading, useToast, Select, Checkbox, Stack, CheckboxGroup } from "@chakra-ui/react";
import { Button, Container, Form, Modal } from "react-bootstrap";
import firebase from "firebase";
import TransactionTable from "../../components/TransactionTable";
import { useForm } from "react-hook-form";
import { addTransactionResolver } from "../../utils/validator/addTransactionResolver";
import { db } from "../../utils/firebase";
import FilterListIcon from '@mui/icons-material/FilterList';
import AddRoundedIcon from '@mui/icons-material/AddRounded';
import { AuthContext } from "../../components/Authentication/AuthProvider";
import { DataContext } from "../../components/Authentication/DataProvider";
import Loader from "../../components/Loader";

const Transaction = () => {
const { transactions, users, requestTransactions, setRequestTransactions } = useContext(DataContext)
const { user } = useContext(AuthContext)
const { groups, transactions, users, requestTransactions, setRequestTransactions } = useContext(DataContext)
const [currentGroup, setCurrentGroup] = useState({});
const [involved, setInvolved] = useState([]);
const [showAdd, setShowAdd] = useState(false);
const handleClose = () => setShowAdd(false);
const [showGroup, setShowGroup] = useState(false);
const [payerId, setPayerId] = useState(user.uid);
const handleCloseGroup = () => setShowGroup(false);
const handleCloseAdd = () => setShowAdd(false);
const handleShowGroup = () => setShowGroup(true);
const handleShowAdd = () => setShowAdd(true);
const toast = useToast();
const {
Expand All @@ -24,20 +32,27 @@ const Transaction = () => {
// setError,
// clearErrors,
} = useForm({ resolver: addTransactionResolver });
const addTransaction = ({ title, desc, amount, payer, involved }) => {
console.log(title, desc, amount, payer, involved);
const addTransaction = (e) => {
const { title, desc, day, amount, payer } = e
console.log(e);
const firstLetter = title.charAt(0);
const firstLetterCap = firstLetter.toUpperCase();
const remainingLetters = title.slice(1);
const finalTitle = firstLetterCap + remainingLetters
const date = new Date().toISOString()
const finalDoc = {
datetime: firebase.database.ServerValue.TIMESTAMP,
datetime: date,
title: finalTitle,
desc,
groupId: currentGroup.id,
groupTitle: currentGroup.data.title,
day,
amount,
paidBy: payer,
involved,
status: "pending",
}
console.log(finalDoc);

db.collection("transactions").add(finalDoc)
.then((ref) => {
Expand All @@ -57,24 +72,103 @@ const Transaction = () => {
console.error("Error writing document: ", error);
})
.finally(() => {
handleClose()
handleCloseAdd()
setRequestTransactions(!requestTransactions)
})

}
const handleFilter = () => {

}

const handleGroupSelector = (e) => {
e.preventDefault()
const grpId = e.target.group.value
let curr;
groups.forEach(group => {
if (group.id === grpId) {
curr = group
}
});
setCurrentGroup(curr)
handleCloseGroup()
handleShowAdd()
}
const handleChange = (event) => {
const payerId = event.target.value;
// const arr = []
// groups.forEach(group => {
// if (group.id === groupId) {
// group.data.members.forEach(member => {
// arr.push(member)
// });
// }
// });
setPayerId(payerId)
// setCurrentGroup(event.target.value)
};
const handleInvolved = (e) => {
console.log(e);
const value = e.target.value;
console.log(value);
// const prev = involved;
// prev.push(value)
// setInvolved(prev)
if (!e.target.checked) {
const prev = involved.filter(val => value !== val)
// involved.forEach(memberId => {
// if (memberId !== value){
// prev.push(memberId)
// }
// });
console.log('pop', prev);
setInvolved(prev)
}
if (e.target.checked) {
const prev = involved;
prev.push(value)
console.log('push', prev);
setInvolved(prev)
}
}
useEffect(() => {
if (Object.keys(currentGroup).length !== 0) setInvolved(currentGroup.data.members)
}, [currentGroup]);
console.log(involved);


return (
<Container style={{ width: "80%" }}>
<Heading display={"inline-block"} margin={"2%"}>Transactions</Heading>
<Button variant="dark" style={{ float: "right", margin: "10px", borderRadius: "50%", backgroundColor: "black", height: "50px", width: "50px", color: '#04bef8' }} onClick={handleShowAdd}><AddRoundedIcon /></Button>
<Button variant="dark" style={{ float: "right", margin: "10px", borderRadius: "50%", backgroundColor: "black", height: "50px", width: "50px", color: '#04bef8' }} onClick={handleShowGroup}><AddRoundedIcon /></Button>
<Button variant="dark" style={{ float: "right", margin: "10px", borderRadius: "50%", backgroundColor: "black", height: "50px", width: "50px", color: '#04bef8' }} onClick={handleFilter}><FilterListIcon /></Button>
<br />
<Modal show={showAdd} onHide={handleClose}>
<Modal show={showGroup} onHide={handleCloseGroup}>
<Modal.Header closeButton>
<Modal.Title>Select Group</Modal.Title>
</Modal.Header>
<form onSubmit={handleGroupSelector}>
<Modal.Body>
<FormControl>
<FormLabel htmlFor="group">Group</FormLabel>
<Select required name='group' placeholder='Select a group' >
{groups?.map((group) => (
<option key={group.id} value={group.id}>{group.data.title}</option>
))}
</Select>
</FormControl>

</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleCloseGroup}>
Close
</Button>
<Button type="submit" variant="info">
Next
</Button>
</Modal.Footer>
</form>
</Modal>
<Modal show={showAdd} onHide={handleCloseAdd}>
<Modal.Header closeButton>
<Modal.Title>Add Transaction</Modal.Title>
</Modal.Header>
Expand All @@ -85,7 +179,7 @@ const Transaction = () => {
<Input
type="text"
name="title"
placeholder="Enter Title"
placeholder="Enter title"
{...register("title")}
/>
<FormErrorMessage>
Expand All @@ -105,41 +199,105 @@ const Transaction = () => {
{errors.desc && errors.desc.message}
</FormErrorMessage>
</FormControl>
<FormControl marginTop="2" isInvalid={errors.day}>
<FormLabel htmlFor="day">Day</FormLabel>
<Input
type="number"
name="day"
placeholder="Enter day of trip"
{...register("day")}
/>
<FormErrorMessage>
{errors.day && errors.day.message}
</FormErrorMessage>
</FormControl>

<FormControl marginTop="2" isInvalid={errors.amount}>
<FormLabel htmlFor="amount">Amount</FormLabel>
<Input
type="number"
name="amount"
placeholder="Enter Amount"
placeholder="Enter amount"
{...register("amount")}
/>
<FormErrorMessage>
{errors.amount && errors.amount.message}
</FormErrorMessage>
</FormControl>

<FormControl mt="2" isInvalid={errors.payer}>
<FormControl isInvalid={errors.payer}>
<FormLabel htmlFor="payer">Paid By:</FormLabel>
<Input
type="email"
{/* <Input
type="text"
name="payer"
placeholder="Enter payers' email"
placeholder="Select a payer"
{...register("payer")}
/>
/> */}
<Select isInvalid={errors.payer} onChangeCapture={handleChange} name='payer' placeholder='Select payer' {...register("payer")} >
{Object.keys(users).length !== 0 && Object.keys(currentGroup).length !== 0 && currentGroup.data.members?.map((memberId) => (
<option key={memberId} value={memberId}>{users[memberId].name}</option>
))}
</Select>
<FormErrorMessage>
{errors.payer && errors.payer.message}
</FormErrorMessage>
</FormControl>

<FormControl mt="2" isInvalid={errors.involved}>
<FormLabel htmlFor="involved">Add user involved</FormLabel>
<Input
type="email"
name="involved"
placeholder="Enter email of user involved"
{...register("involved")}
/>
<FormControl as='fieldset' mt="2">
<FormLabel htmlFor="involved">Add members involved</FormLabel>
{Object.keys(users).length !== 0 && Object.keys(currentGroup).length !== 0 && involved.length !== 0 && (
<React.Fragment>


{/* <Form>
<Form.Group className="mb-3" controlId="exampleForm.ControlInput1">
<Form.Label>Email address</Form.Label>
{['checkbox', 'checkbox'].map((type) => (
<div key={`default-${type}`} className="mb-3">
<Form.Check
type={type}
id={`default-${type}`}
label={`default ${type}`}
/>
</div>
))}
</Form.Group>
</Form> */}
{/* <CheckboxGroup defaultValue='Itachi'>
<Stack spacing='24px'>
<Checkbox name="involved" onSelect={handleInvolved} id="1" value='Sasuke'>Sasuke</Checkbox>
<Checkbox name="involved" onSelect={handleInvolved} id="2" value='Nagato'>Nagato</Checkbox>
<Checkbox name="involved" onSelect={handleInvolved} id="3" value='Itachi'>Itachi</Checkbox>
<Checkbox name="involved" onSelect={handleInvolved} id="4" value='Sage of the six Paths'>Sage of the six Paths</Checkbox>
</Stack>
</CheckboxGroup> */}
{/* <FormHelperText>Select only if you're a fan.</FormHelperText> */}

<CheckboxGroup colorScheme='green' value={involved} >
<Stack spacing={[1, 5]} direction={['column', 'row']}>
{payerId && involved.length !== 0 && currentGroup.data.members?.map(memberId => (
<FormLabel>
<input
type="checkbox"
checked={involved.includes(memberId)}
disabled={memberId === payerId}
// name="involved"
// id="field-5"
// {...register('involved')}
onChange={handleInvolved}
// className="chakra-input css-1c6j008"
value={memberId}
placeholder={"Enter email to add"}
/>
{users[memberId].name}
{/* <Checkbox isDisabled={memberId === payerId} key={users[memberId].userId} value={users[memberId].userId}>{users[memberId].name}</Checkbox> */}
</FormLabel>
))}
</Stack>
</CheckboxGroup>
</React.Fragment>

)}
<FormErrorMessage>
{errors.involved && errors.involved.message}
</FormErrorMessage>
Expand All @@ -148,23 +306,13 @@ const Transaction = () => {
<Box mt="5" color="red.500">
{errors.API_ERROR && errors.API_ERROR.message}
</Box>
{/* <Button
isLoading={isSubmitting}
// onClick={addInvolved}
mt={4}
variant="info"
type="submit"
w="100%"
>
Add
</Button> */}

</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
<Button variant="secondary" onClick={handleCloseAdd}>
Close
</Button>
<Button isLoading={isSubmitting} type="submit" variant="success">
<Button type="submit" variant="success">
Submit
</Button>
</Modal.Footer>
Expand Down
Loading