summaryrefslogtreecommitdiff
path: root/src/lib/api/ChangeDescription.tsx
blob: e0b760a3e20fc90e7a2444c6defb09cb7fa9c093 (plain)
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
const API_URL = process.env.NEXT_PUBLIC_API_URL;

export const changeDescription = async (description: string) => {
    try {
        const res = await fetch(`${API_URL}/api/users/description`, {
            method: 'PATCH',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ description }),
            credentials: 'include',
        });

        if (!res.ok) {
            let errorData: any = null;

            try {
                errorData = await res.json();
            } catch {}

            const detail = errorData?.detail;

            return {
                data: null,
                error: {
                    general: Array.isArray(detail)
                        ? detail[0]?.msg
                        : detail?.msg || 'Ошибка изменения описания',
                },
            };
        }

        const data = await res.json();

        return {
            data,
            error: null,
        };
    } catch (err: any) {
        return {
            data: null,
            error: { general: err.message },
        };
    }
};