mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-17 17:40:16 -04:00
make carpool notes editable
This commit is contained in:
parent
449308b2d6
commit
a01f44e8a6
|
@ -27,6 +27,7 @@ type CarpoolState = {
|
||||||
members: { id: number; name: string }[];
|
members: { id: number; name: string }[];
|
||||||
invitations: Record<number, ICarpool['invitations'][0]>;
|
invitations: Record<number, ICarpool['invitations'][0]>;
|
||||||
creatorId: number;
|
creatorId: number;
|
||||||
|
note: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const CarpoolContext = createContext({
|
export const CarpoolContext = createContext({
|
||||||
|
@ -66,6 +67,7 @@ export default function Carpool({ id }: { id: number }) {
|
||||||
});
|
});
|
||||||
setCarpool({
|
setCarpool({
|
||||||
id: carpool.id,
|
id: carpool.id,
|
||||||
|
note: carpool.note,
|
||||||
name: carpool.name,
|
name: carpool.name,
|
||||||
event: carpool.event,
|
event: carpool.event,
|
||||||
members: carpool.members,
|
members: carpool.members,
|
||||||
|
|
|
@ -1,11 +1,23 @@
|
||||||
import EventIcon from '@material-ui/icons/Event';
|
import EventIcon from '@material-ui/icons/Event';
|
||||||
import LocationOnIcon from '@material-ui/icons/LocationOn';
|
import LocationOnIcon from '@material-ui/icons/LocationOn';
|
||||||
|
import { useCallback } from 'react';
|
||||||
import { useContext } from 'react';
|
import { useContext } from 'react';
|
||||||
|
import { setCarpoolNote } from '../api';
|
||||||
|
|
||||||
import { CarpoolContext } from './Carpool';
|
import { CarpoolContext } from './Carpool';
|
||||||
|
import CarpoolNote from './CarpoolNote';
|
||||||
|
|
||||||
export default function CarpoolDetails() {
|
export default function CarpoolDetails() {
|
||||||
const { carpool } = useContext(CarpoolContext);
|
const { carpool } = useContext(CarpoolContext);
|
||||||
|
const onSaveCarpoolNote = useCallback(
|
||||||
|
(note) => {
|
||||||
|
setCarpoolNote(carpool.id, note).then(() => {
|
||||||
|
carpool.note = note;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[carpool]
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div
|
<div
|
||||||
|
@ -28,6 +40,7 @@ export default function CarpoolDetails() {
|
||||||
<EventIcon style={{ marginRight: '1rem' }} />
|
<EventIcon style={{ marginRight: '1rem' }} />
|
||||||
DAWN - DUSK
|
DAWN - DUSK
|
||||||
</div>
|
</div>
|
||||||
|
<CarpoolNote note={carpool.note} setNote={onSaveCarpoolNote} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
76
src/components/Carpool/CarpoolNote.tsx
Normal file
76
src/components/Carpool/CarpoolNote.tsx
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
|
import UIPressable from '../UI/UIPressable';
|
||||||
|
import UITextInput from '../UI/UITextInput';
|
||||||
|
import useIsCreator from './useIsCreator';
|
||||||
|
import EditIcon from '@material-ui/icons/Edit';
|
||||||
|
import DoneIcon from '@material-ui/icons/Done';
|
||||||
|
import CancelIcon from '@material-ui/icons/Clear';
|
||||||
|
|
||||||
|
export default function CarpoolNote({
|
||||||
|
note,
|
||||||
|
setNote,
|
||||||
|
}: {
|
||||||
|
note: string;
|
||||||
|
setNote: (note: string) => void;
|
||||||
|
}) {
|
||||||
|
const isCreator = useIsCreator();
|
||||||
|
const [editing, setEditing] = useState(false);
|
||||||
|
const [editText, setEditText] = useState(note);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setEditText(note);
|
||||||
|
}, [note]);
|
||||||
|
|
||||||
|
const onCancelNote = useCallback(() => {
|
||||||
|
setEditing(false);
|
||||||
|
setEditText(note);
|
||||||
|
}, [note]);
|
||||||
|
|
||||||
|
const onSaveNote = useCallback(() => {
|
||||||
|
setEditing(false);
|
||||||
|
setNote(editText);
|
||||||
|
}, [editText, setNote]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ marginTop: '0.5rem', padding: '1rem' }}>
|
||||||
|
<b>Note</b>
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
|
{editing ? (
|
||||||
|
<div>
|
||||||
|
<UITextInput
|
||||||
|
onChangeText={setEditText}
|
||||||
|
value={editText}
|
||||||
|
style={{ marginTop: 0 }}
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
marginTop: '1rem',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<DoneIcon
|
||||||
|
onClick={onSaveNote}
|
||||||
|
style={{ marginRight: '1rem', cursor: 'pointer' }}
|
||||||
|
/>
|
||||||
|
<CancelIcon
|
||||||
|
onClick={onCancelNote}
|
||||||
|
style={{ marginRight: '1rem', cursor: 'pointer' }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
||||||
|
<span>{note}</span>
|
||||||
|
{isCreator && (
|
||||||
|
<EditIcon
|
||||||
|
onClick={() => setEditing(true)}
|
||||||
|
style={{ marginTop: '1rem', cursor: 'pointer' }}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
|
@ -248,6 +248,10 @@ export async function createCarpool({
|
||||||
return { id };
|
return { id };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function setCarpoolNote(carpoolId: number, note: string) {
|
||||||
|
return await post(`/carpools/${carpoolId}/set_note`, { note });
|
||||||
|
}
|
||||||
|
|
||||||
export async function sendCarpoolInvite(carpoolId: number, userId: number) {
|
export async function sendCarpoolInvite(carpoolId: number, userId: number) {
|
||||||
await post(`/carpools/${carpoolId}/invite`, { userId });
|
await post(`/carpools/${carpoolId}/invite`, { userId });
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ export type IUser = {
|
||||||
export type ICarpool = {
|
export type ICarpool = {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
|
note: string;
|
||||||
eventId: number | null;
|
eventId: number | null;
|
||||||
event: {
|
event: {
|
||||||
id: number;
|
id: number;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user