|
1 | | -import { Box, Button, DialogActions, TextField } from "@mui/material"; |
| 1 | +import { styled } from '@mui/material/styles'; |
| 2 | +import { Box, Button, DialogActions } from "@mui/material"; |
2 | 3 | import RequiredTextField from "../../../src/components/RequiredTextField.jsx"; |
3 | | -import { useState } from "react"; |
| 4 | +import CloudUploadIcon from '@mui/icons-material/CloudUpload'; |
| 5 | +import { useState, useEffect, use } from "react"; |
| 6 | +import { getCookie } from '../../../src/utils.js'; |
4 | 7 |
|
5 | 8 | function OrganizationForm({ successCallback, failureCallback, cancelCallback, createMode }) { |
6 | 9 | const [name, setName] = useState(""); |
7 | 10 | const [description, setDescription] = useState(""); |
8 | 11 | const [nameHelperText, setNameHelperText] = useState(""); |
9 | 12 | const [descriptionHelperText, setDescriptionHelperText] = useState(""); |
| 13 | + const [logoFile, setLogoFile] = useState(null); |
| 14 | + const [logoUrl, setLogoUrl] = useState(null); |
| 15 | + const [logoServerPath, setLogoServerPath] = useState(null); |
10 | 16 |
|
11 | 17 | const apiBaseUrl = localStorage.getItem('apiBaseUrl'); |
12 | 18 |
|
| 19 | + const removeLogo = () => { |
| 20 | + setLogoUrl(null); |
| 21 | + setLogoServerPath(null); |
| 22 | + setLogoFile(null); |
| 23 | + } |
| 24 | + |
13 | 25 | const handleCreate = () => (event) => { |
14 | 26 | event.preventDefault(); |
15 | | - // Implement organization creation logic here |
16 | | - // On success, call successCallback with organization data |
17 | | - // On failure, call failureCallback with error |
18 | | - successCallback({ id: 1, name: "New Organization" }); |
| 27 | + fetch(`${apiBaseUrl}/organizations/`, { |
| 28 | + method: 'POST', |
| 29 | + headers: { |
| 30 | + 'Content-Type': 'application/json', |
| 31 | + 'X-CSRFToken': getCookie('csrftoken'), |
| 32 | + }, |
| 33 | + body: JSON.stringify({ |
| 34 | + name: name, |
| 35 | + description: description, |
| 36 | + logo: logoServerPath, |
| 37 | + }), |
| 38 | + }) |
| 39 | + .then(response => { |
| 40 | + if (!response.ok) { |
| 41 | + return response.json().then(data => { |
| 42 | + throw data; |
| 43 | + }); |
| 44 | + } |
| 45 | + return response.json(); |
| 46 | + }) |
| 47 | + .then(data => { |
| 48 | + successCallback(data); |
| 49 | + }) |
| 50 | + .catch(error => { |
| 51 | + failureCallback(error); |
| 52 | + }); |
19 | 53 | } |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + if (logoFile) { |
| 57 | + fetch(`${apiBaseUrl}/organizations/1/file_upload/`, { |
| 58 | + method: 'POST', |
| 59 | + headers: { |
| 60 | + 'X-CSRFToken': getCookie('csrftoken'), |
| 61 | + }, |
| 62 | + body: (() => { |
| 63 | + const formData = new FormData(); |
| 64 | + formData.append('file', logoFile); |
| 65 | + return formData; |
| 66 | + })(), |
| 67 | + }) |
| 68 | + .then(response => { |
| 69 | + if (!response.ok) { |
| 70 | + throw new Error('File upload failed'); |
| 71 | + } |
| 72 | + return response.json(); |
| 73 | + }) |
| 74 | + .then(data => { |
| 75 | + console.log('File uploaded successfully:', data); |
| 76 | + setLogoUrl(data.file_url); |
| 77 | + setLogoServerPath(data.file_path); |
| 78 | + console.log('File path set to:', logoServerPath); |
| 79 | + }) |
| 80 | + .catch(error => { |
| 81 | + console.error('Error uploading file:', error); |
| 82 | + }); |
| 83 | + } |
| 84 | + }, [logoFile]); |
| 85 | + |
| 86 | + const VisuallyHiddenInput = styled('input')({ |
| 87 | + clip: 'rect(0 0 0 0)', |
| 88 | + clipPath: 'inset(50%)', |
| 89 | + height: 1, |
| 90 | + overflow: 'hidden', |
| 91 | + position: 'absolute', |
| 92 | + bottom: 0, |
| 93 | + left: 0, |
| 94 | + whiteSpace: 'nowrap', |
| 95 | + width: 1, |
| 96 | + }); |
| 97 | + |
20 | 98 | return ( |
21 | 99 | <Box p={2}> |
22 | 100 | <RequiredTextField label="Name" helperText={nameHelperText} fullWidth margin="normal" value={name} onChange={(e) => setName(e.target.value)} /> |
23 | 101 | <RequiredTextField label="Description" helperText={descriptionHelperText} fullWidth margin="normal" multiline rows={4} value={description} onChange={(e) => setDescription(e.target.value)} /> |
24 | | - Logo: File upload component |
| 102 | + { !logoUrl ? <Button |
| 103 | + component="label" |
| 104 | + role={undefined} |
| 105 | + variant="contained" |
| 106 | + tabIndex={-1} |
| 107 | + startIcon={<CloudUploadIcon />} |
| 108 | + > |
| 109 | + Logo |
| 110 | + <VisuallyHiddenInput |
| 111 | + type="file" |
| 112 | + onChange={(event) => setLogoFile(event.target.files[0])} |
| 113 | + /> |
| 114 | + </Button> |
| 115 | + : (<><img src={logoUrl} alt="Organization Logo" style={{ marginTop: '10px', maxHeight: '100px' }} /><br /> |
| 116 | + <Button variant="text" color="secondary" onClick={removeLogo}>Remove Logo</Button></> |
| 117 | + )} |
25 | 118 | <DialogActions> |
26 | 119 | <Button onClick={cancelCallback}>Cancel</Button> |
27 | 120 | <Button type="submit" color="primary" onClick={handleCreate()}> |
|
0 commit comments