/**
 * This file provided by Facebook is for non-commercial testing and evaluation
 * purposes only.  Facebook reserves all rights not expressly granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */ import { commitMutation , graphql , } from 'relay-hooks' ; import { ConnectionHandler , Disposable , IEnvironment , RecordSourceSelectorProxy , } from 'relay-runtime' ; import { TodoListFooter_user } from '../__generated__/relay/TodoListFooter_user.graphql' ; import { isNotNull } from '../components/TodoApp' ; type Todos = NonNullable < TodoListFooter_user [ 'todos' ] > ; type Edges = NonNullable < Todos [ 'edges' ] > ; type Edge = NonNullable < Edges [ number ] > ; type Node = NonNullable < Edge [ 'node' ] > ; type NullableNode = Edge [ 'node' ] ; const mutation = graphql `
  mutation RemoveCompletedTodosMutation($input: RemoveCompletedTodosInput!) {
    removeCompletedTodos(input: $input) {
      deletedTodoIds
      user {
        completedCount
        totalCount
      }
    }
  }
` ; function sharedUpdater ( store , user , deletedIDs ) { const userProxy = store . get ( user . id ) ; const conn = ConnectionHandler . getConnection ( userProxy , 'TodoList_todos' ) ; if ( conn ) { deletedIDs . forEach ( ( deletedID ) : void => ConnectionHandler . deleteNode ( conn , deletedID ) ) ; } } function commit ( environment , todos , user ) { const input = { userId : user . userId , } ; commitMutation ( environment , { mutation , variables : { input , } , updater : ( store ) => { const payload = store . getRootField ( 'removeCompletedTodos' ) ; const deletedIds = payload . getValue ( 'deletedTodoIds' ) ; sharedUpdater ( store , user , deletedIds ) ; } , optimisticUpdater : ( store ) => { const completedNodeIds = todos . edges ? todos . edges . filter ( isNotNull ) . map ( ( edge ) : NullableNode => edge . node ) . filter ( isNotNull ) . filter ( ( node ) : boolean => node . complete ) . map ( ( node ) : string => node . id ) : [ ] ; const userRecord = store . get ( user . id ) ; userRecord . setValue ( userRecord . getValue ( 'totalCount' ) - completedNodeIds . length , 'totalCount' ) ; userRecord . setValue ( 0 , 'completedCount' ) ; sharedUpdater ( store , user , completedNodeIds ) ; } , } ) ; } export default { commit } ;