All files / pages AdminBannedWordsPage.jsx

100% Statements 15/15
100% Branches 0/0
100% Functions 8/8
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          1x     39x   39x           39x 3x         1x       39x 2x                 39x 3x 3x     39x                   2x                                 24x         2x                          
import { useState } from "react";
import { Button, Form, Table } from "react-bootstrap";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
 
const endpoint = "/api/admin/bannedwords/all";
 
export default function AdminBannedWordsPage() {
  const [word, setWord] = useState("");
 
  const { data: bannedWords } = useBackend(
    [endpoint],
    { method: "GET", url: endpoint },
    [],
  );
 
  const addMutation = useBackendMutation(
    (value) => ({
      method: "POST",
      url: "/api/admin/bannedwords/post",
      params: { word: value },
    }),
    { onSuccess: () => setWord("") },
    [endpoint],
  );
 
  const deleteMutation = useBackendMutation(
    (id) => ({
      method: "DELETE",
      url: "/api/admin/bannedwords",
      params: { id },
    }),
    {},
    [endpoint],
  );
 
  const submitWord = (event) => {
    event.preventDefault();
    addMutation.mutate(word);
  };
 
  return (
    <BasicLayout>
      <h2>Banned Words</h2>
 
      <Form onSubmit={submitWord}>
        <Form.Group className="mb-3">
          <Form.Label htmlFor="banned-word">Word</Form.Label>
          <Form.Control
            id="banned-word"
            value={word}
            onChange={(event) => setWord(event.target.value)}
          />
        </Form.Group>
        <Button type="submit" className="mb-3">
          Add
        </Button>
      </Form>
 
      <Table striped bordered>
        <thead>
          <tr>
            <th>Word</th>
            <th>Actions</th>
          </tr>
        </thead>
        <tbody>
          {bannedWords.map((entry) => (
            <tr key={entry.id}>
              <td>{entry.word}</td>
              <td>
                <Button
                  variant="danger"
                  onClick={() => deleteMutation.mutate(entry.id)}
                  aria-label={`Delete ${entry.word}`}
                >
                  Delete
                </Button>
              </td>
            </tr>
          ))}
        </tbody>
      </Table>
    </BasicLayout>
  );
}