Skip to content

Instantly share code, notes, and snippets.

View intojs's full-sized avatar

Daniel Dughila intojs

View GitHub Profile
@intojs
intojs / handle-gists.4.test.js
Created December 20, 2017 23:18
FP in Node.js - handle-gists.4.test.js
import {getGistMock} from "./mocks/gist.mock";
import {getLinkHeaderMock} from "./mocks/link-header.mock";
import {getLinkHeaderStringMock} from "./mocks/link-header-string.mock";
import {getGistsUrlMock} from "./mocks/gists-url.mock";
import {getProcessedGistMock} from "./mocks/processed-gist.mock";
import {
getDescription,
getGistsFactory,
getNextUrlFactory,
handleGistsRouteFactory,
const MVCComponent = () => {
const [model, dispatch] = useReducer(reducer, initialModel);
const handleEvent = () => {
dispatch(action);
};
return <View model={model} handleEvent={handleEvent} />;
};
const withValidation = (state, field, value) => {
const changedState = {
...state,
fields: {
...state.fields,
[field]: {
...state.fields[field],
value,
},
},
@intojs
intojs / NewsletterFormModel.js
Last active February 21, 2021 20:21
React MVC - Initial model
const NewsletterFormModel = {
isLoading: true,
isValid: false,
fields: {
fullName: {
value: "",
},
newsletter: {
value: "",
options: [],
const NewsletterForm = ({ newsletterOptions, onSubmit }) => {
const [model, dispatch] = useReducer(reducer, initialModel);
useEffect(() => {
dispatch(actions.setNewsletterOptions(newsletterOptions));
}, [newsletterOptions]);
const handleFieldChange = (change) => {
dispatch(actions.changeField(change));
};
const isNewsletterFormModelValid = (model) => {
const {
fields: { fullName, newsletter },
} = model;
return !!(fullName.value && fullName.value.length > 2 && newsletter.value);
};
const setNewsletterOptions = (newsletterOptions) => ({
type: actionTypes.SET_NEWSLETTERS_OPTIONS,
payload: { newsletterOptions },
});
const changeField = (change) => ({
type: actionTypes.CHANGE_FIELD,
payload: change,
});
const newsletterFormReducer = (state, action) => {
const { type, payload } = action;
switch (type) {
case actionTypes.SET_NEWSLETTERS_OPTIONS:
return {
...state,
isLoading: payload.newsletterOptions.length === 0,
fields: {
...state.fields,
let counter = 0;
const count = () => {
counter += 1;
}
const NewsletterFormView = (props) => {
const classes = useStyles();
const {
model: {
isLoading,
isValid,
fields: { newsletter },
},
onFieldChange,