Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to change hat name after hat has been created. #45

Merged
merged 1 commit into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/components/SettingsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ const backgroundColors = [
'#CA6EFE',
];

// TODO: add option to change hat name

interface Props {
show: boolean;
handleClose: () => void;
Expand All @@ -62,19 +60,22 @@ const SettingsModal = ({ show, handleClose }: Props) => {
const {
semscreen: { settings, updateSettings },
me,
hats: { selectedHat, setHatName },
} = useContext(DataContext)!;

const [values, setValues] = useState({
username: me.username,
hatName: selectedHat.name,
textColor: settings.textColor,
backgroundColor: settings.backgroundColor,
});

function updateSession() {
const { textColor, backgroundColor, username } = values;
const { textColor, backgroundColor, username, hatName } = values;
console.log(values);
updateSettings({ textColor, backgroundColor });
if (username !== me.username) me.setUsername(username);
if (hatName !== selectedHat.name) setHatName(selectedHat.id, hatName);
handleClose();
}

Expand All @@ -86,7 +87,7 @@ const SettingsModal = ({ show, handleClose }: Props) => {
</Modal.Header>
<Modal.Body>
<Form.Group>
<Form.Label>Username</Form.Label>
<Form.Label>Notetaker</Form.Label>
<Form.Control
name="username"
value={values.username}
Expand All @@ -96,6 +97,17 @@ const SettingsModal = ({ show, handleClose }: Props) => {
/>
</Form.Group>

<Form.Group>
<Form.Label>Hat</Form.Label>
<Form.Control
name="hatName"
value={values.hatName}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setValues({ ...values, hatName: e.target.value })
}
/>
</Form.Group>

<Form.Group>
<Form.Label>Text color</Form.Label>
<TwitterPicker
Expand Down
21 changes: 21 additions & 0 deletions src/hooks/useHats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ type ChangeHatIconI = (
hatColorIndex: number
) => void;

type SetHatNameI = (
id: string,
name: string,
) => void;

type SwitchSelectedHatI = (id: string) => void;

type CreateHatI = (name: string) => void;
Expand All @@ -39,6 +44,7 @@ interface UseHatsI {
destroyHat: DestroyHatI;
selectedHat: HatI;
setSelectedHat: (hat: HatI) => void;
setHatName: SetHatNameI;
switchSelectedHat: SwitchSelectedHatI;
changeHatIcon: ChangeHatIconI;
}
Expand All @@ -49,6 +55,20 @@ export default function useHats(): UseHatsI {
const [hats, setHats] = useLocalStorage('hats', [...initialAppStateV2.hats]);
const [selectedHat, setSelectedHat] = useLocalStorage('selectedHat', hats[0]);

const setHatName: SetHatNameI = (id, name) => {
setHats(
hats.map((h: HatI) => {
if (h.id === id) {
return {
...h,
name,
};
}
return h;
})
);
};

const changeHatIcon: ChangeHatIconI = (id, hatIndex, hatColorIndex) => {
setHats(
hats.map((h: HatI) => {
Expand Down Expand Up @@ -127,6 +147,7 @@ export default function useHats(): UseHatsI {
selectedHat,
setSelectedHat,
switchSelectedHat,
setHatName,
changeHatIcon,
};
}
Expand Down