Skip to content

Commit

Permalink
Tips componentで一覧表示できるように
Browse files Browse the repository at this point in the history
  • Loading branch information
erukiti committed Apr 30, 2019
1 parent 0a81405 commit 41c6f1f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/components/tips.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
import React, { useState, useCallback } from "react";
import { addTips } from '../domains/tips';
import React, { useState, useCallback, useEffect } from "react";
import { addTips, fetchAllTips, Tips, TipsPort } from '../domains/tips';
import { createTipsLocalStorage } from '../adapters/local-storage'

const Tips: React.FC = () => {
const [value, setValue] = useState('aaaa')
const TipsComponent: React.FC = () => {
const [value, setValue] = useState('')
const [allTips, setAllTips] = useState<Tips[]>([])
function handleChange(text: string) {
setValue(text)
}

useEffect(() => {
const tipsPort: TipsPort = {
setAllTips: (tipses) => {
setAllTips(tipses);
}
}
const tipsRepositoryPort = createTipsLocalStorage();
fetchAllTips(tipsRepositoryPort, tipsPort);
}, [])

const handleSubmit = useCallback((event: React.FormEvent) => {
const tipsRepositoryPort = createTipsLocalStorage();
addTips(tipsRepositoryPort, value)
event.preventDefault()
}, [value])

return <form onSubmit={handleSubmit}>
return <>
<form onSubmit={handleSubmit}>
<input type="text" value={value} onChange={e => handleChange(e.target.value)} />
<input type="submit" value="add" />
</form>
<ul>
{
allTips.map(tips => <li>{tips.message}</li>)
}
</ul>
</>
};

export default Tips;
export default TipsComponent;
2 changes: 2 additions & 0 deletions src/domains/tips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface TipsPort {
setAllTips: (allTips: Tips[]) => void;
}

export type Port = Partial<TipsRepositoryPort | TipsPort>

const createId = () => "hoge";

export const addTips = (
Expand Down

0 comments on commit 41c6f1f

Please sign in to comment.