@@ -3,6 +3,7 @@ import { Dispatch } from 'redux'
33import { createAction } from 'deox'
44
55import axios from '../services/request'
6+ import { requestNotificationPermission } from '../services/push-notification'
67import { TodoType } from '../models'
78
89export const addTodo = createAction (
@@ -50,29 +51,40 @@ export const toggleTodo = createAction(
5051( resolve :Function ) => ( id :number ) => resolve ( { id} )
5152)
5253
54+ export const registerNotificationId = createAction (
55+ 'REGISTRATION_NOTIFICATION_ID' ,
56+ ( resolve :Function ) => ( id :string ) => resolve ( { id} )
57+ )
58+
59+ export const globalError = createAction ( 'GLOBAL_ERROR' )
60+
5361export const addTodoRequest = ( todo :Partial < TodoType > ) => {
5462todo . completed = false
5563todo . createdAt = new Date ( )
5664
5765return ( dispatch :Dispatch ) =>
5866axios . post ( 'todos' , todo )
5967. then ( ( { data} :AxiosResponse < TodoType > ) => dispatch ( addTodo ( data ) ) )
68+ . catch ( ( ) => dispatch ( globalError ( ) ) )
6069}
6170
6271export const editTodoRequest = ( todo :TodoType ) =>
6372( dispatch :Dispatch ) =>
6473axios . put ( `todos/${ todo . id } ` , todo )
6574. then ( ( { data} :AxiosResponse < TodoType > ) => dispatch ( editTodo ( data ) ) )
75+ . catch ( ( ) => dispatch ( globalError ( ) ) )
6676
6777export const fetchTodosRequest = ( ) =>
6878( dispatch :Dispatch ) =>
6979axios . get ( 'todos' )
7080. then ( ( { data} :AxiosResponse < TodoType [ ] > ) => dispatch ( fetchTodos ( data ) ) )
81+ . catch ( ( ) => dispatch ( globalError ( ) ) )
7182
7283export const removeTodoRequest = ( id :number ) =>
7384( dispatch :Dispatch ) =>
7485axios . delete ( `todos/${ id } ` )
7586. then ( ( ) => dispatch ( removeTodo ( id ) ) )
87+ . catch ( ( ) => dispatch ( globalError ( ) ) )
7688
7789export const toggleTodoRequest = ( todo :TodoType ) => {
7890const data = {
@@ -82,4 +94,14 @@ export const toggleTodoRequest = (todo: TodoType) => {
8294return ( dispatch :Dispatch ) =>
8395axios . patch ( `todos/${ todo . id } ` , data )
8496. then ( ( ) => dispatch ( toggleTodo ( todo . id ) ) )
97+ . catch ( ( ) => dispatch ( globalError ( ) ) )
8598}
99+
100+ export const registerNotificationIdRequest = ( ) =>
101+ ( dispatch :Dispatch ) =>
102+ requestNotificationPermission ( )
103+ . then ( id =>
104+ axios . post ( `notificationIds` , { id} )
105+ . then ( ( ) => dispatch ( registerNotificationId ( id ) ) )
106+ )
107+ . catch ( ( ) => dispatch ( globalError ( ) ) )