import { useCallback, useState, useEffect } from 'react'; import { defineMessages, useIntl, FormattedMessage } from 'react-intl'; import { Helmet } from 'react-helmet'; import { useParams, useHistory } from 'react-router-dom'; import { isFulfilled } from '@reduxjs/toolkit'; import ListAltIcon from '@/material-icons/400-24px/list_alt.svg?react'; import type { ApiCollectionJSON, ApiCreateCollectionPayload, } from 'mastodon/api_types/collections'; import { Button } from 'mastodon/components/button'; import { Column } from 'mastodon/components/column'; import { ColumnHeader } from 'mastodon/components/column_header'; import { TextAreaField, ToggleField } from 'mastodon/components/form_fields'; import { TextInputField } from 'mastodon/components/form_fields/text_input_field'; import { LoadingIndicator } from 'mastodon/components/loading_indicator'; import { createCollection } from 'mastodon/reducers/slices/collections'; import { useAppDispatch, useAppSelector } from 'mastodon/store'; const messages = defineMessages({ edit: { id: 'column.edit_collection', defaultMessage: 'Edit collection' }, create: { id: 'column.create_collection', defaultMessage: 'Create collection', }, }); const CollectionSettings: React.FC<{ collection?: ApiCollectionJSON | null; }> = ({ collection }) => { const dispatch = useAppDispatch(); const history = useHistory(); const { id, name: initialName = '', description: initialDescription = '', tag, discoverable: initialDiscoverable = true, sensitive: initialSensitive = false, } = collection ?? {}; const [name, setName] = useState(initialName); const [description, setDescription] = useState(initialDescription); const [topic, setTopic] = useState(tag?.name ?? ''); const [discoverable] = useState(initialDiscoverable); const [sensitive, setSensitive] = useState(initialSensitive); const handleNameChange = useCallback( (event: React.ChangeEvent) => { setName(event.target.value); }, [], ); const handleDescriptionChange = useCallback( (event: React.ChangeEvent) => { setDescription(event.target.value); }, [], ); const handleTopicChange = useCallback( (event: React.ChangeEvent) => { setTopic(event.target.value); }, [], ); const handleSensitiveChange = useCallback( (event: React.ChangeEvent) => { setSensitive(event.target.checked); }, [], ); const handleSubmit = useCallback( (e: React.FormEvent) => { e.preventDefault(); if (id) { // void dispatch( // updateList({ // id, // title, // exclusive, // replies_policy: repliesPolicy, // }), // ).then(() => { // return ''; // }); } else { const payload: ApiCreateCollectionPayload = { name, description, discoverable, sensitive, }; if (topic) { payload.tag_name = topic; } void dispatch( createCollection({ payload, }), ).then((result) => { if (isFulfilled(result)) { history.replace( `/collections/${result.payload.collection.id}/edit`, ); history.push(`/collections`); } return ''; }); } }, [id, dispatch, name, description, topic, discoverable, sensitive, history], ); return (
} hint={ } value={name} onChange={handleNameChange} maxLength={40} />
} hint={ } value={description} onChange={handleDescriptionChange} maxLength={100} />
} hint={ } value={topic} onChange={handleTopicChange} maxLength={40} />
} hint={ } checked={sensitive} onChange={handleSensitiveChange} />
); }; export const CollectionEditorPage: React.FC<{ multiColumn?: boolean; }> = ({ multiColumn }) => { const intl = useIntl(); const dispatch = useAppDispatch(); const { id } = useParams<{ id?: string }>(); const collection = useAppSelector((state) => id ? state.collections.collections[id] : undefined, ); const isEditMode = !!id; const isLoading = isEditMode && !collection; useEffect(() => { // if (id) { // dispatch(fetchCollection(id)); // } }, [dispatch, id]); const pageTitle = intl.formatMessage(id ? messages.edit : messages.create); return (
{isLoading ? ( ) : ( )}
{pageTitle}
); };