forked from mirrors/pronouns.cc
feat(frontend): add new field, new field entry, save buttons to edit profile page
This commit is contained in:
parent
459e525415
commit
e5b4f78998
4 changed files with 120 additions and 19 deletions
|
@ -10,6 +10,7 @@ export interface Props {
|
||||||
onClick?: MouseEventHandler<HTMLButtonElement>;
|
onClick?: MouseEventHandler<HTMLButtonElement>;
|
||||||
style?: ButtonStyle;
|
style?: ButtonStyle;
|
||||||
bold?: boolean;
|
bold?: boolean;
|
||||||
|
noRound?: boolean;
|
||||||
children?: ReactNode;
|
children?: ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +34,9 @@ function PrimaryButton(props: Props) {
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={props.onClick}
|
onClick={props.onClick}
|
||||||
className="bg-blue-500 dark:bg-blue-500 hover:bg-blue-700 hover:dark:bg-blue-800 p-2 rounded-md text-white"
|
className={`bg-blue-500 dark:bg-blue-500 hover:bg-blue-700 hover:dark:bg-blue-800 p-2 ${
|
||||||
|
!props.noRound && "rounded-md"
|
||||||
|
} text-white`}
|
||||||
>
|
>
|
||||||
<span className={props.bold ? "font-bold" : ""}>{props.children}</span>
|
<span className={props.bold ? "font-bold" : ""}>{props.children}</span>
|
||||||
</button>
|
</button>
|
||||||
|
@ -45,7 +48,9 @@ function SuccessButton(props: Props) {
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={props.onClick}
|
onClick={props.onClick}
|
||||||
className="bg-green-600 dark:bg-green-700 hover:bg-green-700 hover:dark:bg-green-800 p-2 rounded-md text-white"
|
className={`bg-green-600 dark:bg-green-700 hover:bg-green-700 hover:dark:bg-green-800 p-2 ${
|
||||||
|
!props.noRound && "rounded-md"
|
||||||
|
} text-white`}
|
||||||
>
|
>
|
||||||
<span className={props.bold ? "font-bold" : ""}>{props.children}</span>
|
<span className={props.bold ? "font-bold" : ""}>{props.children}</span>
|
||||||
</button>
|
</button>
|
||||||
|
@ -57,7 +62,9 @@ function DangerButton(props: Props) {
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={props.onClick}
|
onClick={props.onClick}
|
||||||
className="bg-red-600 dark:bg-red-700 hover:bg-red-700 hover:dark:bg-red-800 p-2 rounded-md text-white"
|
className={`bg-red-600 dark:bg-red-700 hover:bg-red-700 hover:dark:bg-red-800 p-2 ${
|
||||||
|
!props.noRound && "rounded-md"
|
||||||
|
} text-white`}
|
||||||
>
|
>
|
||||||
<span className={props.bold ? "font-bold" : ""}>{props.children}</span>
|
<span className={props.bold ? "font-bold" : ""}>{props.children}</span>
|
||||||
</button>
|
</button>
|
||||||
|
|
|
@ -4,12 +4,14 @@ import {
|
||||||
HandThumbsUp,
|
HandThumbsUp,
|
||||||
Heart,
|
Heart,
|
||||||
People,
|
People,
|
||||||
|
Plus,
|
||||||
Trash3,
|
Trash3,
|
||||||
} from "react-bootstrap-icons";
|
} from "react-bootstrap-icons";
|
||||||
|
|
||||||
import Card from "./Card";
|
import Card from "./Card";
|
||||||
import TextInput from "./TextInput";
|
import TextInput from "./TextInput";
|
||||||
import Button, { ButtonStyle } from "./Button";
|
import Button, { ButtonStyle } from "./Button";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
export interface EditField {
|
export interface EditField {
|
||||||
id: number;
|
id: number;
|
||||||
|
@ -28,6 +30,8 @@ export enum PronounChoice {
|
||||||
type EditableCardProps = {
|
type EditableCardProps = {
|
||||||
field: EditField;
|
field: EditField;
|
||||||
onChangeName: React.ChangeEventHandler<HTMLInputElement>;
|
onChangeName: React.ChangeEventHandler<HTMLInputElement>;
|
||||||
|
onChangePronoun: React.ChangeEventHandler<HTMLInputElement>;
|
||||||
|
onAddPronoun(pronoun: string): void;
|
||||||
onChangeFavourite(
|
onChangeFavourite(
|
||||||
e: React.MouseEvent<HTMLButtonElement>,
|
e: React.MouseEvent<HTMLButtonElement>,
|
||||||
entry: string
|
entry: string
|
||||||
|
@ -40,6 +44,8 @@ type EditableCardProps = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export function EditableCard(props: EditableCardProps) {
|
export function EditableCard(props: EditableCardProps) {
|
||||||
|
const [input, setInput] = useState("");
|
||||||
|
|
||||||
const footer = (
|
const footer = (
|
||||||
<div className="flex justify-between">
|
<div className="flex justify-between">
|
||||||
<TextInput value={props.field.name} onChange={props.onChangeName} />
|
<TextInput value={props.field.name} onChange={props.onChangeName} />
|
||||||
|
@ -55,8 +61,12 @@ export function EditableCard(props: EditableCardProps) {
|
||||||
{Object.keys(props.field.pronouns).map((pronoun, index) => {
|
{Object.keys(props.field.pronouns).map((pronoun, index) => {
|
||||||
const choice = props.field.pronouns[pronoun];
|
const choice = props.field.pronouns[pronoun];
|
||||||
return (
|
return (
|
||||||
<li className="flex justify-between my-1" key={index}>
|
<li className="flex justify-between my-1 items-center" key={index}>
|
||||||
<div>{pronoun}</div>
|
<TextInput
|
||||||
|
value={pronoun}
|
||||||
|
prevValue={pronoun}
|
||||||
|
onChange={props.onChangePronoun}
|
||||||
|
/>
|
||||||
<div className="rounded-md">
|
<div className="rounded-md">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
|
@ -123,6 +133,18 @@ export function EditableCard(props: EditableCardProps) {
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
<li className="flex justify-between my-1 items-center">
|
||||||
|
<TextInput value={input} onChange={(e) => setInput(e.target.value)} />
|
||||||
|
<Button
|
||||||
|
style={ButtonStyle.success}
|
||||||
|
onClick={() => {
|
||||||
|
props.onAddPronoun(input);
|
||||||
|
setInput("");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Plus aria-hidden className="inline" /> Add
|
||||||
|
</Button>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { ChangeEventHandler } from "react";
|
||||||
|
|
||||||
export type Props = {
|
export type Props = {
|
||||||
contrastBackground?: boolean;
|
contrastBackground?: boolean;
|
||||||
|
prevValue?: string;
|
||||||
defaultValue?: string;
|
defaultValue?: string;
|
||||||
value?: string;
|
value?: string;
|
||||||
onChange?: ChangeEventHandler<HTMLInputElement>;
|
onChange?: ChangeEventHandler<HTMLInputElement>;
|
||||||
|
@ -15,6 +16,7 @@ export default function TextInput(props: Props) {
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
|
data-prev-value={props.prevValue ?? props.value}
|
||||||
className={`p-1 lg:p-2 rounded-md ${bg} border-slate-300 text-black dark:border-slate-900 dark:text-white`}
|
className={`p-1 lg:p-2 rounded-md ${bg} border-slate-300 text-black dark:border-slate-900 dark:text-white`}
|
||||||
defaultValue={props.defaultValue}
|
defaultValue={props.defaultValue}
|
||||||
value={props.value}
|
value={props.value}
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useRecoilState } from "recoil";
|
import { useRecoilState, useRecoilValue } from "recoil";
|
||||||
import Loading from "../../components/Loading";
|
import Loading from "../../components/Loading";
|
||||||
import fetchAPI from "../../lib/fetch";
|
import fetchAPI from "../../lib/fetch";
|
||||||
import { userState } from "../../lib/state";
|
import { userState } from "../../lib/state";
|
||||||
import { MeUser, Field } from "../../lib/types";
|
import { MeUser, Field } from "../../lib/types";
|
||||||
import cloneDeep from "lodash/cloneDeep";
|
import cloneDeep from "lodash/cloneDeep";
|
||||||
import { ReactSortable } from "react-sortablejs";
|
import { ReactSortable } from "react-sortablejs";
|
||||||
import Card from "../../components/Card";
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
EditableCard,
|
EditableCard,
|
||||||
|
@ -15,8 +14,11 @@ import {
|
||||||
PronounChoice,
|
PronounChoice,
|
||||||
} from "../../components/Editable";
|
} from "../../components/Editable";
|
||||||
|
|
||||||
|
import Button, { ButtonStyle } from "../../components/Button";
|
||||||
|
import { Plus, Save, Trash } from "react-bootstrap-icons";
|
||||||
|
|
||||||
export default function Index() {
|
export default function Index() {
|
||||||
const [user, setUser] = useRecoilState(userState);
|
const user = useRecoilValue(userState);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [state, setState] = useState(cloneDeep(user));
|
const [state, setState] = useState(cloneDeep(user));
|
||||||
|
|
||||||
|
@ -49,6 +51,16 @@ export default function Index() {
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
const [fields, setFields] = useState(cloneDeep(originalOrder));
|
const [fields, setFields] = useState(cloneDeep(originalOrder));
|
||||||
|
const resetFields = () => {
|
||||||
|
setFields(cloneDeep(originalOrder));
|
||||||
|
};
|
||||||
|
const addField = () => {
|
||||||
|
if (fields.length >= 25) return;
|
||||||
|
|
||||||
|
const lastId = fields[fields.length - 1]?.id ?? 0;
|
||||||
|
|
||||||
|
setFields([...fields, { id: lastId + 1, name: "", pronouns: {} }]);
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
@ -61,11 +73,53 @@ export default function Index() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const fieldsUpdated = !fieldsEqual(fields, originalOrder);
|
const fieldsUpdated = !fieldsEqual(fields, originalOrder);
|
||||||
|
const isEdited = fieldsUpdated;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container mx-auto">
|
<div className="container mx-auto">
|
||||||
|
<h1 className="p-2 border-b border-slate-300 dark:border-slate-600 flex items-center justify-between">
|
||||||
|
<span className="text-3xl">Editing your profile</span>
|
||||||
|
{isEdited && (
|
||||||
|
<Button
|
||||||
|
style={ButtonStyle.success}
|
||||||
|
onClick={() =>
|
||||||
|
updateUser({
|
||||||
|
displayName: state!.display_name,
|
||||||
|
bio: state!.bio,
|
||||||
|
fields,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Save aria-hidden className="inline" /> Save changes
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</h1>
|
||||||
|
|
||||||
<div>{`fieldsUpdated: ${fieldsUpdated}`}</div>
|
<div>{`fieldsUpdated: ${fieldsUpdated}`}</div>
|
||||||
{/* @ts-ignore: This component isn't updated to have a "children" prop yet, but it accepts it just fine. */}
|
<h3 className="p-2 border-b border-slate-300 dark:border-slate-600 flex items-center justify-between">
|
||||||
|
<span className="text-xl">Fields</span>
|
||||||
|
<div className="inline">
|
||||||
|
<Button
|
||||||
|
noRound
|
||||||
|
style={ButtonStyle.success}
|
||||||
|
onClick={() => addField()}
|
||||||
|
>
|
||||||
|
{" "}
|
||||||
|
<Plus aria-hidden className="inline" />
|
||||||
|
Add field
|
||||||
|
</Button>
|
||||||
|
{fieldsUpdated && (
|
||||||
|
<Button
|
||||||
|
noRound
|
||||||
|
style={ButtonStyle.danger}
|
||||||
|
onClick={() => resetFields()}
|
||||||
|
>
|
||||||
|
<Trash aria-hidden className="inline" />
|
||||||
|
Reset fields
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</h3>
|
||||||
<ReactSortable
|
<ReactSortable
|
||||||
handle=".handle"
|
handle=".handle"
|
||||||
list={fields}
|
list={fields}
|
||||||
|
@ -76,6 +130,22 @@ export default function Index() {
|
||||||
<EditableCard
|
<EditableCard
|
||||||
key={i}
|
key={i}
|
||||||
field={field}
|
field={field}
|
||||||
|
onChangePronoun={(e) => {
|
||||||
|
const prev =
|
||||||
|
e.target.attributes.getNamedItem("data-prev-value")?.value;
|
||||||
|
if (!prev || !e.target.value) return;
|
||||||
|
|
||||||
|
const choice = field.pronouns[prev];
|
||||||
|
delete field.pronouns[prev];
|
||||||
|
|
||||||
|
field.pronouns[e.target.value] = choice;
|
||||||
|
|
||||||
|
setFields([...fields]);
|
||||||
|
}}
|
||||||
|
onAddPronoun={(pronoun) => {
|
||||||
|
field.pronouns[pronoun] = PronounChoice.okay;
|
||||||
|
setFields([...fields]);
|
||||||
|
}}
|
||||||
onChangeName={(e) => {
|
onChangeName={(e) => {
|
||||||
field.name = e.target.value;
|
field.name = e.target.value;
|
||||||
setFields([...fields]);
|
setFields([...fields]);
|
||||||
|
@ -125,8 +195,8 @@ function fieldsEqual(arr1: EditField[], arr2: EditField[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateUser(args: {
|
async function updateUser(args: {
|
||||||
displayName: string;
|
displayName: string | null;
|
||||||
bio: string;
|
bio: string | null;
|
||||||
fields: EditField[];
|
fields: EditField[];
|
||||||
}) {
|
}) {
|
||||||
const newFields = args.fields.map((editField) => {
|
const newFields = args.fields.map((editField) => {
|
||||||
|
@ -139,22 +209,22 @@ async function updateUser(args: {
|
||||||
avoid: [],
|
avoid: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.keys(editField).forEach((pronoun) => {
|
Object.keys(editField.pronouns).forEach((pronoun) => {
|
||||||
switch (editField.pronouns[pronoun]) {
|
switch (editField.pronouns[pronoun]) {
|
||||||
case PronounChoice.favourite:
|
case PronounChoice.favourite:
|
||||||
field.favourite?.push(pronoun);
|
field.favourite!.push(pronoun);
|
||||||
break;
|
break;
|
||||||
case PronounChoice.okay:
|
case PronounChoice.okay:
|
||||||
field.okay?.push(pronoun);
|
field.okay!.push(pronoun);
|
||||||
break;
|
break;
|
||||||
case PronounChoice.jokingly:
|
case PronounChoice.jokingly:
|
||||||
field.jokingly?.push(pronoun);
|
field.jokingly!.push(pronoun);
|
||||||
break;
|
break;
|
||||||
case PronounChoice.friendsOnly:
|
case PronounChoice.friendsOnly:
|
||||||
field.friends_only?.push(pronoun);
|
field.friends_only!.push(pronoun);
|
||||||
break;
|
break;
|
||||||
case PronounChoice.avoid:
|
case PronounChoice.avoid:
|
||||||
field.avoid?.push(pronoun);
|
field.avoid!.push(pronoun);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -163,8 +233,8 @@ async function updateUser(args: {
|
||||||
});
|
});
|
||||||
|
|
||||||
return await fetchAPI<MeUser>("/users/@me", "PATCH", {
|
return await fetchAPI<MeUser>("/users/@me", "PATCH", {
|
||||||
display_name: args.displayName,
|
display_name: args.displayName ?? null,
|
||||||
bio: args.bio,
|
bio: args.bio ?? null,
|
||||||
fields: newFields,
|
fields: newFields,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue