-
Notifications
You must be signed in to change notification settings - Fork 19
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
stateManagement using react context api #132
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React, { createContext, useContext, useReducer } from "react"; | ||
|
||
export const DataLayerContext = createContext(); | ||
|
||
export const DataLayer = ({ reducer, initialState, children }) => ( | ||
<DataLayerContext.Provider value={useReducer(reducer, initialState)}> | ||
{children} | ||
</DataLayerContext.Provider> | ||
); | ||
const useDataLayerValue = () => useContext(DataLayerContext); | ||
|
||
export { useDataLayerValue }; | ||
|
||
export default DataLayer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const initialState = { | ||
states: [], | ||
districts: [], | ||
vaccineData: [], | ||
contributors: [], | ||
toSearch: [ | ||
"Find By District", | ||
"Find By PinCode & Date", | ||
"Find By Pincode & Date(Slots for next 7 days)", | ||
"Find By District & Date(Slots for next 7 days)", | ||
], | ||
|
||
toSearchValue: "", | ||
stateCode: "States", | ||
districtCode: "PLEASE SELECT A STATE FIRST", | ||
vaccinePerPage: 3, | ||
currentPage: 1, | ||
}; | ||
|
||
const reducer = (state, action) => { | ||
console.log(action); | ||
switch (action.type) { | ||
case "SET_STATES": | ||
return { | ||
...state, | ||
states: action.states, | ||
}; | ||
case "SET_DISTRICTS": | ||
return { | ||
...state, | ||
districts: action.districts, | ||
}; | ||
|
||
case "SET_VACCINEDATA": | ||
return { | ||
...state, | ||
vaccineData: action.vaccineData, | ||
}; | ||
case "SET_CONTRIBUTORS": | ||
return { | ||
...state, | ||
contributors: action.contributors, | ||
}; | ||
|
||
case "SET_TOSEARCHVALUE": | ||
return { | ||
...state, | ||
toSearchValue: action.toSearchValue, | ||
}; | ||
|
||
case "SET_STATECODE": | ||
return { | ||
...state, | ||
stateCode: action.stateCode, | ||
}; | ||
case "SET_DISTRICTCODE": | ||
return { | ||
...state, | ||
districtCode: action.districtCode, | ||
}; | ||
|
||
case "SET_CURRENTPAGE": | ||
return { | ||
...state, | ||
currentPage: action.currentPage, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export { initialState }; | ||
|
||
export default reducer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,27 +17,27 @@ import SearchIcon from "@material-ui/icons/Search"; | |
import "./Home.css"; | ||
import VaccineDataMain from "../VaccineData/VaccineDataMain"; | ||
import Pagination from "../Pagination/Pagination"; | ||
import { useDataLayerValue } from "../../Context/DataLayer"; | ||
|
||
const Home = () => { | ||
const [state, setState] = useState([]); | ||
const [stateCode, setStateCode] = useState("States"); | ||
const [districts, setDistricts] = useState([]); | ||
const [districtCode, setDistrictCode] = useState( | ||
"PLEASE SELECT A STATE FIRST" | ||
); | ||
const [ | ||
{ | ||
states, | ||
districts, | ||
vaccineData, | ||
toSearch, | ||
toSearchValue, | ||
stateCode, | ||
districtCode, | ||
vaccinePerPage, | ||
currentPage, | ||
}, | ||
dispatch, | ||
] = useDataLayerValue(); | ||
|
||
const [pin, setPin] = useState(""); | ||
const [formattedDate, setFormattedDate] = useState(""); | ||
const [selectedDate, setSelectedDate] = useState(new Date()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Justinnn07 why are the above states not used under context? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These states most prob. won't be used outside these components , so thought of maintaining as it is! Well, if you want i can specify them under context |
||
const [vaccineData, setVaccineData] = useState([]); | ||
const [toSearchValue, setToSearchValue] = useState(""); | ||
const [toSearch] = useState([ | ||
"Find By District", | ||
"Find By PinCode & Date", | ||
"Find By Pincode & Date(Slots for next 7 days)", | ||
"Find By District & Date(Slots for next 7 days)", | ||
]); | ||
const [currentPage, setCurrentPage] = useState(1); | ||
const [vaccinePerPage] = useState(3); | ||
const indexOfLastVaccine = currentPage * vaccinePerPage; | ||
const indexOfFirstVaccine = indexOfLastVaccine - vaccinePerPage; | ||
const currentVaccine = vaccineData.slice( | ||
|
@@ -52,22 +52,35 @@ const Home = () => { | |
} | ||
|
||
const paginate = (pageNumber) => { | ||
setCurrentPage(pageNumber); | ||
dispatch({ | ||
type: "SET_CURRENTPAGE", | ||
currentPage: pageNumber, | ||
}); | ||
}; | ||
|
||
const nextPage = () => { | ||
setCurrentPage(currentPage + 1); | ||
|
||
dispatch({ | ||
type: "SET_CURRENTPAGE", | ||
currentPage: currentPage + 1, | ||
}); | ||
if (currentPage + 1 > pageNumber.length) { | ||
setCurrentPage(pageNumber.length); | ||
dispatch({ | ||
type: "SET_CURRENTPAGE", | ||
currentPage: pageNumber.length, | ||
}); | ||
} | ||
}; | ||
|
||
const prevPage = () => { | ||
setCurrentPage(currentPage - 1); | ||
|
||
dispatch({ | ||
type: "SET_CURRENTPAGE", | ||
currentPage: currentPage - 1, | ||
}); | ||
if (currentPage - 1 <= 0 && pageNumber.length) { | ||
setCurrentPage(pageNumber.slice(0, 1)); | ||
dispatch({ | ||
type: "SET_CURRENTPAGE", | ||
currentPage: pageNumber.slice(0, 1), | ||
}); | ||
} | ||
}; | ||
|
||
|
@@ -83,27 +96,41 @@ const Home = () => { | |
fetch("https://cdn-api.co-vin.in/api/v2/admin/location/states") | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
setState(data.states); | ||
dispatch({ | ||
type: "SET_STATES", | ||
states: data.states, | ||
}); | ||
}); | ||
GetFormattedDate(); | ||
// eslint-disable-next-line | ||
}, [selectedDate, formattedDate]); | ||
|
||
const handleDateChange = (date) => { | ||
setSelectedDate(date); | ||
setVaccineData([]); | ||
setDistrictCode(""); | ||
setCurrentPage(1); | ||
dispatch({ | ||
type: "SET_DISTRICTCODE", | ||
districtCode: "", | ||
}); | ||
dispatch({ | ||
type: "SET_CURRENTPAGE", | ||
currentPage: 1, | ||
}); | ||
}; | ||
|
||
const onStateChange = async (e) => { | ||
const stateCode = e.target.value; | ||
|
||
setDistricts([]); | ||
|
||
setCurrentPage(1); | ||
|
||
setVaccineData([]); | ||
dispatch({ | ||
type: "SET_DISTRICTS", | ||
districts: [], | ||
}); | ||
dispatch({ | ||
type: "SET_CURRENTPAGE", | ||
currentPage: 1, | ||
}); | ||
dispatch({ | ||
type: "SET_VACCINEDATA", | ||
vaccineData: [], | ||
}); | ||
|
||
const url = | ||
stateCode === "States" | ||
|
@@ -113,15 +140,23 @@ const Home = () => { | |
await fetch(url) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
setStateCode(stateCode); | ||
setDistricts(data.districts); | ||
dispatch({ | ||
type: "SET_STATECODE", | ||
stateCode: stateCode, | ||
}); | ||
dispatch({ | ||
type: "SET_DISTRICTS", | ||
districts: data.districts, | ||
}); | ||
}); | ||
}; | ||
|
||
const findByDistrict = async (e) => { | ||
const districtCode = e.target.value; | ||
setCurrentPage(1); | ||
|
||
dispatch({ | ||
type: "SET_CURRENTPAGE", | ||
currentPage: 1, | ||
}); | ||
const url = | ||
districtCode === "PLEASE SELECT A STATE FIRST" | ||
? null | ||
|
@@ -130,8 +165,15 @@ const Home = () => { | |
await fetch(url) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
setDistrictCode(districtCode); | ||
setVaccineData(data.sessions); | ||
dispatch({ | ||
type: "SET_DISTRICTCODE", | ||
districtCode: districtCode, | ||
}); | ||
|
||
dispatch({ | ||
type: "SET_VACCINEDATA", | ||
vaccineData: data.sessions, | ||
}); | ||
}); | ||
}; | ||
|
||
|
@@ -163,7 +205,15 @@ const Home = () => { | |
fee_type: res?.fee_type, | ||
slots: res?.sessions?.slice(0, 1).map((res) => res.slots), | ||
})); | ||
setVaccineData(pincodeData); | ||
dispatch({ | ||
type: "SET_VACCINEDATA", | ||
vaccineData: pincodeData, | ||
}); | ||
|
||
dispatch({ | ||
type: "SET_CURRENTPAGE", | ||
currentPage: 1, | ||
}); | ||
}); | ||
} | ||
}; | ||
|
@@ -177,8 +227,10 @@ const Home = () => { | |
) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
console.log(data); | ||
setVaccineData(data.sessions); | ||
dispatch({ | ||
type: "SET_VACCINEDATA", | ||
vaccineData: data.sessions, | ||
}); | ||
}); | ||
} | ||
}; | ||
|
@@ -201,8 +253,14 @@ const Home = () => { | |
variant="filled" | ||
value={toSearchValue} | ||
onChange={(e) => { | ||
setToSearchValue(e.target.value); | ||
setVaccineData([]); | ||
dispatch({ | ||
type: "SET_TOSEARCHVALUE", | ||
toSearchValue: e.target.value, | ||
}); | ||
dispatch({ | ||
type: "SET_VACCINEDATA", | ||
vaccineData: [], | ||
}); | ||
}} | ||
> | ||
{toSearch.map((functionName, index) => { | ||
|
@@ -233,7 +291,7 @@ const Home = () => { | |
onChange={onStateChange} | ||
> | ||
<MenuItem value="States">Select a State</MenuItem> | ||
{state?.map((stateData) => ( | ||
{states?.map((stateData) => ( | ||
<MenuItem value={stateData?.state_id}> | ||
{stateData?.state_name} | ||
</MenuItem> | ||
|
@@ -290,7 +348,7 @@ const Home = () => { | |
onChange={onStateChange} | ||
> | ||
<MenuItem value="States">Select a State</MenuItem> | ||
{state?.map((stateData) => ( | ||
{states?.map((stateData) => ( | ||
<MenuItem value={stateData?.state_id}> | ||
{stateData?.state_name} | ||
</MenuItem> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Justinnn07 why the name of the folder was changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Themes are not under components right ? so, thought of making a folder for themes