mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-17 10:11:31 +10:00
169 lines
4.4 KiB
JavaScript
169 lines
4.4 KiB
JavaScript
import React, { useState, useContext } from 'react';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import set from 'lodash/set';
|
|
|
|
import TextField from '../../../shared/TextField';
|
|
import AppContext from '../../../context/AppContext';
|
|
import Checkbox from '../../../shared/Checkbox';
|
|
import { addItem, deleteItem, moveItemUp, moveItemDown } from '../../../utils';
|
|
import ItemActions from '../../../shared/ItemActions';
|
|
|
|
const ExtrasTab = ({ data, onChange }) => {
|
|
const context = useContext(AppContext);
|
|
const { dispatch } = context;
|
|
|
|
return (
|
|
<>
|
|
<div className="mb-6 grid grid-cols-6 items-center">
|
|
<div className="col-span-1">
|
|
<Checkbox
|
|
checked={data.extras.enable}
|
|
onChange={v => onChange('data.extras.enable', v)}
|
|
/>
|
|
</div>
|
|
<div className="col-span-5">
|
|
<TextField
|
|
placeholder="Heading"
|
|
value={data.extras.heading}
|
|
onChange={v => onChange('data.extras.heading', v)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<hr className="my-6" />
|
|
|
|
{data.extras.items.map((x, index) => (
|
|
<Item
|
|
item={x}
|
|
key={x.id}
|
|
index={index}
|
|
onChange={onChange}
|
|
dispatch={dispatch}
|
|
first={index === 0}
|
|
last={index === data.extras.items.length - 1}
|
|
/>
|
|
))}
|
|
|
|
<AddItem dispatch={dispatch} />
|
|
</>
|
|
);
|
|
};
|
|
|
|
const AddItem = ({ dispatch }) => {
|
|
const [isOpen, setOpen] = useState(false);
|
|
const [item, setItem] = useState({
|
|
id: uuidv4(),
|
|
enable: true,
|
|
key: '',
|
|
value: '',
|
|
});
|
|
|
|
const onChange = (key, value) => setItem(items => set({ ...items }, key, value));
|
|
|
|
const onSubmit = () => {
|
|
if (item.key === '' || item.value === '') return;
|
|
|
|
addItem(dispatch, 'extras', item);
|
|
|
|
setItem({
|
|
id: uuidv4(),
|
|
enable: true,
|
|
key: '',
|
|
value: '',
|
|
});
|
|
|
|
setOpen(false);
|
|
};
|
|
|
|
return (
|
|
<div className="my-4 border border-gray-200 rounded p-5">
|
|
<div
|
|
className="flex justify-between items-center cursor-pointer"
|
|
onClick={() => setOpen(!isOpen)}
|
|
>
|
|
<h6 className="text-sm font-medium">Add Item</h6>
|
|
<i className="material-icons">{isOpen ? 'expand_less' : 'expand_more'}</i>
|
|
</div>
|
|
|
|
<div className={`mt-6 ${isOpen ? 'block' : 'hidden'}`}>
|
|
<TextField
|
|
label="Key"
|
|
className="mb-6"
|
|
placeholder="Date of Birth"
|
|
value={item.key}
|
|
onChange={v => onChange('key', v)}
|
|
/>
|
|
|
|
<TextField
|
|
label="Value"
|
|
className="mb-6"
|
|
placeholder="6th August 1995"
|
|
value={item.value}
|
|
onChange={v => onChange('value', v)}
|
|
/>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={onSubmit}
|
|
className="bg-gray-600 hover:bg-gray-700 text-white text-sm font-medium py-2 px-5 rounded"
|
|
>
|
|
<div className="flex items-center">
|
|
<i className="material-icons mr-2 font-bold text-base">add</i>
|
|
<span className="text-sm">Add</span>
|
|
</div>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
const Item = ({ item, index, onChange, dispatch, first, last }) => {
|
|
const [isOpen, setOpen] = useState(false);
|
|
const identifier = `data.extras.items[${index}]`;
|
|
|
|
return (
|
|
<div className="my-4 border border-gray-200 rounded p-5">
|
|
<div
|
|
className="flex justify-between items-center cursor-pointer"
|
|
onClick={() => setOpen(!isOpen)}
|
|
>
|
|
<h6 className="text-sm font-medium">{item.key}</h6>
|
|
<i className="material-icons">{isOpen ? 'expand_less' : 'expand_more'}</i>
|
|
</div>
|
|
|
|
<div className={`mt-6 ${isOpen ? 'block' : 'hidden'}`}>
|
|
<TextField
|
|
label="Key"
|
|
className="mb-6"
|
|
placeholder="Date of Birth"
|
|
value={item.key}
|
|
onChange={v => onChange(`${identifier}.key`, v)}
|
|
/>
|
|
|
|
<TextField
|
|
label="Value"
|
|
className="mb-6"
|
|
placeholder="6th August 1995"
|
|
value={item.value}
|
|
onChange={v => onChange(`${identifier}.value`, v)}
|
|
/>
|
|
|
|
<ItemActions
|
|
item={item}
|
|
onChange={onChange}
|
|
type="extras"
|
|
identifier={identifier}
|
|
dispatch={dispatch}
|
|
deleteItem={deleteItem}
|
|
first={first}
|
|
moveItemUp={moveItemUp}
|
|
last={last}
|
|
moveItemDown={moveItemDown}
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default ExtrasTab;
|