Skip to content
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

How to apply DRY principles in example code #265

Closed
ray-delossantos opened this issue Dec 8, 2019 · 3 comments
Closed

How to apply DRY principles in example code #265

ray-delossantos opened this issue Dec 8, 2019 · 3 comments

Comments

@ray-delossantos
Copy link

In the examples for the main website there's the TODO App. How can one use DRY principles for removing the duplicated code?

//Duplicated Code:

return {
		data: dataNew,
		view: (<Todos data={dataNew}/>)
	};
const updateTodo = ({ data, view }) => {
	const dataNew = {
		...data,
		todo: view.target.value
	};

	return {
		data: dataNew,
		view: (<Todos data={dataNew}/>)
	};
};

const createTodo = ({ data }) => {
	const dataNew = {
		todo: "",
		todos: [...data.todos, data.todo]
	};

	return {
		data: dataNew,
		view: (<Todos data={dataNew}/>)
	};
};

const removeTodo = index => ({ data }) => {
	const dataNew = {
		...data,
		todos: data.todos.filter(
			(todo, todoIndex) =>
				todoIndex !== index
		)
	};

	return {
		data: dataNew,
		view: (<Todos data={dataNew}/>)
	};
};

const Todos = ({ data }) => (
	<div>
		<h1>Todos</h1>
		<input
			type="text"
			placeholder="What needs to be done?"
			value={data.todo}
			@input={updateTodo}
		/>
		<button @click={createTodo}>Create</button>
		<for={todo, index}
			of={data.todos}
			name="ul"
		>
			<li @click={removeTodo(index)}>
				{todo}
			</li>
		</for>
	</div>
);

Moon.use({
	data: Moon.data.driver({
		todo: "",
		todos: [
			"Learn Moon",
			"Take a nap",
			"Go shopping"
		]
	}),
	view: Moon.view.driver("#root")
});

Moon.run(({ data }) => ({
	view: (<Todos data={data}/>)
}));

https://kbrsh.github.io/moon/play

@kbrsh
Copy link
Owner

kbrsh commented Dec 8, 2019

You can make a function that returns both new data and a new view, like this:

const updateData = dataNew => ({
	data: dataNew,
	view: (<Todos data={dataNew}/>)
});

const updateTodo = ({ data, view }) => updateData({
  	...data,
	todo: view.target.value
});

const createTodo = ({ data, view }) => updateData({
	todo: "",
	todos: [...data.todos, data.todo]
});

const removeTodo = index => ({ data, view }) => updateData({
	...data,
	todos: data.todos.filter(
		(todo, todoIndex) =>
			todoIndex !== index
		)
});

Example

@ray-delossantos
Copy link
Author

Nice, thanks for the info. I thought you had some shortcuts utility functions for this kind of repetitive code.

@kbrsh
Copy link
Owner

kbrsh commented Dec 9, 2019

No problem. Utility functions for things like this usually end up being too specific. For example, this wouldn't really work if you were using more drivers (such as time or the new http driver). I would just encourage using the standard functional programming technique to stay DRY — creating functions!

@kbrsh kbrsh closed this as completed Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants