Skip to content

Instantly share code, notes, and snippets.

View intojs's full-sized avatar

Daniel Dughila intojs

View GitHub Profile
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,
},
},
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,
});
let counter = 0;
const count = () => {
counter += 1;
}
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,
const NewsletterFormView = (props) => {
const classes = useStyles();
const {
model: {
isLoading,
isValid,
fields: { newsletter },
},
onFieldChange,
@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: [],
@intojs
intojs / Controller.js
Created November 21, 2020 00:38
React MVC - Controller
class Controller {
#model;
initialize() {
this.#model = new Model();
const view = new View(this.#handleIncrementBtnClick);
this.#model.attach(view);
}