This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from bisfun-hatfes/editable-ship-name
Editable ship names
- Loading branch information
Showing
10 changed files
with
92 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react' | ||
|
||
export const Pencil = ({ className = '', primary = ''}: IconProps) => ( | ||
<svg xmlns="http://www.w3.org/2000/svg" className={className} viewBox="0 0 20 20" fill={primary}> | ||
<path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z" /> | ||
</svg> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from 'react' | ||
import { useForm } from 'react-hook-form' | ||
import { useQueryClient, useMutation } from 'react-query' | ||
import { pierKey } from "../../query-keys"; | ||
import { Pencil } from '../../icons/Pencil' | ||
import { Close } from '../../icons/Close' | ||
import { Pier, UpdatePier } from "../../../background/services/pier-service" | ||
import { send } from '../../client/ipc' | ||
import { NameField } from '../../details/components/NameField' | ||
|
||
export const EditableShipName = (props: { ship: Pier }) => { | ||
const [editMode, setEditMode] = React.useState(false) | ||
const queryClient = useQueryClient() | ||
|
||
const form = useForm<UpdatePier>({ | ||
mode: 'onChange', | ||
reValidateMode: 'onChange', | ||
defaultValues: { | ||
name: props.ship.name | ||
} | ||
}) | ||
const { mutate } = useMutation((data: UpdatePier) => send('update-pier', {...props.ship, ...data}), { | ||
onSuccess: (pier: Pier) => { | ||
queryClient.setQueryData(pierKey(pier.slug), pier) | ||
setEditMode(false) | ||
} | ||
}) | ||
|
||
const { isValid } = form.formState | ||
|
||
const ShipNameDisplay = ( | ||
<div className="w-2/3 flex flex-row items-baseline"> | ||
<div className="mr-3 text-xl text-black dark:text-white whitespace-nowrap">{ props.ship.name }</div> | ||
<button className="button opacity-70" onClick={setEditMode.bind(this, true)}> | ||
<Pencil className="w-4 h-4 opacity-70" primary="gray"/> | ||
</button> | ||
</div> | ||
) | ||
|
||
const ShipNameEdit = ( | ||
<form onSubmit={form.handleSubmit((data) => mutate(data))}> | ||
<div className="w-100 flex flex-row baseline"> | ||
<div className="w-2/3 mr-2 text-black dark:text-white"> | ||
<NameField form={form} /> | ||
</div> | ||
<div className="relative"> | ||
<button className="button absolute top-2 font-semibold text-gray-300 dark:text-gray-700" type="submit" disabled={!isValid}> | ||
Save | ||
</button> | ||
<button className="button absolute top-2 p-1.5 left-14" onClick={setEditMode.bind(this, false)}> | ||
<Close className="w-4 h-4" primary="fill-current opacity-40" /> | ||
</button> | ||
</div> | ||
</div> | ||
</form> | ||
) | ||
|
||
return ( | ||
<div className="w-100 flex flex-row items-baseline"> | ||
{ | ||
editMode | ||
? ShipNameEdit | ||
: ShipNameDisplay | ||
} | ||
</div> | ||
) | ||
} |