Skip to content

Instantly share code, notes, and snippets.

View rowlandekemezie's full-sized avatar
🏠
Working from home

Rowland I. Ekemezie rowlandekemezie

🏠
Working from home
View GitHub Profile
@rowlandekemezie
rowlandekemezie / ajaxWithReduxSaga.js
Last active January 11, 2024 06:46
A basic implementation of AJAX with redux-saga
const { applyMiddleware, createStore } = Redux;
const createSagaMiddleware = ReduxSaga.default;
const { put, call } = ReduxSaga.effects;
const { takeLatest } = ReduxSaga;
const { connect, Provider } = ReactRedux;
// GitHub API
const gitHubApi = (username) => {
return fetch(`https://api.github.com/users/${username}`)
.then(response => {
@rowlandekemezie
rowlandekemezie / Instructions.js
Created March 10, 2018 19:05
Update and Delete request with redux-saga
/*
1) Depending on your experience, this is all you might need to do to hook up your workflow
2) Remember, not to copy and paste. My idea is to show you how things fit it to you can tailor it to your application
GRACIAS 💪
*/
// Current. The request is synchronous because the former needs to finish before the later
const earning = await fetchLastMonthEarning(teacherInfo.teacher.teacherId, startDate, endDate);
const teachingHours = await fetchTeachingHours(teacherInfo.teacher.teacherId);
// Refactored. Ensures the request is run concurrently
const [ earning, teachingHours ]= await Promise.all([
fetchLastMonthEarning(teacherInfo.teacher.teacherId, startDate, endDate),
fetchTeachingHours(teacherInfo.teacher.teacherId)
])
@rowlandekemezie
rowlandekemezie / sample-test.js
Created September 2, 2019 19:41
How you can nuke beforeEach and ensure each test spec is totally isolated from others
// Define a setup function you can reuse and maybe nuke beforeEach completely.
// Your test spec is currently too simple to even require the hasle beforeEach;
function setUp() {
const props = {
strategy: null,
event: mockEvent, // You can use jest to mock
eventId: mockEventId,
ConfigLogo: null,
handleDuplicateEvent: action("select provider")
}
// Dependencies
const { applyMiddleware, createStore } = Redux;
const createSagaMiddleware = ReduxSaga.default;
const { put, call, takeLatest } = ReduxSaga.effects;
const { connect, Provider } = ReactRedux;
// GitHub API
const gitHubApi = (username) => {
// Make API calls here
};
@rowlandekemezie
rowlandekemezie / Sagas.js
Last active March 23, 2019 12:34
Saga for making calls to gitHubApi
// Here, we use ES6 destructuring assignment to extract payload
// from the action object passed to it.
function* loadUserDetails({ payload }) {
try {
const user = yield call(gitHubApi, payload);
// Yields effect to the reducer specifying action type
// and user details.
yield put({type: 'LOAD_USER_SUCCESS', user});
} catch (error) {
yield put({ type: 'LOAD_USER_FAILURE', error });
@rowlandekemezie
rowlandekemezie / AjaxWithReduxThunk.js
Last active March 23, 2019 12:30
Using redux thunk to for async
// Dependencies
const { applyMiddleware, createStore } = Redux;
const { connect, Provider } = ReactRedux;
// GitHub API
const gitHubApi = (username) => {
// Put your Api call here
};
// redux-thunk implementation
@rowlandekemezie
rowlandekemezie / ajaxWithinReactComponent.js
Last active March 23, 2019 12:22
A basic implementation of Ajax within the react component.
// UserProfile Component
class UserProfile extends React.Component {
constructor() {
super();
this.state = {
user: []
}
}
// componentDidMount lifecycle method allows dynamic behaviour, AJAX,
// side effects, etc. We issue our API call here and set the
@rowlandekemezie
rowlandekemezie / range.js
Created January 29, 2018 21:53
Utility functions
// Create a range of values
function range(start, end) {
return Array.apply(null, Array(end - start + 1))
.map((_, index) => index + start);
}
@rowlandekemezie
rowlandekemezie / custom_radio_button.scss
Created January 17, 2018 18:52
My CSS trick in creating a custom radio button
input[type="radio"] {
position: absolute;
left: -9999px;
}
.label {
display: block;
position: relative;
margin-left: 0;
padding: 3px 0 3px 25px;