-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom hooks and context api added, refactored
- Loading branch information
1 parent
b3a320e
commit f82fff2
Showing
18 changed files
with
92 additions
and
188 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from "react"; | ||
const DataContext = React.createContext(); | ||
import useRequest from "../hooks/useRequest"; | ||
|
||
const DataProvider = ({ children, baseUrl, routeName }) => { | ||
const state = useRequest(baseUrl, routeName); | ||
return <DataContext.Provider value={state}>{children}</DataContext.Provider>; | ||
}; | ||
|
||
export { DataContext, DataProvider }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React, { useEffect, useReducer } from "react"; | ||
import requestReducer, { REQUEST_STATUS } from "../reducers/request"; | ||
import axios from "axios"; | ||
import { | ||
GET_ALL_FAILURE, | ||
GET_ALL_SUCCESS, | ||
PUT_FAILURE, | ||
PUT_SUCCESS, | ||
} from "../actions/request"; | ||
|
||
const useRequestSimple = (baseUrl, routeName) => { | ||
const [{ records, status, error }, dispatch] = useReducer(requestReducer, { | ||
status: REQUEST_STATUS.LOADING, | ||
records: [], | ||
error: null, | ||
}); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
try { | ||
const response = await axios.get(`${baseUrl}/${routeName}`); | ||
dispatch({ | ||
type: GET_ALL_SUCCESS, | ||
records: response.data, | ||
}); | ||
} catch (e) { | ||
console.log("Loading data error", e); | ||
|
||
dispatch({ | ||
type: GET_ALL_FAILURE, | ||
error: e, | ||
}); | ||
} | ||
}; | ||
fetchData(); | ||
}, [baseUrl, routeName]); | ||
|
||
const propsLocal = { | ||
records, | ||
status, | ||
error, | ||
put: async (record) => { | ||
try { | ||
await axios.put(`${baseUrl}/${routeName}/${record.id}`, record); | ||
dispatch({ | ||
type: PUT_SUCCESS, | ||
record: record, | ||
}); | ||
} catch (e) { | ||
dispatch({ | ||
type: PUT_FAILURE, | ||
error: e, | ||
}); | ||
} | ||
}, | ||
}; | ||
return propsLocal; | ||
}; | ||
|
||
export default useRequestSimple; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.