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

send beneficiary to agency #9

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 12 additions & 3 deletions src/contexts/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import APP_ACTIONS from '../actions/appActions';

const initialState = {
aidConnectId:null,
isActive:false
isActive:false,
projectName:'',
projectId:''
};

export const AppContext = createContext(initialState);
Expand All @@ -16,17 +18,24 @@ const [state, dispatch] = useReducer(appReduce, initialState);

const checkAidConnectId = useCallback(async (_aidConnectId) => {
const {data} = await Service.checkAidConnectId(_aidConnectId);
dispatch({ type: APP_ACTIONS.SET_AID_CONNECT_ID, data: {aidConnectId:data.id,isActive:data.isActive} });
dispatch({ type: APP_ACTIONS.SET_AID_CONNECT_ID,
data: {aidConnectId:data.id,isActive:data.isActive,projectName:data.projectName,projectId:data.projectId} });
},[])

const sendBeneficiaries = async(beneficiaries) => {
const data = await Service.sendBeneficiaries(state.aidConnectId,beneficiaries);
return data;
}



return (
<AppContext.Provider
value={
{
...state,
checkAidConnectId
checkAidConnectId,
sendBeneficiaries
}
}
>
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/registerBeneficiaryContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import BENEFICIARY_ACTIONS from '../actions/beneficiaryActions';
const initialState = {
name: '',
phone: '',
gender: '',
gender: 'U',
dob: '',
agency: '',
email: '',
Expand Down
8 changes: 6 additions & 2 deletions src/modules/beneficiary/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {AppContext} from '../../contexts/AppContext'
export default function Preview() {
const history = useHistory();
const {aidConnectId} = useContext(AppContext);
const { name, phone, address, gender, photo, govt_id_image, resetBeneficiary } =
const { name, phone, address, gender, photo,email,govt_id, govt_id_image, resetBeneficiary } =
useContext(RegisterBeneficiaryContext);

const handleRegister = async event => {
Expand All @@ -22,9 +22,13 @@ export default function Preview() {
name,
phone,
address,
email: email || '',
gender,
govt_id: govt_id || '',
photo,
govt_id_image,
createdAt: Date.now()
createdAt: Date.now(),
shared:false
});
resetBeneficiary();
history.push(`/${aidConnectId}`);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/beneficiary/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const AddBeneficiary = () => {
className="form-control"
name="address_temporary"
placeholder="Enter temporary address"
value={address_temporary}
value={address_temporary ? address_temporary : ''}
onChange={updateBeneficiaryData}
/>
<i className="clear-input">
Expand Down Expand Up @@ -170,7 +170,7 @@ const AddBeneficiary = () => {
<label className="label" htmlFor="gender">
Gender
</label>
<select className="form-control custom-select" id="gender">
<select className="form-control custom-select" name="gender" onChange={updateBeneficiaryData}>
<option value="U">Select gender</option>
<option value="M">Male</option>
<option value="F">Female</option>
Expand Down
35 changes: 31 additions & 4 deletions src/modules/beneficiary/share.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import React, { useState, useEffect, useCallback ,useContext} from 'react';
import { Link } from 'react-router-dom';
import Swal from 'sweetalert2';
import AppHeader from '../layouts/AppHeader';
import { IoHomeOutline, IoEye } from 'react-icons/io5';
import { Button } from 'react-bootstrap';
// import { useHistory } from 'react-router-dom';
import { useHistory } from 'react-router-dom';
import DataService from '../../services/db';
import {AppContext} from '../../contexts/AppContext';
import Info from '../global/Info';


const BeneficiaryList = () => {
// const history = useHistory();
const {aidConnectId,isActive} = useContext(AppContext);
const history = useHistory();
const {aidConnectId,isActive,sendBeneficiaries} = useContext(AppContext);
const [beneficiary, setBeneficiary] = useState([]);
const [selectAll, setSelectAll] = useState(false);
const [selectedBeneficiary, setSelectedBeneficiary] = useState([]);
Expand Down Expand Up @@ -40,7 +41,33 @@ const BeneficiaryList = () => {
} else setSelectedBeneficiary([...selectedBeneficiary, phone]);
};

const handleShare = () => {};
const filterSharedBeneficiary = beneficiaries => {
const unSharedBeneficiary = beneficiaries.filter((el) => !el.shared);
return unSharedBeneficiary.map((el)=>{
const {shared,...rest} = el;
return rest
})
}

const handleShare = async () => {
if(!selectedBeneficiary.length) return Swal.fire('Error', 'Beneficiary Not Selected', 'warning');
const beneficiaries = await Promise.all(selectedBeneficiary.map(async (el)=>{
const {name,phone,address,email,gender,govt_id,shared} = await DataService.getBeneficiary(el);
return {name,phone,address,email,gender,govt_id,shared}
}))
const filteredBen = filterSharedBeneficiary(beneficiaries);
if(!filteredBen.length) return Swal.fire('Error', 'Selected Beneficiaries Already Shared', 'warning');

const d= await sendBeneficiaries(filteredBen);
if(d && d.inserted){
await Promise.all(selectedBeneficiary.map(async (el)=>{
await DataService.updateBeneficiary(el,{shared:true})
}))
await Swal.fire('Success',`Selected Beneficiaries Shared to Agency`,'success')
return history.push(`/${aidConnectId}`)
}
Swal.fire('Error',`Something Went Workng! Contact Admin`,'error')
};

useEffect(() => {
getAllBeneficiary();
Expand Down
4 changes: 2 additions & 2 deletions src/modules/home/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {AppContext} from '../../contexts/AppContext';

export default function Main() {
const [totalBen, setTotalBen] = useState(0);
const {aidConnectId} = useContext(AppContext);
const {aidConnectId,projectName} = useContext(AppContext);
const getTotalBeneficiary = useCallback(async () => {
const beneficiaries = await DataService.listBeneficiaries();
setTotalBen(beneficiaries.length);
Expand All @@ -22,7 +22,7 @@ export default function Main() {
<div className="section wallet-card-section pt-2">
<div className="wallet-card">
<div className="balance">
<h3 className="title">Wash Programme</h3>
<h3 className="title">{projectName}</h3>
</div>
<div className="balance mt-2">
<div className="left">
Expand Down
4 changes: 3 additions & 1 deletion src/reducers/appReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const AppReducer = (state, action) => {
return {
...state,
aidConnectId: action.data.aidConnectId,
isActive: action.data.isActive
isActive: action.data.isActive,
projectName: action.data.projectName,
projectId: action.data.projectId
};

case APP_ACTIONS.SET_AID_CONNECT_STATUS:
Expand Down
1 change: 1 addition & 0 deletions src/reducers/beneficiaryReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const AppReducer = (state, action) => {
agency: action.data.agency,
email: action.data.email,
address: action.data.address,
address_temporary: action.data.address_temporary,
govt_id: action.data.govt_id,
project: action.data.project,
extras: {
Expand Down
6 changes: 5 additions & 1 deletion src/services/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DB } from '../constants';
const db = new Dexie(DB.NAME);

db.version(DB.VERSION).stores({
beneficiaries: 'phone,name,address,createdAt,photo,govt_id_image',
beneficiaries: 'phone,name,address,email,gender,govt_id,createdAt,photo,govt_id_image,shared',
agencies:
'address,name,api,network,rahatAddress,tokenAddress,erc20Address,erc1155Address,adminAddress,phone,email,logo,isApproved',
projects: 'id,name',
Expand Down Expand Up @@ -73,6 +73,10 @@ const DataService = {
return db.beneficiaries.get({ type }).orderBy('createdAt').reverse();
},

async updateBeneficiary(key, data) {
return db.beneficiaries.update(key, data);
},

};

export default DataService;
21 changes: 18 additions & 3 deletions src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ import axios from 'axios';
import API from '../constants/api';


export async function checkAidConnectId(id) {
export async function checkAidConnectId(aidConnectId) {
try {
if (!id) throw new Error('Must send id');
const {data} = await axios.get(`${API.AID_CONNECT}/${id}`);
if (!aidConnectId) throw new Error('Must send aidConnectId');
const {data} = await axios.get(`${API.AID_CONNECT}/${aidConnectId}`);
return data;
} catch (e) {
throw Error(e);
}
}

export async function sendBeneficiaries(aidConnectId,payload) {
try {
if (!aidConnectId) throw new Error('Must send aidConnectId');
const {data} = await axios({
url:`${API.AID_CONNECT}/${aidConnectId}/beneficiaries`,
method: 'post',
data: payload
});
return data;
} catch (e) {
console.log(e)
throw Error(e);
}
}