|
|
|
@ -15,26 +15,26 @@ interface EditorProps { |
|
|
|
|
const Editor: React.FC<EditorProps> = ({ links, setLinks }) => { |
|
|
|
|
const { displayDragDrop } = useDragDrop(); |
|
|
|
|
const { headers } = useAuth(); |
|
|
|
|
const [formState, setFormState] = useState<EditableLink[]>(links); |
|
|
|
|
const [editableLinks, setEditableLinks] = useState<EditableLink[]>(links); |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
setFormState(links); |
|
|
|
|
setEditableLinks(links); |
|
|
|
|
}, [links]); |
|
|
|
|
|
|
|
|
|
const handleOnDragEnd = (result: DropResult) => { |
|
|
|
|
if (!result?.destination) return; |
|
|
|
|
|
|
|
|
|
const items = Array.from(formState); |
|
|
|
|
const items = Array.from(editableLinks); |
|
|
|
|
const [reorderedItem] = items.splice(result.source.index, 1); |
|
|
|
|
items.splice(result.destination.index, 0, reorderedItem); |
|
|
|
|
|
|
|
|
|
setFormState(items); |
|
|
|
|
setEditableLinks(items); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/*note that we need to make the new links name render with nothing*/ |
|
|
|
|
const handleOnClickAdd = () => |
|
|
|
|
setFormState([ |
|
|
|
|
...formState, |
|
|
|
|
setEditableLinks([ |
|
|
|
|
...editableLinks, |
|
|
|
|
{ |
|
|
|
|
name: "", |
|
|
|
|
url: "", |
|
|
|
@ -44,21 +44,20 @@ const Editor: React.FC<EditorProps> = ({ links, setLinks }) => { |
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
const onSubmit = async () => { |
|
|
|
|
// const res = await updateLinks(formState);
|
|
|
|
|
const res = await fetch("/api/editor/links", { |
|
|
|
|
method: "POST", |
|
|
|
|
headers: { |
|
|
|
|
"Content-Type": "application/json", |
|
|
|
|
...headers, |
|
|
|
|
}, |
|
|
|
|
body: JSON.stringify({ links: formState }), |
|
|
|
|
body: JSON.stringify({ links: editableLinks }), |
|
|
|
|
}); |
|
|
|
|
const updatedLinks = await res.json(); |
|
|
|
|
setLinks(updatedLinks); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const onCancel = () => { |
|
|
|
|
setFormState(links); |
|
|
|
|
setEditableLinks(links); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
@ -80,22 +79,22 @@ const Editor: React.FC<EditorProps> = ({ links, setLinks }) => { |
|
|
|
|
{...provided.droppableProps} |
|
|
|
|
ref={provided.innerRef} |
|
|
|
|
> |
|
|
|
|
{formState.map((link, index) => ( |
|
|
|
|
{editableLinks.map((link, index) => ( |
|
|
|
|
<Link |
|
|
|
|
key={index} |
|
|
|
|
index={index} |
|
|
|
|
link={link} |
|
|
|
|
onChange={(newLink: EditableLink) => |
|
|
|
|
setFormState([ |
|
|
|
|
...formState.slice(0, index), |
|
|
|
|
setEditableLinks([ |
|
|
|
|
...editableLinks.slice(0, index), |
|
|
|
|
newLink, |
|
|
|
|
...formState.slice(index + 1), |
|
|
|
|
...editableLinks.slice(index + 1), |
|
|
|
|
]) |
|
|
|
|
} |
|
|
|
|
onDelete={() => |
|
|
|
|
setFormState([ |
|
|
|
|
...formState.slice(0, index), |
|
|
|
|
...formState.slice(index + 1), |
|
|
|
|
setEditableLinks([ |
|
|
|
|
...editableLinks.slice(0, index), |
|
|
|
|
...editableLinks.slice(index + 1), |
|
|
|
|
]) |
|
|
|
|
} |
|
|
|
|
/> |
|
|
|
@ -111,7 +110,7 @@ const Editor: React.FC<EditorProps> = ({ links, setLinks }) => { |
|
|
|
|
<button |
|
|
|
|
className="block flex py-2 items-center justify-center rounded-md bg-purple-600 hover:bg-purple-500 cursor-pointer text-white w-full disabled:opacity-50" |
|
|
|
|
onClick={onSubmit} |
|
|
|
|
disabled={equal(formState, links)} |
|
|
|
|
disabled={equal(editableLinks, links)} |
|
|
|
|
> |
|
|
|
|
Submit |
|
|
|
|
</button> |
|
|
|
@ -119,13 +118,13 @@ const Editor: React.FC<EditorProps> = ({ links, setLinks }) => { |
|
|
|
|
<button |
|
|
|
|
className="block flex py-2 items-center justify-center rounded-md bg-purple-600 hover:bg-purple-500 cursor-pointer text-white w-full disabled:opacity-50" |
|
|
|
|
onClick={onCancel} |
|
|
|
|
disabled={equal(formState, links)} |
|
|
|
|
disabled={equal(editableLinks, links)} |
|
|
|
|
> |
|
|
|
|
Cancel |
|
|
|
|
</button> |
|
|
|
|
</div> |
|
|
|
|
</div> |
|
|
|
|
<Preview links={formState} /> |
|
|
|
|
<Preview links={editableLinks.filter((link) => link.active)} /> |
|
|
|
|
</div> |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|