Skip to content

Commit b547e8d

Browse files
committed
fix: conditional allow posts
1 parent bf2872c commit b547e8d

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

src/components/CollaborationResponseModal/index.tsx

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ interface CollaborationResponseModalProps {
1717
collaborationRequestId: string;
1818
}
1919

20-
const MIN_FEEDBACK_LENGTH = 10;
20+
const MIN_FEEDBACK_LENGTH = 3;
21+
const MAX_FEEDBACK_LENGTH = 255;
2122

2223
export const CollaborationResponseModal = ({
2324
isOpen,
@@ -76,18 +77,25 @@ export const CollaborationResponseModal = ({
7677
},
7778
});
7879

79-
const isValidFeedback = feedback.trim().length >= MIN_FEEDBACK_LENGTH;
80-
const charactersRemaining = MIN_FEEDBACK_LENGTH - feedback.trim().length;
80+
const isValidFeedback = (value: string) => {
81+
const trimmed = value.trim();
82+
return trimmed.length >= MIN_FEEDBACK_LENGTH && trimmed.length <= MAX_FEEDBACK_LENGTH;
83+
};
84+
85+
const isFeedbackValid = isValidFeedback(feedback);
86+
const feedbackLength = feedback.trim().length;
87+
const charactersRemaining = MIN_FEEDBACK_LENGTH - feedbackLength;
8188

8289
const handleFeedbackChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
83-
setFeedback(e.target.value);
84-
if (showError && e.target.value.trim().length >= MIN_FEEDBACK_LENGTH) {
90+
const newValue = e.target.value.slice(0, MAX_FEEDBACK_LENGTH);
91+
setFeedback(newValue);
92+
if (showError && isValidFeedback(newValue)) {
8593
setShowError(false);
8694
}
8795
};
8896

8997
const handleAction = (action: "Approved" | "Rejected") => {
90-
if (!isValidFeedback) {
98+
if (!isFeedbackValid) {
9199
setShowError(true);
92100
return;
93101
}
@@ -186,23 +194,22 @@ export const CollaborationResponseModal = ({
186194
placeholder="Escreva seu feedback sobre a solicitação..."
187195
rows={5}
188196
disabled={approveCollaborationMutation.isPending}
197+
maxLength={MAX_FEEDBACK_LENGTH}
189198
className={showError ? styles.textareaError : ""}
190199
/>
191200
<div className={styles.feedbackInfo}>
192201
<span
193202
className={`${styles.charCounter} ${
194-
charactersRemaining > 0 ? styles.charCounterError : ""
203+
!isFeedbackValid ? styles.charCounterError : ""
195204
}`}
196205
>
197-
{charactersRemaining > 0
198-
? `${charactersRemaining} caracteres restantes`
199-
: `${feedback.trim().length} caracteres`}
206+
{feedbackLength} / {MAX_FEEDBACK_LENGTH} caracteres
207+
{charactersRemaining > 0 && ` (mínimo ${MIN_FEEDBACK_LENGTH})`}
200208
</span>
201209
</div>
202210
{showError && (
203211
<span className={styles.errorMessage}>
204-
O feedback deve ter pelo menos {MIN_FEEDBACK_LENGTH}{" "}
205-
caracteres
212+
O feedback deve ter entre {MIN_FEEDBACK_LENGTH} e {MAX_FEEDBACK_LENGTH} caracteres
206213
</span>
207214
)}
208215
</div>
@@ -223,7 +230,7 @@ export const CollaborationResponseModal = ({
223230
onClick={() => handleAction("Rejected")}
224231
disabled={
225232
approveCollaborationMutation.isPending ||
226-
!isValidFeedback ||
233+
!isFeedbackValid ||
227234
pendingAction === "Rejected"
228235
}
229236
className={styles.rejectButton}
@@ -234,7 +241,7 @@ export const CollaborationResponseModal = ({
234241
onClick={() => handleAction("Approved")}
235242
disabled={
236243
approveCollaborationMutation.isPending ||
237-
!isValidFeedback ||
244+
!isFeedbackValid ||
238245
pendingAction === "Approved"
239246
}
240247
className={styles.approveButton}

src/components/CreateIdea/components/CreatePostHeader/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import styles from "../../style.module.css";
22

33
interface IProps {
44
submitForm: () => void;
5+
isFormValid: boolean;
56
}
67

7-
export const Header = ({ submitForm }: IProps) => {
8+
export const Header = ({ submitForm, isFormValid }: IProps) => {
89
return (
910
<header className={styles.header}>
1011
<h2>Nova ideia</h2>
1112

12-
<button onClick={submitForm}>Publicar</button>
13+
<button onClick={submitForm} disabled={!isFormValid}>Publicar</button>
1314
</header>
1415
);
1516
};

src/components/CreateIdea/index.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,24 @@ export const CreateIdeaScreen = () => {
3636

3737
const fileInputRef = useRef<HTMLInputElement | null>(null);
3838

39-
const { register, handleSubmit } = useForm<ICreateIdea>();
39+
const { register, handleSubmit, watch } = useForm<ICreateIdea>();
40+
41+
const title = watch("title");
42+
const content = watch("content");
43+
44+
const isValidTitle = (value?: string) => {
45+
if (!value) return false;
46+
const trimmed = value.trim();
47+
return trimmed.length >= 3 && trimmed.length <= 70 && /^[^{}]+$/.test(trimmed);
48+
};
49+
50+
const isValidContent = (value?: string) => {
51+
if (!value) return false;
52+
const trimmed = value.trim();
53+
return trimmed.length >= 3 && trimmed.length <= 700 && /^[^{}]+$/.test(trimmed);
54+
};
55+
56+
const isFormValid = isValidTitle(title) && isValidContent(content) && images && images.length > 0;
4057

4158
const handleImageChange = (e: React.ChangeEvent<HTMLInputElement>) => {
4259
const files = e.target.files;
@@ -168,7 +185,7 @@ export const CreateIdeaScreen = () => {
168185

169186
return (
170187
<div className={styles.container}>
171-
<Header submitForm={handleSubmit(onSubmit)} />
188+
<Header submitForm={handleSubmit(onSubmit)} isFormValid={isFormValid} />
172189

173190
<div className={styles.avatar}>
174191
<UserIcon

src/components/Idea/components/CollaborationModal/index.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,22 @@ export const CollaborationModal = ({
3535
},
3636
});
3737

38+
const isValidMessage = (value: string) => {
39+
const trimmed = value.trim();
40+
return trimmed.length >= 3 && trimmed.length <= 255 && /^[^{}]+$/.test(trimmed);
41+
};
42+
43+
const isMessageValid = isValidMessage(collaborationMessage);
44+
3845
const handleSendCollaboration = async () => {
39-
if (!collaborationMessage.trim()) {
40-
toast.error("Por favor, escreva uma mensagem");
46+
if (!isMessageValid) {
47+
toast.error("A mensagem deve ter entre 3 e 255 caracteres");
4148
return;
4249
}
4350

4451
createCollaborationRequestMutation({
4552
ideaId: ideaId,
46-
message: collaborationMessage,
53+
message: collaborationMessage.trim(),
4754
});
4855
};
4956

@@ -59,14 +66,15 @@ export const CollaborationModal = ({
5966
onChange={(e) => setCollaborationMessage(e.target.value)}
6067
placeholder="Ex: Tenho experiência em backend e adoraria ajudar..."
6168
rows={5}
69+
maxLength={255}
6270
/>
6371
<div className={styles.modalActions}>
6472
<button onClick={onClose} className={styles.cancelButton}>
6573
Cancelar
6674
</button>
6775
<button
6876
onClick={handleSendCollaboration}
69-
disabled={isLoadingCollaboration}
77+
disabled={isLoadingCollaboration || !isMessageValid}
7078
className={styles.sendButton}
7179
>
7280
{isLoadingCollaboration ? "Enviando..." : "Enviar Solicitação"}

0 commit comments

Comments
 (0)