# All files / src/store/slices snippetsSlice.ts

75.56% Statements 133/176
92.85% Branches 13/14
80% Functions 4/5
75.56% Lines 133/176

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
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    1771x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    22x
    22x
    1x
    1x
    1x
    1x
    1x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    2x
    2x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    2x
    2x
    1x
    1x
    2x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    37x
    37x

    37x

    37x

    37x
    22x
    22x
    22x
    37x
    22x
    22x
    22x
    37x

    37x
    2x
    2x
    37x

    37x
    1x
    1x
    37x

    37x

    37x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
     import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit'
    import { Snippet } from '@/lib/types'
    import {
      getAllSnippets,
      createSnippet as createSnippetDB,
      updateSnippet as updateSnippetDB,
      deleteSnippet as deleteSnippetDB,
      getSnippetsByNamespace,
      bulkMoveSnippets as bulkMoveSnippetsDB,
      moveSnippetToNamespace,
    } from '@/lib/db'

    interface SnippetsState {
      items: Snippet[]
      loading: boolean
      error: string | null
      selectedIds: string[]
      selectionMode: boolean
    }

    const initialState: SnippetsState = {
      items: [],
      loading: false,
      error: null,
      selectedIds: [],
      selectionMode: false,
    }

    export const fetchAllSnippets = createAsyncThunk(
      'snippets/fetchAll',
      async () => {
        return await getAllSnippets()
      }
    )

    export const fetchSnippetsByNamespace = createAsyncThunk(
      'snippets/fetchByNamespace',
      async (namespaceId: string) => {
        return await getSnippetsByNamespace(namespaceId)
      }
    )

    export const createSnippet = createAsyncThunk(
      'snippets/create',
      async (snippetData: Omit<Snippet, 'id' | 'createdAt' | 'updatedAt'>) => {
        const newSnippet: Snippet = {
          ...snippetData,
          id: Date.now().toString(),
          createdAt: Date.now(),
          updatedAt: Date.now(),
        }
        await createSnippetDB(newSnippet)
        return newSnippet
      }
    )

    export const updateSnippet = createAsyncThunk(
      'snippets/update',
      async (snippet: Snippet) => {
        const updatedSnippet = {
          ...snippet,
          updatedAt: Date.now(),
        }
        await updateSnippetDB(updatedSnippet)
        return updatedSnippet
      }
    )

    export const deleteSnippet = createAsyncThunk(
      'snippets/delete',
      async (id: string) => {
        await deleteSnippetDB(id)
        return id
      }
    )

    export const moveSnippet = createAsyncThunk(
      'snippets/move',
      async ({ snippetId, targetNamespaceId }: { snippetId: string, targetNamespaceId: string }) => {
        await moveSnippetToNamespace(snippetId, targetNamespaceId)
        return { snippetId, targetNamespaceId }
      }
    )

    export const bulkMoveSnippets = createAsyncThunk(
      'snippets/bulkMove',
      async ({ snippetIds, targetNamespaceId }: { snippetIds: string[], targetNamespaceId: string }) => {
        await bulkMoveSnippetsDB(snippetIds, targetNamespaceId)
        return { snippetIds, targetNamespaceId }
      }
    )

    const snippetsSlice = createSlice({
      name: 'snippets',
      initialState,
      reducers: {
        toggleSelectionMode: (state) => {
          state.selectionMode = !state.selectionMode
          if (!state.selectionMode) {
            state.selectedIds = []
          }
        },
        toggleSnippetSelection: (state, action: PayloadAction<string>) => {
          const index = state.selectedIds.indexOf(action.payload)
          if (index !== -1) {
            state.selectedIds.splice(index, 1)
          } else {
            state.selectedIds.push(action.payload)
          }
        },
        clearSelection: (state) => {
          state.selectedIds = []
        },
        selectAllSnippets: (state) => {
          state.selectedIds = state.items.map(s => s.id)
        },
      },
      extraReducers: (builder) => {
        builder
          .addCase(fetchAllSnippets.pending, (state) => {
            state.loading = true
            state.error = null
          })
          .addCase(fetchAllSnippets.fulfilled, (state, action) => {
            state.loading = false
            state.items = action.payload
          })
          .addCase(fetchAllSnippets.rejected, (state, action) => {
            state.loading = false
            state.error = action.error.message || 'Failed to fetch snippets'
          })
          .addCase(fetchSnippetsByNamespace.pending, (state) => {
            state.loading = true
            state.error = null
          })
          .addCase(fetchSnippetsByNamespace.fulfilled, (state, action) => {
            state.loading = false
            state.items = action.payload
          })
          .addCase(fetchSnippetsByNamespace.rejected, (state, action) => {
            state.loading = false
            state.error = action.error.message || 'Failed to fetch snippets'
          })
          .addCase(createSnippet.fulfilled, (state, action) => {
            state.items.unshift(action.payload)
          })
          .addCase(updateSnippet.fulfilled, (state, action) => {
            const index = state.items.findIndex(s => s.id === action.payload.id)
            if (index !== -1) {
              state.items[index] = action.payload
            }
          })
          .addCase(deleteSnippet.fulfilled, (state, action) => {
            state.items = state.items.filter(s => s.id !== action.payload)
          })
          .addCase(moveSnippet.fulfilled, (state, action) => {
            const { snippetId } = action.payload
            state.items = state.items.filter(s => s.id !== snippetId)
          })
          .addCase(bulkMoveSnippets.fulfilled, (state, action) => {
            const { snippetIds } = action.payload
            state.items = state.items.filter(s => !snippetIds.includes(s.id))
            state.selectedIds = []
            state.selectionMode = false
          })
      },
    })

    export const {
      toggleSelectionMode,
      toggleSnippetSelection,
      clearSelection,
      selectAllSnippets,
    } = snippetsSlice.actions

    export default snippetsSlice.reducer