Skip to content

Commit

Permalink
Try different inline text editing
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmesami committed Mar 6, 2024
1 parent c69ffad commit 23fafbe
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 79 deletions.
13 changes: 4 additions & 9 deletions locale/cs/LC_MESSAGES/djangojs.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-04 23:22+0000\n"
"POT-Creation-Date: 2024-03-06 13:34+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -28,8 +28,7 @@ msgstr ""
msgid "Homepage"
msgstr "Domů"

#, javascript-format
msgid "Could not create site: %s"
msgid "Could not update site: {err}"
msgstr ""

msgid "Create Site"
Expand All @@ -38,8 +37,7 @@ msgstr ""
msgid "Available websites"
msgstr ""

#, javascript-format
msgid "Could not update site: %s"
msgid "No sites yet"
msgstr ""

msgid "Site name"
Expand All @@ -57,7 +55,7 @@ msgstr ""
msgid "Available regions"
msgstr ""

msgid "Cancel"
msgid "Close"
msgstr ""

msgid "Saving"
Expand All @@ -66,8 +64,5 @@ msgstr ""
msgid "Save changes"
msgstr ""

msgid "Edit title"
msgstr ""

msgid "Preview"
msgstr "Náhled"
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"react-bootstrap": "^2.10.1",
"react-contenteditable": "^3.3.7",
"react-dom": "^18.2.0",
"react-edit-text": "^5.1.1",
"react-redux": "^9.1.0",
"sanitize-html": "^2.12.1",
"sass": "^1.43.4",
Expand Down
7 changes: 5 additions & 2 deletions tumbs/static/js/cms/components/CreateWebsiteButton.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from "react";
import { useDispatch } from "react-redux";
import { _, interpolate } from "../i18n";
import { _ } from "../i18n";
import { actions as websitesActions } from "../slices/websites";
import { actions as alertsActions } from "../slices/alerts";
import { apiRequest } from "../network";
Expand Down Expand Up @@ -36,7 +36,10 @@ const CreateWebsiteButton = () => {
.catch((err) => {
setStatus("error");
dispatch(
alertsActions.addAlert({ content: interpolate(_("Could not create site: %s"), err), severity: "danger" }),
alertsActions.addAlert({
content: _('Could not create site: "{err}"').supplant({ err: String(err) }),
severity: "danger",
}),
);
// TODO: notify Sentry
});
Expand Down
11 changes: 7 additions & 4 deletions tumbs/static/js/cms/components/UpdateWebsiteDialog.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from "react";
import * as R from "ramda";
import { useSelector, useDispatch } from "react-redux";
import { _, interpolate } from "../i18n";
import { _ } from "../i18n";
import { actions as alertsActions } from "../slices/alerts";
import { actions as updateWebsiteActions } from "../slices/updateWebsiteDialog";
import { actions as websitesActions } from "../slices/websites";
Expand Down Expand Up @@ -39,18 +39,21 @@ const UpdateWebsiteDialog = ({ website }) => {
.catch((err) => {
setStatus("error");
dispatch(
alertsActions.addAlert({ content: interpolate(_("Could not update site: %s"), err), severity: "danger" }),
alertsActions.addAlert({
content: _('Could not update site: "{err}"').supplant({ err: String(err) }),
severity: "danger",
}),
);
// TODO: notify Sentry
hide();
});
};

return (
<Offcanvas show={visible} onHide={hide} placement="end">
<Offcanvas show={visible} onHide={hide} placement="end" aria-labelledby="updateWebsiteLabel">
<Form onSubmit={handleSubmit}>
<Offcanvas.Header closeButton>
<Offcanvas.Title>Edit Site</Offcanvas.Title>
<Offcanvas.Title id="updateWebsiteLabel">Edit Site</Offcanvas.Title>
</Offcanvas.Header>

<Offcanvas.Body>
Expand Down
66 changes: 16 additions & 50 deletions tumbs/static/js/cms/components/WebsiteNameEditor.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
import React, { useState } from "react";
import * as R from "ramda";
import { useDispatch } from "react-redux";
import { _, interpolate } from "../i18n";
import { _ } from "../i18n";
import { actions as websitesActions } from "../slices/websites";
import { actions as alertsActions } from "../slices/alerts";
import { apiRequest } from "../network";
import Form from "react-bootstrap/Form";
import InputGroup from "react-bootstrap/InputGroup";
import Button from "react-bootstrap/Button";
import { EditText } from "react-edit-text";

const WebsiteNameEditor = ({ website }) => {
const dispatch = useDispatch();
const [status, setStatus] = useState("initial");
let isLoading = status === "loading";
let isEditing = ["editing", "loading"].includes(status);

const startEditing = () => setStatus("editing");
const stopEditing = () => setStatus("initial");
const handleSubmit = ({ value, previousValue }) => {
if (value === previousValue) return;

const handleSubmit = (e) => {
e.preventDefault();
setStatus("loading");

apiRequest("update_website", {
payload: R.assoc("name", e.target.name.value, website),
payload: R.assoc("name", value, website),
args: { website_id: website.id },
})
.then((data) => {
Expand All @@ -33,52 +28,23 @@ const WebsiteNameEditor = ({ website }) => {
.catch((err) => {
setStatus("error");
dispatch(
alertsActions.addAlert({ content: interpolate(_("Could not update site: %s"), err), severity: "danger" }),
alertsActions.addAlert({
content: _('Could not update site: "{err}"').supplant({ err: String(err) }),
severity: "danger",
}),
);
// TODO: notify Sentry
});
};

return (
<>
{isEditing ? (
<Form onSubmit={handleSubmit}>
<InputGroup>
<Form.Control
name="name"
defaultValue={website.name}
disabled={isLoading}
autoFocus
placeholder={_("Site name")}
aria-label={_("Site name")}
/>
<Button variant="outline-secondary" disabled={isLoading} onClick={stopEditing} title={_("Cancel")}>
<i className="bi-x-circle" />
</Button>
<Button
variant="outline-secondary"
type="submit"
disabled={isLoading}
title={isLoading ? _("Saving") : _("Save changes")}
>
{isLoading ? <i className="spinner-grow spinner-grow-sm" /> : <i className="bi-floppy text-success" />}
</Button>
</InputGroup>
</Form>
) : (
<>
<h3>{website.name}</h3>
<Button
variant="light"
onClick={startEditing}
className="button-icon button-start-editing"
title={_("Edit title")}
>
<i className="bi-pencil" />
</Button>
</>
)}
</>
<EditText
defaultValue={website.name}
name="name"
onSave={handleSubmit}
readonly={isLoading}
className="h3 content-editable"
/>
);
};

Expand Down
1 change: 0 additions & 1 deletion tumbs/static/js/cms/i18n.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const _ = window.gettext;
export const gettext = window.gettext;
export const interpolate = window.interpolate;
9 changes: 1 addition & 8 deletions tumbs/static/js/cms/network.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import * as R from "ramda";
import { ENDPOINTS } from "./store";

const interpolate = (str, o) => {
return str.replace(/{([^{}]*)}/g, function (a, b) {
const r = o[b];
return typeof r === "string" || typeof r === "number" ? r : a;
});
};

export const apiRequest = (endpoint, { args = {}, payload = {}, init = {} }) => {
const { uri, method } = ENDPOINTS[endpoint];

return fetch(
interpolate(uri, args),
uri.supplant(args),
R.mergeRight(
{
method: method,
Expand Down
9 changes: 8 additions & 1 deletion tumbs/static/js/project.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
import '../sass/project.scss';
import "../sass/project.scss";

String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g, (a, b) => {
const r = o[b];
return typeof r === "string" || typeof r === "number" ? r : a;
});
};
4 changes: 0 additions & 4 deletions tumbs/static/sass/project.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
padding: .2rem;
}

.button-start-editing {
margin-left: -1rem;
}

.button-settings {
margin-left: auto;
}
Expand Down

0 comments on commit 23fafbe

Please sign in to comment.