File size: 8,291 Bytes
0d37b12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
import { Container, Row, Col, Card, Form, Image, Alert, Button } from "react-bootstrap";
import AdminTemplate from "../../templates/AdminTemplate";
import { useSearchParams } from 'react-router-dom';
import axios from "axios";
import DataStorage from "../../organisms/DataStorage";
import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";

export default function AdminBranchEditPage() {

    const [error, setError] = useState("");
    const [searchParams] = useSearchParams();
    const storeId = searchParams.get('id') || null;

    const [storeDetail, setStoreItem] = useState({
        name:'',
        location:'',
        image_url:''
    });
    const [initialUrl, setInitialUrl] = useState("");
    const [initialTitle, setInitialTitle] = useState("");
    const [initialDesc, setInitialDesc] = useState("");
    // const [loading, setLoading] = useState(true);
    const [isChanged, setChanged] = useState(false);

    const navigate = useNavigate();

    useEffect(() => {
        if (!DataStorage.get('isLoggedInAdmin')) {
            navigate('/admin-login');
        }
    }, [navigate]);

    const checkChange = () => {
        if (storeDetail.name.trim() !== initialTitle.trim()
            || storeDetail.location.trim() !== initialDesc.trim()
            || storeDetail.image_url.trim() !== initialUrl.trim()) {
            setChanged(true);
        } else {
            setChanged(false);
        }
    }

    const handleChange = (e) => {
        const { name, value } = e.target;
        setStoreItem({ ...storeDetail, [name]: value });
        checkChange();
    };

    const handleAvatarUrlChange = (e) => {
        const url = e.target.value;
        setStoreItem((prevData) => ({
            ...prevData,
            image_url: url // Cập nhật URL ảnh
        }));
        setChanged(true);
    };

    useEffect(() => {
        if (storeId) {
            axios.get(process.env.REACT_APP_API_URL + `/branchs/${storeId}`)
                .then((response) => {
                    setStoreItem(response.data);
                    // setLoading(false);
                    setInitialTitle(response.data.name);
                    setInitialDesc(response.data.location);
                    setInitialUrl(response.data.image_url);
                })
                .catch((error) => {
                    setError(JSON.stringify(error));
                })
        }
    }, [storeId]);

    const handleSubmit = () => {
        const submit_data = {
            'name': storeDetail.name,
            'location': storeDetail.location,
            'image_url': storeDetail.image_url
        }
        if (storeId) {
            axios.patch(process.env.REACT_APP_API_URL + `/branchs/${storeId}`, submit_data)
                .then((response) => {
                    // setFeedItem(response.data);
                    // // setLoading(false);
                    // setInitialDesc(response.data.description);
                    // setInitialTitle(response.data.title);
                    // setChanged(false);
                    navigate('/admin-branchs-list');
                    // window.location.reload();
                })
                .catch((error) => {
                    setError(JSON.stringify(error));
                })
        } else {
            axios.post(process.env.REACT_APP_API_URL + `/branchs`, submit_data)
            .then((response) => {
                navigate('/admin-branchs-list');
            })
            .catch((error) => {
                setError(JSON.stringify(error));
            })
        }
    }

    return (
        <AdminTemplate content={
            (
                <Container fluid className="d-flex align-items-center justify-content-center mt-5">
                    <Row className="align-items-center">
                        {/* <Col xs={1} md={2}></Col> */}
                        <Col>
                            <Card style={{ width: '100%' }} className='justify-content-center'>
                                <Card.Header>
                                    <Card.Title className='mt-1 text-center'>{storeId ? 'Chỉnh sửa thông tin chi nhánh' : 'Tạo chi nhánh'}</Card.Title>
                                </Card.Header>
                                <Card.Body>

                                    <Form>
                                        {error && <Alert variant="danger">{error}</Alert>}
                                        <Row className="mb-3">
                                            <Col xs={12} className="text-center mb-3">
                                                {/* Hiển thị ảnh từ URL */}
                                                <Image
                                                    src={storeDetail.image_url} // Hiển thị ảnh đại diện từ URL
                                                    alt="Ảnh mô tả"
                                                    width="auto"
                                                    height={400}
                                                    className="mb-3"
                                                />
                                                <Form.Group controlId="formImageUrl">
                                                    <Form.Label>URL ảnh mô tả</Form.Label>
                                                    <Form.Control
                                                        type="text"
                                                        placeholder="Nhập URL ảnh"
                                                        value={storeDetail.image_url || ''}
                                                        onChange={handleAvatarUrlChange} // Gọi hàm khi URL thay đổi
                                                    />
                                                </Form.Group>
                                            </Col>
                                            <Col xs={12} className="mb-3">
                                                <Form.Group controlId="formTitle">
                                                    <Form.Label>Tên chi nhánh</Form.Label>
                                                    <Form.Control
                                                        type="text"
                                                        name="name"
                                                        placeholder={storeId ? 'Đang tải dữ liệu...' : 'Tên chi nhánh'}
                                                        value={storeDetail.name || ''}
                                                        onChange={handleChange}
                                                    />
                                                </Form.Group>
                                            </Col>
                                        </Row>

                                        <Form.Group controlId="formText" className="mb-3">
                                            <Form.Label>Địa chỉ</Form.Label>
                                            <Form.Control
                                                as="textarea"
                                                name="location"
                                                placeholder={storeId ? 'Đang tải dữ liệu...' : 'Địa chỉ'}
                                                rows={5}
                                                value={storeDetail.location || ''}
                                                onChange={handleChange}
                                            />
                                        </Form.Group>

                                        <Button variant="primary" type="button" disabled={!isChanged} onClick={handleSubmit}>
                                            {storeId ? 'Cập nhật' : 'Tạo mới'}
                                        </Button>
                                    </Form>
                                </Card.Body>
                            </Card>

                        </Col>
                        {/* <Col xs={1} md={2}></Col> */}
                    </Row>
                </Container>
            )
        } />
    )
}