All files / src/store documentSlice.ts

0% Statements 0/87
0% Branches 0/1
0% Functions 0/1
0% Lines 0/87

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109                                                                                                                                                                                                                         
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'
import type { Document, ExtractionResult, AnalysisResult, ContextResult, ConseilResult } from '../types'
import { documentApi } from '../services/api'
 
interface DocumentState {
  documents: Document[]
  currentDocument: Document | null
  extractionResult: ExtractionResult | null
  analysisResult: AnalysisResult | null
  contextResult: ContextResult | null
  conseilResult: ConseilResult | null
  loading: boolean
  error: string | null
}
 
const initialState: DocumentState = {
  documents: [],
  currentDocument: null,
  extractionResult: null,
  analysisResult: null,
  contextResult: null,
  conseilResult: null,
  loading: false,
  error: null,
}
 
export const uploadDocument = createAsyncThunk(
  'document/upload',
  async (file: File) => {
    return await documentApi.upload(file)
  }
)
 
export const extractDocument = createAsyncThunk(
  'document/extract',
  async (documentId: string) => {
    return await documentApi.extract(documentId)
  }
)
 
export const analyzeDocument = createAsyncThunk(
  'document/analyze',
  async (documentId: string) => {
    return await documentApi.analyze(documentId)
  }
)
 
export const getContextData = createAsyncThunk(
  'document/context',
  async (documentId: string) => {
    return await documentApi.getContext(documentId)
  }
)
 
export const getConseil = createAsyncThunk(
  'document/conseil',
  async (documentId: string) => {
    return await documentApi.getConseil(documentId)
  }
)
 
const documentSlice = createSlice({
  name: 'document',
  initialState,
  reducers: {
    setCurrentDocument: (state, action: PayloadAction<Document | null>) => {
      state.currentDocument = action.payload
    },
    clearResults: (state) => {
      state.extractionResult = null
      state.analysisResult = null
      state.contextResult = null
      state.conseilResult = null
    },
  },
  extraReducers: (builder) => {
    builder
      .addCase(uploadDocument.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(uploadDocument.fulfilled, (state, action) => {
        state.loading = false
        state.documents.push(action.payload)
        state.currentDocument = action.payload
      })
      .addCase(uploadDocument.rejected, (state, action) => {
        state.loading = false
        state.error = action.error.message || 'Erreur lors du téléversement'
      })
      .addCase(extractDocument.fulfilled, (state, action) => {
        state.extractionResult = action.payload
      })
      .addCase(analyzeDocument.fulfilled, (state, action) => {
        state.analysisResult = action.payload
      })
      .addCase(getContextData.fulfilled, (state, action) => {
        state.contextResult = action.payload
      })
      .addCase(getConseil.fulfilled, (state, action) => {
        state.conseilResult = action.payload
      })
  },
})
 
export const { setCurrentDocument, clearResults } = documentSlice.actions
export const documentReducer = documentSlice.reducer