Projekt

Obecné

Profil

Stáhnout (2.63 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { createAsyncThunk } from '@reduxjs/toolkit'
2
import axiosInstance from '../../api/api'
3
import {UsersDetailState} from "./userDetailSlice"
4

    
5

    
6
const apiError = 'Error, server is currently unavailable.'
7

    
8
export const fetchUsers = createAsyncThunk(
9
    'usersDetail/fetchUsers',
10
    async (dispatch, { getState }) => {
11
        try {
12
            console.log("fetch users called")
13
            const {data, status} = await axiosInstance.get(
14
                `/users`
15
            )
16
            if (status === 200) {
17
                console.log(data)
18
                return data
19
            }
20
            console.log("not returning data: " + data)
21
            return Promise.reject(apiError)
22
        } catch (err: any) {
23
            return Promise.reject(apiError)
24
        }
25
    }
26
)
27

    
28
export const savePermissions = createAsyncThunk(
29
    'usersDetail/savePermissions',
30
    async (dispatch, { getState }) => {
31
        try {
32
            const { usersDetail } = getState() as { usersDetail: UsersDetailState }
33
            console.log("savePermissions called")
34

    
35
            const selectedUser = usersDetail.selectedUser
36
            if (selectedUser === undefined) {
37
                return Promise.reject("User is undefined")
38
            }
39

    
40
            const selectedUserPermissions = usersDetail.permissions
41
            if (selectedUserPermissions === undefined) {
42
                return Promise.reject("Permissions is undefined")
43
            }
44

    
45
            const selectedUserIdentifier = selectedUser.email
46

    
47
            const {data, status} = await axiosInstance.patch(
48
                `/users/${selectedUserIdentifier}/permissions`,
49
                selectedUserPermissions
50
            )
51
            if (status === 200) {
52
                console.log("returning data: " + data)
53
                return data
54
            }
55
            console.log("not returning data: " + data)
56
            return Promise.reject(apiError)
57
        } catch (err: any) {
58
            return Promise.reject(apiError)
59
        }
60
    }
61
)
62

    
63
export const deleteUser = createAsyncThunk(
64
    'usersDetail/deleteUser',
65
    async (dispatch, { getState }) => {
66
        const { usersDetail } = getState() as { usersDetail: UsersDetailState }
67
        console.log("savePermissions called")
68

    
69
        const selectedUser = usersDetail.selectedUser
70

    
71
        if (selectedUser === undefined) {
72
            return Promise.reject("User is undefined")
73
        }
74
        const { data, status } = await axiosInstance.delete(
75
            `/users/${selectedUser?.email}`
76
        )
77
        if (status !== 200) {
78
            // TODO dodělat zpracování erroru
79
            return Promise.reject("Deleting user failed")
80
        }
81
        return status
82
    }
83
)
(4-4/4)