-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
161 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,3 +66,7 @@ | |
padding: 11px 20px; | ||
height: 50px; | ||
} | ||
|
||
.button.disabled { | ||
background: #777 !important; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import styles from './error.module.css'; | ||
|
||
function Error({ children }) { | ||
return <div className={styles.error}>{children}</div>; | ||
} | ||
|
||
export default Error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.error { | ||
padding: 20px 10px; | ||
color: #f00; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './error'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './success'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import styles from './success.module.css'; | ||
|
||
function Success({ children }) { | ||
return <div className={styles.success}>{children}</div>; | ||
} | ||
|
||
export default Success; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.success { | ||
padding: 20px 10px; | ||
color: #0f0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
import { FAILURE, REQUEST, SUCCESS } from '../constants'; | ||
|
||
const DEFAULT_METHOD = 'GET'; | ||
|
||
export default (store) => (next) => async (action) => { | ||
if (!action.CallAPI) return next(action); | ||
|
||
const { CallAPI, type, ...rest } = action; | ||
const { CallAPI, type, method, body, ...rest } = action; | ||
|
||
next({ ...rest, type: type + REQUEST }); | ||
|
||
try { | ||
const data = await fetch(CallAPI).then((res) => res.json()); | ||
const response = await fetch(CallAPI, { | ||
method: method || DEFAULT_METHOD, | ||
body: JSON.stringify(body) || null, | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
const data = await response.json(); | ||
if (!response.ok) throw data; | ||
next({ ...rest, type: type + SUCCESS, data }); | ||
} catch (error) { | ||
next({ ...rest, type: type + FAILURE, error }); | ||
throw next({ ...rest, type: type + FAILURE, error }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,54 @@ | ||
import { DECREMENT, INCREMENT, REMOVE } from '../constants'; | ||
import produce from 'immer'; | ||
|
||
import { | ||
DECREMENT, | ||
INCREMENT, | ||
REMOVE, | ||
SEND_ORDER, | ||
REQUEST, | ||
SUCCESS, | ||
FAILURE, | ||
} from '../constants'; | ||
|
||
const initialState = { | ||
entities: {}, | ||
loading: false, | ||
loaded: false, | ||
error: null, | ||
}; | ||
|
||
export default produce((draft = initialState, action) => { | ||
const { type, id, error } = action; | ||
|
||
// { [productId]: amount } | ||
export default function (state = {}, action) { | ||
const { type, id } = action; | ||
switch (type) { | ||
case INCREMENT: | ||
return { ...state, [id]: (state[id] || 0) + 1 }; | ||
draft.entities[id] = (draft.entities[id] || 0) + 1; | ||
break; | ||
|
||
case DECREMENT: | ||
return { ...state, [id]: state[id] > 0 ? (state[id] || 0) - 1 : 0 }; | ||
draft.entities[id] = | ||
draft.entities[id] > 0 ? (draft.entities[id] || 0) - 1 : 0; | ||
break; | ||
|
||
case REMOVE: | ||
return { ...state, [id]: 0 }; | ||
draft.entities[id] = 0; | ||
break; | ||
|
||
case SEND_ORDER + REQUEST: { | ||
draft.loading = true; | ||
break; | ||
} | ||
case SEND_ORDER + SUCCESS: { | ||
draft.loading = false; | ||
draft.entities = {}; | ||
break; | ||
} | ||
case SEND_ORDER + FAILURE: { | ||
draft.loading = false; | ||
draft.error = error; | ||
break; | ||
} | ||
default: | ||
return state; | ||
return draft; | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters