-
Notifications
You must be signed in to change notification settings - Fork 17
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 #30 from ilong4rennes/refactor/component-files
Refactor/component files
- Loading branch information
Showing
6 changed files
with
684 additions
and
764 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,65 @@ | ||
import React from "react"; | ||
import React, { useEffect, useState } from "react"; | ||
import DatePeriod from "../Models/DatePeriod"; | ||
import { utcToday } from "../utils"; | ||
import DatePeriodInput from "./DatePeriodInput"; | ||
import DatePeriod from "../Models/DatePeriod"; | ||
|
||
export default class CategoryPeriodsInput extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { periods: [] }; | ||
} | ||
const CategoryPeriodsInput = ({ categoryPeriods, showHistoric }) => { | ||
const [periods, setPeriods] = useState([]); | ||
|
||
componentDidMount() { | ||
const { categoryPeriods } = this.props; | ||
categoryPeriods.subscribe(() => { | ||
this.setState({ periods: categoryPeriods.periods }); | ||
useEffect(() => { | ||
const unsubscribe = categoryPeriods.subscribe(() => { | ||
setPeriods(categoryPeriods.periods); | ||
}); | ||
} | ||
|
||
render() { | ||
const { category } = this.props.categoryPeriods; | ||
const { showHistoric } = this.props; | ||
const { periods } = this.state; | ||
|
||
const today = utcToday(); | ||
const categoryTitle = { | ||
labaccess: "Labaccess", | ||
special_labaccess: "Access till lokalen (styrelsen etc)", | ||
membership: "Medlemsskap", | ||
return () => { | ||
unsubscribe(); | ||
}; | ||
}, [categoryPeriods]); | ||
|
||
const { category } = categoryPeriods; | ||
|
||
const today = utcToday(); | ||
const categoryTitle = { | ||
labaccess: "Labaccess", | ||
special_labaccess: "Access till lokalen (styrelsen etc)", | ||
membership: "Medlemsskap", | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h4 className="uk-margin-top">{categoryTitle[category]}</h4> | ||
{periods.map((p) => { | ||
if (!showHistoric && p.end < today) { | ||
return null; | ||
} | ||
return ( | ||
<div key={p.id}> | ||
<DatePeriodInput period={p} /> | ||
| ||
<a | ||
onClick={() => categoryPeriods.remove(p)} | ||
className="removebutton" | ||
> | ||
<i className="uk-icon-trash" /> | ||
</a> | ||
</div> | ||
); | ||
})} | ||
<button | ||
type="button" | ||
style={{ marginTop: "2px" }} | ||
className="uk-button uk-button-small uk-button-success" | ||
onClick={() => { | ||
const period = new DatePeriod(); | ||
period.start = utcToday(); | ||
period.end = utcToday(); | ||
categoryPeriods.add(period); | ||
}} | ||
> | ||
Lägg till period | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h4 className="uk-margin-top">{categoryTitle[category]}</h4> | ||
{periods.map((p) => { | ||
if (!showHistoric && p.end < today) { | ||
return null; | ||
} | ||
return ( | ||
<div key={p.id}> | ||
<DatePeriodInput key={p.id} period={p} /> | ||
| ||
<a | ||
onClick={() => | ||
this.props.categoryPeriods.remove(p) | ||
} | ||
className="removebutton" | ||
> | ||
<i className="uk-icon-trash" /> | ||
</a> | ||
</div> | ||
); | ||
})} | ||
<button | ||
type="button" | ||
style={{ marginTop: "2px" }} | ||
className="uk-button uk-button-small uk-button-success" | ||
onClick={() => { | ||
const period = new DatePeriod(); | ||
period.start = utcToday(); | ||
period.end = utcToday(); | ||
this.props.categoryPeriods.add(period); | ||
}} | ||
> | ||
Lägg till period | ||
</button> | ||
</div> | ||
); | ||
} | ||
} | ||
export default CategoryPeriodsInput; |
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
Oops, something went wrong.