-
Notifications
You must be signed in to change notification settings - Fork 56
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 #2416 from zetkin/undocumented/join-form-smart-search
Join form smart search
- Loading branch information
Showing
9 changed files
with
238 additions
and
7 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
46 changes: 46 additions & 0 deletions
46
src/features/smartSearch/components/filters/JoinForm/DisplayJoinForm.tsx
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,46 @@ | ||
import { FC } from 'react'; | ||
|
||
import { | ||
JoinFormFilterConfig, | ||
OPERATION, | ||
SmartSearchFilterWithId, | ||
} from '../../types'; | ||
import { Msg } from 'core/i18n'; | ||
import messageIds from 'features/smartSearch/l10n/messageIds'; | ||
import UnderlinedMsg from '../../UnderlinedMsg'; | ||
import { useNumericRouteParams } from 'core/hooks'; | ||
import DisplayJoinFormTitle from './DisplayJoinFormTitle'; | ||
import DisplayTimeFrame from '../DisplayTimeFrame'; | ||
import { getTimeFrameWithConfig } from '../../utils'; | ||
|
||
type Props = { | ||
filter: SmartSearchFilterWithId<JoinFormFilterConfig>; | ||
}; | ||
|
||
const localMessageIds = messageIds.filters.joinForm; | ||
|
||
const DisplayJoinForm: FC<Props> = ({ filter }) => { | ||
const { orgId } = useNumericRouteParams(); | ||
const op = filter.op || OPERATION.ADD; | ||
const timeFrame = getTimeFrameWithConfig({ | ||
after: filter.config.submitted?.after, | ||
before: filter.config.submitted?.before, | ||
}); | ||
|
||
return ( | ||
<Msg | ||
id={localMessageIds.inputString} | ||
values={{ | ||
addRemoveSelect: <UnderlinedMsg id={messageIds.operators[op]} />, | ||
formSelect: filter.config.form ? ( | ||
<DisplayJoinFormTitle formId={filter.config.form} orgId={orgId} /> | ||
) : ( | ||
<UnderlinedMsg id={localMessageIds.anyForm} /> | ||
), | ||
timeFrame: <DisplayTimeFrame config={timeFrame} />, | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default DisplayJoinForm; |
25 changes: 25 additions & 0 deletions
25
src/features/smartSearch/components/filters/JoinForm/DisplayJoinFormTitle.tsx
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,25 @@ | ||
import { FC } from 'react'; | ||
|
||
import messageIds from 'features/smartSearch/l10n/messageIds'; | ||
import useJoinForm from 'features/joinForms/hooks/useJoinForm'; | ||
import UnderlinedMsg from '../../UnderlinedMsg'; | ||
|
||
type Props = { | ||
formId: number; | ||
orgId: number; | ||
}; | ||
|
||
const localMessageIds = messageIds.filters.joinForm; | ||
|
||
const DisplayJoinFormTitle: FC<Props> = ({ formId, orgId }) => { | ||
const { data } = useJoinForm(orgId, formId); | ||
|
||
return ( | ||
<UnderlinedMsg | ||
id={localMessageIds.form} | ||
values={{ title: data?.title || '' }} | ||
/> | ||
); | ||
}; | ||
|
||
export default DisplayJoinFormTitle; |
116 changes: 116 additions & 0 deletions
116
src/features/smartSearch/components/filters/JoinForm/index.tsx
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,116 @@ | ||
import { FC, FormEvent } from 'react'; | ||
import { MenuItem } from '@mui/material'; | ||
|
||
import { | ||
JoinFormFilterConfig, | ||
NewSmartSearchFilter, | ||
OPERATION, | ||
SmartSearchFilterWithId, | ||
TIME_FRAME, | ||
ZetkinSmartSearchFilter, | ||
} from '../../types'; | ||
import FilterForm from '../../FilterForm'; | ||
import { Msg } from 'core/i18n'; | ||
import messageIds from 'features/smartSearch/l10n/messageIds'; | ||
import StyledSelect from '../../inputs/StyledSelect'; | ||
import useSmartSearchFilter from 'features/smartSearch/hooks/useSmartSearchFilter'; | ||
import TimeFrame from '../TimeFrame'; | ||
import useJoinForms from 'features/joinForms/hooks/useJoinForms'; | ||
import { useNumericRouteParams } from 'core/hooks'; | ||
|
||
type Props = { | ||
filter: SmartSearchFilterWithId<JoinFormFilterConfig> | NewSmartSearchFilter; | ||
onCancel: () => void; | ||
onSubmit: ( | ||
filter: | ||
| SmartSearchFilterWithId<JoinFormFilterConfig> | ||
| ZetkinSmartSearchFilter<JoinFormFilterConfig> | ||
) => void; | ||
}; | ||
|
||
const localMessageIds = messageIds.filters.joinForm; | ||
|
||
const JoinFormFilter: FC<Props> = ({ | ||
filter: initialFilter, | ||
onSubmit, | ||
onCancel, | ||
}) => { | ||
const { orgId } = useNumericRouteParams(); | ||
const forms = useJoinForms(orgId).data || []; | ||
const { filter, setConfig, setOp } = | ||
useSmartSearchFilter<JoinFormFilterConfig>(initialFilter, {}); | ||
|
||
const handleSubmit = (e: FormEvent) => { | ||
e.preventDefault(); | ||
onSubmit(filter); | ||
}; | ||
|
||
return ( | ||
<FilterForm | ||
onCancel={onCancel} | ||
onSubmit={handleSubmit} | ||
renderSentence={() => ( | ||
<Msg | ||
id={localMessageIds.inputString} | ||
values={{ | ||
addRemoveSelect: ( | ||
<StyledSelect | ||
onChange={(e) => setOp(e.target.value as OPERATION)} | ||
value={filter.op} | ||
> | ||
{Object.values(OPERATION).map((o) => ( | ||
<MenuItem key={o} value={o}> | ||
<Msg id={messageIds.operators[o]} /> | ||
</MenuItem> | ||
))} | ||
</StyledSelect> | ||
), | ||
formSelect: ( | ||
<StyledSelect | ||
onChange={(e) => { | ||
const formId = parseInt(e.target.value); | ||
const config = { ...filter.config }; | ||
if (formId) { | ||
config.form = formId; | ||
} else { | ||
delete config.form; | ||
} | ||
|
||
setConfig(config); | ||
}} | ||
value={filter.config.form || 'any'} | ||
> | ||
<MenuItem value="any"> | ||
<Msg id={localMessageIds.anyForm} /> | ||
</MenuItem> | ||
{forms.map((form) => ( | ||
<MenuItem key={form.id} value={form.id}> | ||
{form.title} | ||
</MenuItem> | ||
))} | ||
</StyledSelect> | ||
), | ||
timeFrame: ( | ||
<TimeFrame | ||
filterConfig={filter.config.submitted || {}} | ||
onChange={(range) => | ||
setConfig({ ...filter.config, submitted: range }) | ||
} | ||
options={[ | ||
TIME_FRAME.AFTER_DATE, | ||
TIME_FRAME.BEFORE_DATE, | ||
TIME_FRAME.BEFORE_TODAY, | ||
TIME_FRAME.BETWEEN, | ||
TIME_FRAME.EVER, | ||
TIME_FRAME.LAST_FEW_DAYS, | ||
]} | ||
/> | ||
), | ||
}} | ||
/> | ||
)} | ||
/> | ||
); | ||
}; | ||
|
||
export default JoinFormFilter; |
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